mirror of
https://github.com/EFTEC/Services_JSON.git
synced 2026-08-26 15:06:58 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43246435ec |
@@ -69,7 +69,15 @@ var_dump(Services_JSON::decode($json)); // as stdclass
|
|||||||
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
|
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
|
```php
|
||||||
Services_JSON::decode('1,2,3',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT); // returns [1,2,3]
|
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
|
> 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.
|
> 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
|
### Encode
|
||||||
@@ -94,10 +114,12 @@ var_dump(Services_JSON::encode($obj)); // encode an object
|
|||||||
|
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
* 2.3.2
|
||||||
|
* Added flag to decode DECODE_NO_QUOTE
|
||||||
* 2.3.1
|
* 2.3.1
|
||||||
* deleted unused code
|
* deleted unused code
|
||||||
* fixed comments.
|
* fixed comments.
|
||||||
|
* 25% of the code has been refactored.
|
||||||
* 2.3
|
* 2.3
|
||||||
* Fixed a typo with a comment.
|
* Fixed a typo with a comment.
|
||||||
* added phpunit. The entire code is tested but special codification.
|
* added phpunit. The entire code is tested but special codification.
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ class Services_JSON
|
|||||||
public const USE_TO_JSON = 64;
|
public const USE_TO_JSON = 64;
|
||||||
/** @var int If the fix value is broken, then it adds [] or {} automatically */
|
/** @var int If the fix value is broken, then it adds [] or {} automatically */
|
||||||
public const DECODE_FIX_ROOT = 128;
|
public const DECODE_FIX_ROOT = 128;
|
||||||
|
public const DECODE_NO_QUOTE = 256;
|
||||||
|
|
||||||
protected static $use = 0;
|
protected static $use = 0;
|
||||||
// private - cache the mbstring lookup results..
|
// private - cache the mbstring lookup results..
|
||||||
@@ -709,6 +710,9 @@ class Services_JSON
|
|||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(self::$use & self::DECODE_NO_QUOTE) {
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,14 @@ final class FirstTest extends TestCase
|
|||||||
,Services_JSON::decode('"k1":"v1", k2:2',
|
,Services_JSON::decode('"k1":"v1", k2:2',
|
||||||
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT)
|
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(
|
$this->assertEquals(
|
||||||
[ 1,2,3]
|
[ 1,2,3]
|
||||||
,Services_JSON::decode('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)
|
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
|
public function testEncode(): void
|
||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
|||||||
Reference in New Issue
Block a user