diff --git a/README.md b/README.md index 2256184..8eb2258 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ var_dump(Services_JSON::encode($obj)); // encode an object ## Changelog +* 2.5 + * now allows "@" and "." for key values. * 2.4 * update requirements to php 7.4 * added more type hinting (type validation) diff --git a/examples/example_decode.php b/examples/example_decode.php index 32913b3..7636179 100644 --- a/examples/example_decode.php +++ b/examples/example_decode.php @@ -1,4 +1,4 @@ -Original:
$json
"; var_dump(Services_JSON::decode($json)); echo ""; -echo "Decode as array:
"; var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); echo ""; + +$json='hello.world:{a:2,b:3},hello@world:[1,2,3,"aaa","bbbb"]'; + +echo "
$json
"; +var_dump(Services_JSON::decode($json,Services_JSON::DECODE_FIX_ROOT)); +echo ""; +echo "
"; +var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY|Services_JSON::DECODE_FIX_ROOT)); +echo ""; + diff --git a/examples/example_encode.php b/examples/example_encode.php index 44c0197..6fb6314 100644 --- a/examples/example_encode.php +++ b/examples/example_encode.php @@ -1,4 +1,4 @@ -$key = $val; } - } /** @noinspection NotOptimalRegularExpressionsInspection */ elseif (preg_match('/^\s*(\w+)\s*:/Ui', $slice, $parts)) { + } /** @noinspection NotOptimalRegularExpressionsInspection */ elseif (preg_match('/^\s*([a-z.@A-Z0-9_]+)\s*:/Ui', $slice, $parts)) { // name:value pair, where name is unquoted $key = $parts[1]; $val = self::decode2(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B")); diff --git a/tests/FirstTest.php b/tests/FirstTest.php index b5f0836..ba4d151 100644 --- a/tests/FirstTest.php +++ b/tests/FirstTest.php @@ -55,6 +55,11 @@ final class FirstTest extends TestCase ,Services_JSON::decode('a:1', Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) ); + $this->assertEquals( + ['a.b@c'=>1] + ,Services_JSON::decode('a.b@c:1', + Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) + ); } public function testsimple(): void {