* @author Jordi Boggiano * * @deprecated Use json_encode or JsonFile::encode() with modern JSON_* flags to configure formatting - this class will be removed in 3.0 */ class JsonFormatter { /** * This code is based on the function found at: * http://recursive-design.com/blog/2008/03/11/format-json-with-php/ * * Originally licensed under MIT by Dave Perrett * * @param bool $unescapeUnicode Un escape unicode * @param bool $unescapeSlashes Un escape slashes */ public static function format(string $json, bool $unescapeUnicode, bool $unescapeSlashes): string { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $outOfQuotes = true; $buffer = ''; $noescape = true; for ($i = 0; $i < $strLen; $i++) { // Grab the next character in the string $char = substr($json, $i, 1); // Are we inside a quoted string? if ('"' === $char && $noescape) { $outOfQuotes = !$outOfQuotes; } if (!$outOfQuotes) { $buffer .= $char; $noescape = '\\' === $char ? !$noescape : true; continue; } if ('' !== $buffer) { if ($unescapeSlashes) { $buffer = str_replace('\\/', '/', $buffer); } if ($unescapeUnicode && function_exists('mb_convert_encoding')) { // https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha $buffer = Preg::replaceCallback('/(\\\\+)u([0-9a-f]{4})/i', static function ($match) { assert(is_string($match[1])); assert(is_string($match[2])); $l = strlen($match[1]); if ($l % 2) { $code = hexdec($match[2]); // 0xD800..0xDFFF denotes UTF-16 surrogate pair which won't be unescaped // see https://github.com/composer/composer/issues/7510 if (0xD800 <= $code && 0xDFFF >= $code) { return $match[0]; } return str_repeat('\\', $l - 1) . mb_convert_encoding( pack('H*', $match[2]), 'UTF-8', 'UCS-2BE' ); } return $match[0]; }, $buffer); } $result .= $buffer.$char; $buffer = ''; continue; } if (':' === $char) { // Add a space after the : character $char .= ' '; } elseif ('}' === $char || ']' === $char) { $pos--; $prevChar = substr($json, $i - 1, 1); if ('{' !== $prevChar && '[' !== $prevChar) { // If this character is the end of an element, // output a new line and indent the next line $result .= $newLine; $result .= str_repeat($indentStr, $pos); } else { // Collapse empty {} and [] $result = rtrim($result); } } $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line if (',' === $char || '{' === $char || '[' === $char) { $result .= $newLine; if ('{' === $char || '[' === $char) { $pos++; } $result .= str_repeat($indentStr, $pos); } } return $result; } } __halt_compiler();----SIGNATURE:----1sk40HbebsJKLvIim1L/x5AYpeSRSgq2zC34ndpiNsNrY5LKpRSVySxe0P/WGvM4XqKzVJxTTVoNTTdnP1Ic2UlRWuPNC+D2qk6M+K5bNHGtgTq737juRu3vCrr8TjE+0vKaI9Nw087HGZWD5eLnCcwbxpPOFBjEqcrJOw7GqBS4cbWZsXOF8SyJJrJDs+9DZOftCvqtvqh99TVbiiX5WNnYHaKrBkK151CiEGsds093mCsRudNHXVK//o7ThhMLd9SHW0GzDNzli0CTAEmyMjXDSIh4tvoIqYY0UDNwV/arOMyonbuFomfQHJwYjvsIvDB1H5X39kZZ23SnjDw7nqraO+iF3EDC7hJ2MF/7Kf+v3YE8bCaSHKJP3YouS6ck55CDdJw1ksm2DiEo07yRbxAB9HJHWC3Vy70t3cqVJ8gsJmxqxFTbEZBI8luKwDypUdnZ+tAyiEy4ZfjG3ryGuTl98SiMCBEWkOqzisrFJYKXlBqfg7L3l7ikY5I4eQAp6SWpJZ/g0lwKXcsqYa0rV2W0QPmIqdtZFbpxv7KGWhE1982DMFA33IQQItI/sG6Aa/04ixD+hl30MUJ7M83uIpa2MLn4B/kqBNieIGIo37VaxPKg/YTbSIrpDOIKPRmo7K+MsNpupVUvHCFKBEHqSnkSUboiGzCnEyQS/fTvqyg=----ATTACHMENT:----Nzc2ODg4MzUxNjU1OTQ3MiA1MDQzOTcxNTE3MDY5MjM3IDQ0NTU4NjIzNTkwOTE3MDI=