simdjson_decode
simdjson_decode
Decodes a JSON string
mixed **simdjson_decode** string $json bool $associative int $depth
Takes a JSON encoded string and converts it into a PHP value.
This uses a faster Simultaneous Instruction, Multiple Data implementation
than when it is supported by the computer architecture.
json_decode
json
The being decoded.
json``string
This function only works with UTF-8 encoded strings.
This function parses valid inputs which
can decode,
provided that they are less than 4 GiB long.
`json_decode`
associativeWhen true, JSON objects will be returned as
associative arrays; when false, JSON objects will be returned as objects.
depth
Maximum nesting depth of the structure being decoded.
The value must be greater than ,
and less than or equal to .
Callers should use reasonably small values,
because larger depths require more buffer space and will
increase the recursion depth, unlike the current implementation.
`0``2147483647``json_decode`
Returns the value encoded in in appropriate
PHP type. Values , and
are returned as true, false and null
respectively.
json``true``false``null
If is invalid, a is thrown as of PECL simdjson 2.1.0,
while previously, a was thrown.
json``SimdJsonException``RuntimeException
If is outside the allowed range,
a is thrown as of PECL simdjson 3.0.0,
while previously, an error of level was raised.
depth``SimdJsonValueError``E_WARNING
Voorbeeld: examples
<?php
$json = '{"a":1,"b":2,"c":3}';
var_dump(simdjson_decode($json));
var_dump(simdjson_decode($json, true));
?>
object(stdClass)#1 (3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
Voorbeeld: 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
?>
**Voorbeeld: common mistakes using **
<?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
?>
Voorbeeld: 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";
}
?>
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)
Voorbeeld: of large integers
<?php
$json = '{"number": 12345678901234567890}';
var_dump(simdjson_decode($json));
?>
object(stdClass)#1 (1) {
["number"]=>
float(1.2345678901235E+19)
}
Opmerking: > The JSON spec is not JavaScript, but a subset of JavaScript.
Opmerking: > In the event of a failure to decode, a is thrown and and can be used to determine the exact nature of the error.
SimdJsonException``SimdJsonException::getCode``SimdJsonException::getMessage
json_encode``json_decode