-
Notifications
You must be signed in to change notification settings - Fork 0
Managing Options
When you create a new Json
instance, any options you don't provide will use a set of carefully chosen defaults. By default, when decoding:
- objects are decoded as associative arrays
- big integers are decoded as strings (rather than converting them to float).
When encoding:
- big integers are encoded as strings
- "zero" fractions on floats are preserved (rather then encoding them as integers)
- slashes and unicode characters are not escaped
- encoding objects will error unless they are
stdClass
or implementJsonSerializable
Both encoding and decoding will set JSON_THROW_ON_ERROR
which causes the native JsonException
to be thrown on error; this cannot be overridden.
Several "preset" bitmasks are provided for common/useful encoding/decoding flags. These are passed under the Json::DECODE_FLAGS
|ENCODE_FLAGS
option, as appropriate.
- int
Json::ENCODE_ASCII
: Default encoding options + \u encoding of non-ascii characters - int
Json::ENCODE_HEX
: AllJSON_HEX_*
options - int
Json::ENCODE_HTML
: Default encoding options + escaped slashes - int
Json::ENCODE_PRETTY
: Default encoding options + pretty printing
Sets encoding/decoding options.
Use the appropriate Json
const for the option keys. Any options omitted will be set to their default values (you can pass an empty array to set/restore all default options).
Normally, there is no need to call this method directly - pass $options
to the Json constructor instead.
- array
$options
Exncode/decode options:- bool
${Json::ASSOC}
: @see https://php.net/json_decode $assoc - int
${Json::DEPTH}
: @see https://php.net/json_decode $depth - int
${Json::DECODE_FLAGS}
: @see https://php.net/json_decode $options - int
${Json::ENCODE_FLAGS}
: @see https://php.net/json_encode $options
- bool
Throws an InvalidArgumentException
if any of the provided options are invalid. See Handling Errors for more information.
The Json instance, for method chaining.