Skip to content

Commit

Permalink
2.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Nov 10, 2022
1 parent 269be53 commit 4324643
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/Services_JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand Down Expand Up @@ -709,6 +710,9 @@ protected static function decode2($str)
return $obj;
}
}
if(self::$use & self::DECODE_NO_QUOTE) {
return $str;
}
}
return null;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/FirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down

0 comments on commit 4324643

Please sign in to comment.