Skip to content

Managing Options

Adrian edited this page Apr 1, 2021 · 1 revision

Defaults

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 implement JsonSerializable

Both encoding and decoding will set JSON_THROW_ON_ERROR which causes the native JsonException to be thrown on error; this cannot be overridden.

Presets

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: All JSON_HEX_* options
  • int Json::ENCODE_HTML: Default encoding options + escaped slashes
  • int Json::ENCODE_PRETTY: Default encoding options + pretty printing

setOptions( array $options ) : Json

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.

parameters

errors/exceptions

Throws an InvalidArgumentException if any of the provided options are invalid. See Handling Errors for more information.

return value

The Json instance, for method chaining.