archangeldesign/php-gson

PHP GSON 等价物 - 简单的实体映射器。JSON 到对象和对象到 JSON

0.2 2019-07-25 18:05 UTC

This package is auto-updated.

Last update: 2024-08-29 03:11:34 UTC


README

Build Status Coverage Status

无依赖简单 PHP 实体映射器。JSON 字符串到对象和对象到 JSON 字符串。

安装

composer require archangeldesign/php-gson

或下载并包含 autoload.php

include 'php-gson/src/PHPGson/autoload.php';

使用

无实例。对象将使用给定的类名创建。

$complexObject = null;
$success = \PHPGson\Gson::fromJson(
    $complexObject,
    '{"age":35, "hydratorTestObject":{"username":"raff"}}',
    \PHPGson\Extractor::EXTRACTION_MODE_METHOD,
    ComplexHydrationObject::class
);

手动实例化。在两种情况下,都会自动创建子对象。

$complexObject = new ComplexHydrationObject();
$success = \PHPGson\Gson::fromJson(
    $complexObject,
    '{"age":35, "hydratorTestObject":{"username":"raff"}}'
);
$object = new MainObject();
$jsonString = Gson::toJson($object);