From 4f2892bc19a3e4967d34a20cd1d3430392ffa15c Mon Sep 17 00:00:00 2001 From: jorgecc Date: Sun, 2 Mar 2025 18:59:24 -0300 Subject: [PATCH] 2.5 --- README.md | 2 ++ examples/example_decode.php | 2 +- examples/example_decode_unquoted.php | 21 ++++++++++++++++++--- examples/example_encode.php | 2 +- src/Services_JSON.php | 2 +- tests/FirstTest.php | 5 +++++ 6 files changed, 28 insertions(+), 6 deletions(-) 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

"; -echo "Decode as stdclass:
"; +echo "

Decode as stdclass:

"; echo "
";
 var_dump(Services_JSON::decode($json));
 echo "
"; -echo "Decode as array:
"; +echo "

Decode as array:

"; echo "
";
 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 "

Original (.@ and missing {} or [] in root):

$json

"; + +echo "

Decode as stdclass:

"; +echo "
";
+var_dump(Services_JSON::decode($json,Services_JSON::DECODE_FIX_ROOT));
+echo "
"; +echo "

Decode as array:

"; +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 {