Skip to content

Commit

Permalink
json.texy translated in english (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasneubauer authored and dg committed Dec 27, 2021
1 parent 5d366de commit c94f754
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions doc/en/json.texy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Working with JSON - Nette\Utils\Json
************************************

.[perex]
[Nette\Utils\Json |api:] is a static class with useful functions you can use to work with JSON. It sanitizes errors in different PHP versions and throws exceptions on errors.

The following examples use these aliases:

/--php
use Nette\Utils\Json;
use Nette\Utils\JsonException;
\--


encode($value, $options = 0)
============================

Returns `$value` encoded into JSON. Accepts argument `Json::PRETTY` which formats JSON for better readability.

/--php
Json::encode($value); // Returns $value encoded in JSON
Json::encode($value, Json::PRETTY); // Returns formatted $value encoded in JSON
\--

Method `encode()` throws `JsonException` on error.

/--php
try {
Json::encode($value);
} catch (JsonException $e) {
// Exception handling
}
\--


decode($json, $options = 0)
===========================

Converts given JSON either in object or in array. Accepts argument `Json::FORCE_ARRAY` which forces an array instead of an object as the return value.

/--php
Json::decode('{"variable": true}'); // Returns an object of stdClass with attribute $variable
Json::decode('{"variable": true}', Json::FORCE_ARRAY); // Returns an array with key "variable" and value true
\--

Method `decode()` throws `JsonException` on error.

/--php
try {
Json::decode($value);
} catch (JsonException $e) {
// Exception handling
}
\--

0 comments on commit c94f754

Please sign in to comment.