simdjson_decode

(PECL simdjson >= 2.0.0)

simdjson_decodeDecodes a JSON string

Descrição

simdjson_decode(string $json, bool $associative = false, int $depth = 512): mixed

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; when false, 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 to 2147483647. 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

Exemplo #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)
}

Exemplo #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

?>

Exemplo #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

?>

Exemplo #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)

Exemplo #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

Nota:

The JSON spec is not JavaScript, but a subset of JavaScript.

Nota:

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

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top