simdjson_decode
(PECL simdjson >= 2.0.0)
simdjson_decode — Decodes a JSON string
Descrição
Takes a JSON encoded string and converts it into a PHP value. This uses a faster Simultaneous Instruction, Multiple Data implementation than json_decode() when it is supported by the computer architecture.
Parâmetros
json
-
The
json
string being decoded.This function only works with UTF-8 encoded strings.
This function parses valid inputs which json_decode() can decode, provided that they are less than 4 GiB long.
associative
-
When
true
, JSON objects will be returned as associative arrays; whenfalse
, JSON objects will be returned as objects. depth
-
Maximum nesting depth of the structure being decoded. The value must be greater than
0
, and less than or equal to2147483647
. Callers should use reasonably small values, because larger depths require more buffer space and will increase the recursion depth, unlike the current json_decode() implementation.
Valor Retornado
Returns the value encoded in json
in appropriate
PHP type. Values true
, false
and
null
are returned as true
, false
and null
respectively.
Erros/Exceções
If json
is invalid, a SimdJsonException is thrown as of PECL simdjson 2.1.0,
while previously, a RuntimeException was thrown.
If depth
is outside the allowed range,
a SimdJsonValueError is thrown as of PECL simdjson 3.0.0,
while previously, an error of level E_WARNING
was raised.
Exemplos
Example #1 simdjson_decode() examples
<?php
$json = '{"a":1,"b":2,"c":3}';
var_dump(simdjson_decode($json));
var_dump(simdjson_decode($json, true));
?>
O exemplo acima produzirá:
object(stdClass)#1 (3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) } array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) }
Example #2 Accessing invalid object properties
Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
<?php
$json = '{"foo-bar": 12345}';
$obj = simdjson_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
Example #3 common mistakes using simdjson_decode()
<?php
// the following strings are valid JavaScript but not valid JSON
// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
simdjson_decode($bad_json); // Throws SimdJsonException
// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
simdjson_decode($bad_json); // Throws SimdJsonException
// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
simdjson_decode($bad_json); // Throws SimdJsonException
?>
Example #4 depth
errors
<?php
// Encode some data with a maximum depth of 4
// (array -> array -> array -> string)
$json = json_encode(
[
1 => [
'English' => [
'One',
'January'
],
'French' => [
'Une',
'Janvier'
]
]
]
);
// Show the errors for different depths.
var_dump(simdjson_decode($json, true, 4));
try {
var_dump(simdjson_decode($json, true, 3));
} catch (SimdJsonException $e) {
echo "Caught: ", $e->getMessage(), "\n";
}
?>
O exemplo acima produzirá:
array(1) { [1]=> array(2) { ["English"]=> array(2) { [0]=> string(3) "One" [1]=> string(7) "January" } ["French"]=> array(2) { [0]=> string(3) "Une" [1]=> string(7) "Janvier" } } } Caught: The JSON document was too deep (too many nested objects and arrays)
Example #5 simdjson_decode() of large integers
<?php
$json = '{"number": 12345678901234567890}';
var_dump(simdjson_decode($json));
?>
O exemplo acima produzirá:
object(stdClass)#1 (1) { ["number"]=> float(1.2345678901235E+19) }
Notas
Note:
The JSON spec is not JavaScript, but a subset of JavaScript.
Note:
In the event of a failure to decode, a SimdJsonException is thrown and SimdJsonException::getCode() and SimdJsonException::getMessage() can be used to determine the exact nature of the error.
Veja Também
- json_encode() - Retorna a representação JSON de um valor
- json_decode() - Decodifica uma string JSON