1. Go to this page and download the library: Download ergebnis/json-normalizer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ergebnis / json-normalizer example snippets
declare(strict_types=1);
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
$encoded = <<<'JSON'
{
"name": "Andreas Möller",
"url": "https://localheinz.com"
}
JSON;
$json = Json::fromString($encoded);
$callable = function (Json $json): Json {
$decoded = $json->decoded();
foreach (get_object_vars($decoded) as $name => $value) {
if ('https://localheinz.com' !== $value) {
continue;
}
$decoded->{$name} .= '/open-source/';
}
return Json::fromString(json_encode($decoded));
};
$normalizer = new Normalizer\CallableNormalizer($callable);
$normalized = $normalizer->normalize($json);
declare(strict_types=1);
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Printer;
$encoded = <<<'JSON'
{
"name": "Andreas Möller",
"url": "https://localheinz.com"
}
JSON;
$json = Json::fromString($encoded);
$indent = Normalizer\Format\Indent::fromString(' ');
$jsonEncodeOptions = Normalizer\Format\JsonEncodeOptions::fromInt(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$normalizer = new Normalizer\ChainNormalizer(
new Normalizer\JsonEncodeNormalizer($jsonEncodeOptions),
new Normalizer\IndentNormalizer(
$indent,
new Printer\Printer()
),
new Normalizer\WithFinalNewLineNormalizer()
);
$normalized = $normalizer->normalize($json);
declare(strict_types=1);
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Printer;
$encoded = <<<'JSON'
{
"name": "Andreas Möller",
"emoji": "🤓",
"url": "https://localheinz.com"
}
JSON;
$json = Json::fromString($encoded);
$format = Normalizer\Format\Format::create(
Normalizer\Format\Indent::fromString(' '),
Normalizer\Format\JsonEncodeOptions::fromInt(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
Normalizer\Format\NewLine::fromString("\r\n")
true
);
$normalizer = new Normalizer\FormatNormalizer(
new Printer\Printer(),
$format,
);
$normalized = $normalizer->normalize($json);
declare(strict_types=1);
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer;
use Ergebnis\Json\Printer;
$encoded = <<<'JSON'
{
"name": "Andreas Möller",
"url": "https://localheinz.com"
}
JSON;
$json = Json::fromString($encoded);
$indent = Normalizer\Format\Indent::fromString(' ');
$normalizer = new Normalizer\IndentNormalizer(
$indent,
new Printer\Printer()
);
$normalized = $normalizer->normalize($json);