This commit is contained in:
jorgecc
2025-03-02 18:59:24 -03:00
parent 0753c8c54a
commit 4f2892bc19
6 changed files with 28 additions and 6 deletions
+2
View File
@@ -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)
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection ForgottenDebugOutputInspection */
use eftec\ServicesJson\Services_JSON;
include '../vendor/autoload.php';
+18 -3
View File
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection ForgottenDebugOutputInspection */
use eftec\ServicesJson\Services_JSON;
@@ -9,12 +9,27 @@ include '../vendor/autoload.php';
$json='{hello:{a:2,b:3},world:[1,2,3,"aaa","bbbb"]}';
echo "<h1>Original:</h1><pre>$json</pre><br>";
echo "Decode as stdclass:<br>";
echo "<h1>Decode as stdclass:</h1>";
echo "<pre>";
var_dump(Services_JSON::decode($json));
echo "</pre>";
echo "Decode as array:<br>";
echo "<h1>Decode as array:</h1>";
echo "<pre>";
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY));
echo "</pre>";
$json='hello.world:{a:2,b:3},hello@world:[1,2,3,"aaa","bbbb"]';
echo "<h1>Original (.@ and missing {} or [] in root):</h1><pre>$json</pre><br>";
echo "<h1>Decode as stdclass:</h1>";
echo "<pre>";
var_dump(Services_JSON::decode($json,Services_JSON::DECODE_FIX_ROOT));
echo "</pre>";
echo "<h1>Decode as array:</h1>";
echo "<pre>";
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY|Services_JSON::DECODE_FIX_ROOT));
echo "</pre>";
+1 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection ForgottenDebugOutputInspection */
use eftec\ServicesJson\Services_JSON;
+1 -1
View File
@@ -663,7 +663,7 @@ class Services_JSON
} else {
$obj->$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"));
+5
View File
@@ -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
{