exports dart.convert
Utilities for encoding and decoding JSON (JavaScript Object Notation) data.
Parses json
and build the corresponding parsed JSON value.
Parsed JSON values are of the types num
, String
, bool
, Null
, List
s of parsed JSON values or Map
s from String
to parsed JSON values.
The optional reviver
function, if provided, is called once for each object or list property parsed. The arguments are the property name (String
) or list index (int
), and the value is the parsed value. The return value of the reviver will be used as the value of that property instead the parsed value.
Throws FormatException
if the input is not valid JSON text.
Serializes object
into output
stream.
Performs the same operations as stringify
but outputs the resulting string to an existing StringSink
instead of creating a new String
.
If serialization fails by throwing, some data might have been added to output
, but it won't contain valid JSON text.
Serializes object
into a JSON string.
Directly serializable values are num
, String
, bool
, and Null
, as well as some List
and Map
values. For List
, the elements must all be serializable. For Map
, the keys must be String
and the values must be serializable.
If a value is any other type is attempted serialized, a "toJson()" method is invoked on the object and the result, which must be a directly serializable value, is serialized instead of the original value.
If the object does not support this method, throws, or returns a value that is not directly serializable, a JsonUnsupportedObjectError
exception is thrown. If the call throws (including the case where there is no nullary "toJson" method, the error is caught and stored in the JsonUnsupportedObjectError
's cause
field.
If a List
or Map
contains a reference to itself, directly or through other lists or maps, it cannot be serialized and a JsonCyclicError
is thrown.
Json Objects should not change during serialization. If an object is serialized more than once, stringify
is allowed to cache the JSON text for it. I.e., if an object changes after it is first serialized, the new values may or may not be reflected in the result.
A JsonListener
that builds data objects from the parser events.
This is a simple stack-based object builder. It keeps the most recently seen value in a variable, and uses it depending on the following event.
The current Map
or List
being built.
The most recently read property key.
Stack used to handle nested containers.
The current container is pushed on the stack when a new one is started. If the container is a Map
, there is also a current key
which is also stored on the stack.
The most recently read value.
Read out the final result of parsing a JSON string.
Pops the top container from the stack
, including a key if applicable.
Pushes the currently active container (and key, if a Map
).
/ Implementation ///////////////////////////////////////////////////////////
Called on failure to parse source
.
Parses source
, or throws if it fails.
Parses a "false" literal starting at position
.
source
must be "f".position
Parses a "null" literal starting at position
.
source
must be "n".position
Parses a "true" literal starting at position
.
source
must be "t".position