Skip to content

Commit

Permalink
2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Nov 7, 2022
1 parent 470badb commit a8dcadd
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 84 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,31 @@ See the folder examples for further examples

### Decode

Decode transform (de-codified) a JSON string into a stdclass or an associative array
Decode a JSON string into a stdclass or an associative array

```php
$json='{"hello":{"a":2,"b":3},"world":[1,2,3,"aaa"]}';
var_dump(Services_JSON::decode($json)); // as stdclass
var_dump(Services_JSON::decode($json,Services_JSON::SERVICES_JSON_AS_ARRAY)); // as array
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
```
It also works with unquoted keys

```php

$json='{hello:{a:2,b:3},world:[1,2,3,"aaa","bbbb"]}'; // the keys are unquoted.
var_dump(Services_JSON::decode($json)); // as stdclass
var_dump(Services_JSON::decode($json,Services_JSON::SERVICES_JSON_AS_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

```php
Services_JSON::decode('1,2,3',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT); // returns [1,2,3]
Services_JSON::decode('"k1":"v1", k2:2',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) // returns [ 'k1' => 'v1','k2'=>2]
```



### Encode

Encode transform a value (array, object, primitive value, etc.) into a json expression (a string)
Expand All @@ -83,7 +92,10 @@ var_dump(Services_JSON::encode($obj)); // encode an object

## Changelog

* 2.0
* 2.3
* Fixed a typo with a comment.
* added phpunit. The entire code is tested but special codification.
* 2.2
* Now the library is static, so you can call the methods without creating an instance.
* If you want to work with the non-static library, then install 1.1
* 1.1
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
},
"suggest": {
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
2 changes: 1 addition & 1 deletion examples/example_decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
echo "</pre>";
echo "Decode as array:<br>";
echo "<pre>";
var_dump(Services_JSON::decode($json, Services_JSON::SERVICES_JSON_AS_ARRAY));
var_dump(Services_JSON::decode($json, Services_JSON::GET_ARRAY));
echo "</pre>";
2 changes: 1 addition & 1 deletion examples/example_decode_unquoted.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
echo "</pre>";
echo "Decode as array:<br>";
echo "<pre>";
var_dump(Services_JSON::decode($json,Services_JSON::SERVICES_JSON_AS_ARRAY));
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY));
echo "</pre>";
Loading

0 comments on commit a8dcadd

Please sign in to comment.