json
Modulejson_encode, json_decode, json_validate, json_last_error and
json_last_error_msg, as PHP 8.5's ext/json implements them.
Every function here is checked against the real PHP function by
bin/compat fuzz php-std (operations json.*).
| PHP | Rust |
|---|---|
json_encode($v, $flags, $depth) | encode (Value) |
json_decode($s, $assoc, $depth, $flags) | decode (Value), decode_value (serde_json::Value, $assoc = true) |
json_validate($s, $depth, $flags) | validate |
json_last_error(), json_last_error_msg() | ErrorCode::code, ErrorCode::message of the returned error |
JSON_* constants | Flags |
JsonException / ValueError | Error (Error::php_class) |
There is no global "last error": each call returns its error. PHP's
json_last_error() after a call without JSON_THROW_ON_ERROR is the
code of the returned error (0 on success).
Faithful details: floats print with serialize_precision = -1
(crate::number::serialize); strings are validated as UTF-8 exactly
like PHP (php_next_utf8_char when encoding, the scanner's byte ranges
when decoding, so JSON_INVALID_UTF8_SUBSTITUTE replaces the same byte
runs); errors follow PHP's precedence (the first error in input order when
decoding, the last one recorded when encoding); the decoder reports the
same error as PHP's bison parser when nesting exhausts its 10000-entry
stack, and never recurses.
Objects other than stdClass are [Value::Ext]: they encode as an
object of their Extension::to_array (what json_encode does for an
ArrayObject, such as Utopia\Database\Document). Not modelled:
JsonSerializable, enums, recursion (a Value cannot contain itself)
and the engine's stack-size limit when encoding values nested thousands
of levels deep.
Contents
use php_std::json::Flags;pub struct Flags(u32)JSON_* option bits for json_encode, json_decode and json_validate.
PHP passes options as int; Flags::from_php keeps the low 32 bits
like the engine's (int) cast. Unknown bits are ignored.
BitOr, BitOrAssign, BitAnd.use php_std::json::Error;pub enum ErrorWhy a JSON function failed.
Display.use php_std::json::ErrorCode;pub enum ErrorCodeJSON_ERROR_*: what json_last_error() returns.
use php_std::json::decode;pub fn decode(json: &[u8], assoc: Option<bool>, depth: i64, flags: Flags) -> Result<Value, Error>json_decode($json, $assoc, $depth, $flags).
assoc overrides JSON_OBJECT_AS_ARRAY in flags when it is not
None, as in PHP. Objects decode to Value::Object (stdClass) or, in
array mode, to Value::Array with PHP's key rules ("7" is key 7).
Errors: Error::Json with PHP's code (empty input is a syntax error),
Error::Value when depth is not in 1..=i32::MAX.
use php_std::json::decode_ext;pub fn decode_ext<X: Extension>(json: &[u8], assoc: Option<bool>, depth: i64, flags: Flags) -> Result<Value<X>, Error>use php_std::json::decode_value;pub fn decode_value(json: &[u8], depth: i64, flags: Flags) -> Result<Json, Error>json_decode($json, true, $depth, $flags) into the request model
(serde_json::Value): PHP arrays that are lists become JSON arrays (so
{"0":"a"} decodes to ["a"], as PHP's [0 => 'a'] is a list), other
arrays objects; empty objects become [].
Fails with Error::Unrepresentable where PHP decodes a number to an
infinite float (1e999), which serde_json::Value cannot hold.
use php_std::json::encode;pub fn encode<X: Extension>(value: &Value<X>, flags: Flags, depth: i64) -> Result<String, Error>json_encode($value, $flags, $depth).
Returns the JSON text, or the error PHP reports: Err(Error::Json) where
PHP returns false (or throws with JSON_THROW_ON_ERROR), and
Err(Error::Partial) carrying the output PHP returns when
JSON_PARTIAL_OUTPUT_ON_ERROR is set. depth is cast to a C int like
PHP does; it is not validated (a depth below 1 fails on any array).
use php_std::json::next_utf8_char;pub fn next_utf8_char(s: &[u8]) -> Result<(u32, usize), usize>php_next_utf8_char: the code point at the start of s and its length,
or the number of bytes PHP skips as one invalid sequence (UTR #36
strategy 2: an invalid sequence never swallows a byte that could start a
valid one).
use php_std::json::validate;pub fn validate(json: &[u8], depth: i64, flags: Flags) -> Result<(), Error>json_validate($json, $depth, $flags): Ok(()) when PHP returns true.
Only JSON_INVALID_UTF8_IGNORE is accepted in flags (PHP throws
ValueError for anything else). Property names starting with NUL are
valid here (they only fail when decoding to stdClass).
pub const DEFAULT_DEPTH: i64 = 512PHP_JSON_PARSER_DEFAULT_DEPTH: the default $depth of every function.