Skip to content

Commit 4f2892b

Browse files
2.5
1 parent 0753c8c commit 4f2892b

6 files changed

Lines changed: 28 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ var_dump(Services_JSON::encode($obj)); // encode an object
116116

117117

118118
## Changelog
119+
* 2.5
120+
* now allows "@" and "." for key values.
119121
* 2.4
120122
* update requirements to php 7.4
121123
* added more type hinting (type validation)

examples/example_decode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection ForgottenDebugOutputInspection */
22

33
use eftec\ServicesJson\Services_JSON;
44
include '../vendor/autoload.php';
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection ForgottenDebugOutputInspection */
22

33
use eftec\ServicesJson\Services_JSON;
44

@@ -9,12 +9,27 @@
99

1010
$json='{hello:{a:2,b:3},world:[1,2,3,"aaa","bbbb"]}';
1111

12+
echo "<h1>Original:</h1><pre>$json</pre><br>";
1213

13-
echo "Decode as stdclass:<br>";
14+
echo "<h1>Decode as stdclass:</h1>";
1415
echo "<pre>";
1516
var_dump(Services_JSON::decode($json));
1617
echo "</pre>";
17-
echo "Decode as array:<br>";
18+
echo "<h1>Decode as array:</h1>";
1819
echo "<pre>";
1920
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY));
2021
echo "</pre>";
22+
23+
$json='hello.world:{a:2,b:3},hello@world:[1,2,3,"aaa","bbbb"]';
24+
25+
echo "<h1>Original (.@ and missing {} or [] in root):</h1><pre>$json</pre><br>";
26+
27+
echo "<h1>Decode as stdclass:</h1>";
28+
echo "<pre>";
29+
var_dump(Services_JSON::decode($json,Services_JSON::DECODE_FIX_ROOT));
30+
echo "</pre>";
31+
echo "<h1>Decode as array:</h1>";
32+
echo "<pre>";
33+
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY|Services_JSON::DECODE_FIX_ROOT));
34+
echo "</pre>";
35+

examples/example_encode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection ForgottenDebugOutputInspection */
22

33
use eftec\ServicesJson\Services_JSON;
44

src/Services_JSON.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ protected static function decode2($str)
663663
} else {
664664
$obj->$key = $val;
665665
}
666-
} /** @noinspection NotOptimalRegularExpressionsInspection */ elseif (preg_match('/^\s*(\w+)\s*:/Ui', $slice, $parts)) {
666+
} /** @noinspection NotOptimalRegularExpressionsInspection */ elseif (preg_match('/^\s*([a-z.@A-Z0-9_]+)\s*:/Ui', $slice, $parts)) {
667667
// name:value pair, where name is unquoted
668668
$key = $parts[1];
669669
$val = self::decode2(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));

tests/FirstTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function testDecode(): void
5555
,Services_JSON::decode('a:1',
5656
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT)
5757
);
58+
$this->assertEquals(
59+
['a.b@c'=>1]
60+
,Services_JSON::decode('a.b@c:1',
61+
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT)
62+
);
5863
}
5964
public function testsimple(): void
6065
{

0 commit comments

Comments
 (0)