承接 mobijay/json-serializer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mobijay/json-serializer

最新稳定版本:0.2.5

Composer 安装命令:

composer require mobijay/json-serializer

包简介

Serialize PHP object using attribute definitions

README 文档

README

The class you wants to serialize must implements \JsonSerializable

Then you have to use JsonSerializeByAttribute Trait. It contains the serialization method;

for each field or method you want to serialize add the following attribute `#[JsonSerialize] the field name is used as key in the serialized json.

If you want to change the key in the json you have to add it #[JsonSerialize('customPrivateField')]

If null value shouldn't serialized you have to specify it #[JsonSerialize('customPrivateField',false)] by default it is true

A complete example:

`

{
use JsonSerializeByAttribute;

    #[JsonSerialize('nullField1',false)]
    private ?string $nullField1;
    #[JsonSerialize('nullField2',true)]
    private ?string $nullField2 = null;
    
    #[JsonSerialize('customPrivateField')]
    private $privateField;

    public $disturbanceField1;
    private $disturbanceField2;

    #[JsonSerialize] 
    public $publicField;

    #[JsonSerialize] public Carbon $carbonField;
    #[JsonSerialize] public array $unSetArrayField;
    #[JsonSerialize] public array $arrayField;
    #[JsonSerialize] public array $associativeArrayField;

    public JsonTestOneNested $jsonTestOneNested1;

    #[JsonSerialize]
    public JsonTestOneNested $jsonTestOneNested2;

    #[JsonSerialize("objectWithoutSerializable")]
    public JsonTestOneNestedWithoutJsonSerializable $jsonTestOneNestedWithoutJsonSerializable1;

    public function __construct()
    {
        $this->privateField = 'privateFieldValue';
        $this->publicField = 'publicFieldValue';
        $this->carbonField = Carbon::createFromFormat('Y-m-d H:i:s', '2023-10-23 11:33:32');
        $this->associativeArrayField = ["testKey1" => "testValue1", "testKey2" => "testValue2",];
        $this->arrayField = ["test1", "test2"];
        $this->jsonTestOneNested2 = new JsonTestOneNested();
        $this->jsonTestOneNestedWithoutJsonSerializable1 = new JsonTestOneNestedWithoutJsonSerializable();

    }


    #[\Mobijay\JsonSerializer\Attributes\JsonSerialize('customPublicMethod')]
    public function publicMethod()
    {
        return 'publicMethodValue';
    }

    #[\Mobijay\JsonSerializer\Attributes\JsonSerialize]
    public function privateMethod()
    {
        return 'privateMethodValue';
    }

    public function disturbanceMethod()
    {
        return 'disturbanceMethodValue';
    }

}

class JsonTestOneNested implements \JsonSerializable
{
use JsonSerializeByAttribute;

    #[JsonSerialize('nestedCustomPrivateField')]
    private $privateField = "nestedPrivateFieldValue";

}

class JsonTestOneNestedWithoutJsonSerializable
{
private $privateField = "nestedPrivateFieldValueWithout";
public $publicFieldWithoutSerializable = "publicFieldWithoutSerializableValue";
}```




统计信息

  • 总下载量: 220
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-03-05