1 Commits

Author SHA1 Message Date
jorgecc 43246435ec 2.3.2 2022-11-09 21:51:27 -03:00
3 changed files with 46 additions and 2 deletions
+24 -2
View File
@@ -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.
+4
View File
@@ -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 @@ class Services_JSON
return $obj;
}
}
if(self::$use & self::DECODE_NO_QUOTE) {
return $str;
}
}
return null;
}
+18
View File
@@ -32,6 +32,14 @@ final class FirstTest extends TestCase
,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 @@ final class FirstTest extends TestCase
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(