# JSON

The JSON Component is a wrapper around php's `json_encode` and `json_decode`.

### Installation

```shell
composer require sonsofphp/json
```

### Usage

#### Json

```php
<?php
use SonsOfPHP\Component\Json\Json;

// You use this as a drop-in replacement for json_encode and json_decode
$json = Json::encode($value);
$object = Json::decode($json);
$array = Json::decode($json, true);

// It comes with a JsonEncoder and JsonDecoder, see below for usage
$json = new Json();
$encoder = $json->getEncoder();
$decoder = $json->getDecoder();
```

#### JsonEncoder

```php
<?php
use SonsOfPHP\Component\Json\JsonEncoder;

$encoder = new JsonEncoder();

// You can add various flags using the various JSON constants PHP provides. You
// can enter them one at a time or combine them
$encoder = $encoder
    ->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
<?php
use SonsOfPHP\Component\Json\JsonDecoder;

$decoder = new JsonDecoder();

// Add & Remove flags with the constants provided by PHP
$decoder = $decoder
    ->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).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sonsofphp.com/components/json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
