Skip to content

Commit 4324643

Browse files
2.3.2
1 parent 269be53 commit 4324643

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ var_dump(Services_JSON::decode($json)); // as stdclass
6969
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
7070
```
7171

72-
It also works (with the flag Services_JSON::DECODE_FIX_ROOT) where the string misses [] and {} at the start of the code
72+
#### Json unwrapped.
73+
74+
With the flag **Services_JSON::DECODE_FIX_ROOT**, it also works when the origin text misses [] and {} at the start of the code.
75+
Note, this feature is not foolproof, for example "[1,2,3],[4,5,6]" will not wrap as "[[1,2,3],[4,5,6]]"
76+
77+
```
78+
{"a":222,"b:"ccc"} # normal json
79+
"a":222,"b:"ccc" # json without the root parenthesis.
80+
```
7381

7482
```php
7583
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
7987
> Note: DECODE_FIX_ROOT flag detects if the near character is ":" or ",". If the closest character is ":", then it returns
8088
> an object, otherwise it returns a list. If there is none, then it returns a list.
8189
90+
#### Json with unquoted values
91+
92+
With the flag **Services_JSON::DECODE_NO_QUOTE** it also works when the string values are not quoted.
93+
94+
>Note: invisible characters at the beginner and at the end (tabs, line carriages) will be trimmed.
95+
96+
```php
97+
98+
Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}'
99+
, Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE) // ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa']
100+
```
101+
82102

83103

84104
### Encode
@@ -94,10 +114,12 @@ var_dump(Services_JSON::encode($obj)); // encode an object
94114

95115

96116
## Changelog
97-
117+
* 2.3.2
118+
* Added flag to decode DECODE_NO_QUOTE
98119
* 2.3.1
99120
* deleted unused code
100121
* fixed comments.
122+
* 25% of the code has been refactored.
101123
* 2.3
102124
* Fixed a typo with a comment.
103125
* added phpunit. The entire code is tested but special codification.

src/Services_JSON.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class Services_JSON
9191
public const USE_TO_JSON = 64;
9292
/** @var int If the fix value is broken, then it adds [] or {} automatically */
9393
public const DECODE_FIX_ROOT = 128;
94+
public const DECODE_NO_QUOTE = 256;
9495

9596
protected static $use = 0;
9697
// private - cache the mbstring lookup results..
@@ -709,6 +710,9 @@ protected static function decode2($str)
709710
return $obj;
710711
}
711712
}
713+
if(self::$use & self::DECODE_NO_QUOTE) {
714+
return $str;
715+
}
712716
}
713717
return null;
714718
}

tests/FirstTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public function testDecode(): void
3232
,Services_JSON::decode('"k1":"v1", k2:2',
3333
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT)
3434
);
35+
//
36+
$this->assertEquals(
37+
[ 'button1' =>["aaaa","bbb","cccc"],
38+
'button2' =>["aaaa","bbb","cccc"],
39+
'button3' =>["aaaa","bbb","cccc"],]
40+
,Services_JSON::decode('{"button1":["aaaa","bbb","cccc"],"button2":["aaaa","bbb","cccc"],"button3":["aaaa","bbb","cccc"]}',
41+
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT)
42+
);
3543
$this->assertEquals(
3644
[ 1,2,3]
3745
,Services_JSON::decode('1,2,3',
@@ -48,6 +56,16 @@ public function testDecode(): void
4856
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT)
4957
);
5058
}
59+
public function testsimple(): void
60+
{
61+
$this->assertEquals(
62+
['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa']
63+
,Services_JSON::decode('a:aaa,b:bbbb,c: aaaaa',
64+
Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT | Services_JSON::DECODE_NO_QUOTE));
65+
$this->assertEquals(
66+
['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa']
67+
,Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}', Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE));
68+
}
5169
public function testEncode(): void
5270
{
5371
$this->assertEquals(

0 commit comments

Comments
 (0)