JSON encoding support library for nested object and assoc array
php >= 5.5.x
Omelet can be installed with Composer.
Define the following requirement in your composer.json file:
{ "require": { "ritalin/php-json-subset-encoder ": "*" } }
-
Create filter rule.
$meta = \JsonEncoder\FilterRule::newRule() ->includes(['intField']) ->nestRule([ 'objField' => \JsonEncoder\FilterRule::newRule()->includes(['b', 'c']) ]);
-
Build serializer.
// For example, object array. $values = [ new NestObject('www', 10, new SomeObject('a', 'b', 'c')), new NestObject('xxx', 20, new SomeObject('o', 'p', 'q')), new NestObject('@@@', 30, new SomeObject('x', 'y', 'z')) ]; $serializer = EncoderBuilder::AsObjectArray($meta)->build($values);
-
Encode as JSON.
$json = json_encode($serializer);