> For the complete documentation index, see [llms.txt](https://docs.sonsofphp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sonsofphp.com/components/json.md). # JSON The JSON Component is a wrapper around php's `json_encode` and `json_decode`. ### Installation ```shell composer require sonsofphp/json ``` ### Usage #### Json ```php getEncoder(); $decoder = $json->getDecoder(); ``` #### JsonEncoder ```php withFlags(JSON_HEX_QUOT) ->withFlags(JSON_HEX_TAG | JSON_HEX_AMP); // Removing flags is just as easy $encoder = $encoder->withoutFlags(JSON_HEX_TAG); // Changing the depth is just as easy $encoder = $encoder->withDepth(256); // Once you are ready, you just encode the value to output json $json = $encoder->encode($value); // You can chain everything together as well $json = (new JsonEncoder()) ->withDepth(256) ->withFlags(JSON_HEX_QUOT) ->encode($value); ``` #### JsonDecoder ```php withFlags(...) ->withoutFlags(...); // Change the depth $decoder = $decoder->withDepth(256); // Easy to return array $decoder = $decoder->asArray(); // Decode the json $array = $decoder->decode($json); ``` ### Need Help? Check out [Sons of PHP's Organization Discussions](https://github.com/orgs/SonsOfPHP/discussions).