PHP.nl

simdjson_is_valid

simdjson_is_valid

Check if a JSON string is valid

bool **simdjson_is_valid** string $json int $depth

Takes a JSON encoded string and returns true if it is valid.

json The being validated. json``string

This function only works with UTF-8 encoded strings.

   This function validates inputs which
    can decode,
   provided that they are less than 4 GiB long.
  `json_decode`

depth Maximum nesting depth of the structure being validated. 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 true if is a valid JSON string, false otherwise. json

If is longer than 4 GiB, 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}';
$invalidJson = '{"a":1,"b":2,"c":';

var_dump(simdjson_is_valid($json));
var_dump(simdjson_is_valid($invalidJson));

?>
bool(true)
bool(false)

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_is_valid($json, 4));
var_dump(simdjson_is_valid($json, 3));
?>
bool(true)
bool(false)

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