diff --git a/README.md b/README.md index ac15fab..6563f22 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,15 @@ var_dump(Services_JSON::decode($json)); // as stdclass var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array ``` -It also works (with the flag Services_JSON::DECODE_FIX_ROOT) where the string misses [] and {} at the start of the code +#### Json unwrapped. + +With the flag **Services_JSON::DECODE_FIX_ROOT**, it also works when the origin text misses [] and {} at the start of the code. +Note, this feature is not foolproof, for example "[1,2,3],[4,5,6]" will not wrap as "[[1,2,3],[4,5,6]]" + +``` +{"a":222,"b:"ccc"} # normal json +"a":222,"b:"ccc" # json without the root parenthesis. +``` ```php Services_JSON::decode('1,2,3',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT); // returns [1,2,3] @@ -79,6 +87,18 @@ Services_JSON::decode('"k1":"v1", k2:2',Services_JSON::GET_ARRAY | Services_JSON > Note: DECODE_FIX_ROOT flag detects if the near character is ":" or ",". If the closest character is ":", then it returns > an object, otherwise it returns a list. If there is none, then it returns a list. +#### Json with unquoted values + +With the flag **Services_JSON::DECODE_NO_QUOTE** it also works when the string values are not quoted. + +>Note: invisible characters at the beginner and at the end (tabs, line carriages) will be trimmed. + +```php + +Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}' + , Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE) // ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa'] +``` + ### Encode @@ -94,10 +114,12 @@ var_dump(Services_JSON::encode($obj)); // encode an object ## Changelog - +* 2.3.2 + * Added flag to decode DECODE_NO_QUOTE * 2.3.1 * deleted unused code * fixed comments. + * 25% of the code has been refactored. * 2.3 * Fixed a typo with a comment. * added phpunit. The entire code is tested but special codification. diff --git a/src/Services_JSON.php b/src/Services_JSON.php index 01f4eb8..ddd9c82 100644 --- a/src/Services_JSON.php +++ b/src/Services_JSON.php @@ -91,6 +91,7 @@ class Services_JSON public const USE_TO_JSON = 64; /** @var int If the fix value is broken, then it adds [] or {} automatically */ public const DECODE_FIX_ROOT = 128; + public const DECODE_NO_QUOTE = 256; protected static $use = 0; // private - cache the mbstring lookup results.. @@ -709,6 +710,9 @@ protected static function decode2($str) return $obj; } } + if(self::$use & self::DECODE_NO_QUOTE) { + return $str; + } } return null; } diff --git a/tests/FirstTest.php b/tests/FirstTest.php index 2527030..b5f0836 100644 --- a/tests/FirstTest.php +++ b/tests/FirstTest.php @@ -32,6 +32,14 @@ public function testDecode(): void ,Services_JSON::decode('"k1":"v1", k2:2', Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) ); + // + $this->assertEquals( + [ 'button1' =>["aaaa","bbb","cccc"], + 'button2' =>["aaaa","bbb","cccc"], + 'button3' =>["aaaa","bbb","cccc"],] + ,Services_JSON::decode('{"button1":["aaaa","bbb","cccc"],"button2":["aaaa","bbb","cccc"],"button3":["aaaa","bbb","cccc"]}', + Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) + ); $this->assertEquals( [ 1,2,3] ,Services_JSON::decode('1,2,3', @@ -48,6 +56,16 @@ public function testDecode(): void Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) ); } + public function testsimple(): void + { + $this->assertEquals( + ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa'] + ,Services_JSON::decode('a:aaa,b:bbbb,c: aaaaa', + Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT | Services_JSON::DECODE_NO_QUOTE)); + $this->assertEquals( + ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa'] + ,Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}', Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE)); + } public function testEncode(): void { $this->assertEquals(