dart.json

exports dart.convert


Functions
parse(String json, reviver): dynamic
printOn(Object object, StringSink output): void
stringify(Object object): String
Classes
BuildJsonListener
JsonListener
JsonParser
ReviverJsonListener

Utilities for encoding and decoding JSON (JavaScript Object Notation) data.

Functions

static dynamic parse(String json, reviver)

Parses json and build the corresponding parsed JSON value.

Parsed JSON values are of the types num, String, bool, Null, Lists of parsed JSON values or Maps 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.

static void printOn(Object object, StringSink output)

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.

static String stringify(Object object)

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.


Class BuildJsonListener extends JsonListener

Fields
currentContainer: dynamic
key: String
result: dynamic
stack: List
value: dynamic
Getters and Setters
result: dynamic
Constructors
BuildJsonListener()
Methods
arrayElement(): void
beginArray(): void
beginObject(): void
endArray(): void
endObject(): void
handleBool(bool value): void
handleNull(): void
handleNumber(num value): void
handleString(String value): void
popContainer(): void
propertyName(): void
propertyValue(): void
pushContainer(): void

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.

Fields

dynamic currentContainer

The current Map or List being built.

String key

The most recently read property key.

final dynamic result
List stack

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.

dynamic value

The most recently read value.

Getters and Setters

dynamic get result

Read out the final result of parsing a JSON string.

Constructors

BuildJsonListener()

Methods

void arrayElement()
void beginArray()
void beginObject()
void endArray()
void endObject()
void handleBool(bool value)
void handleNull()
void handleNumber(num value)
void handleString(String value)
void popContainer()

Pops the top container from the stack, including a key if applicable.

void propertyName()
void propertyValue()
void pushContainer()

Pushes the currently active container (and key, if a Map).


Abstract class JsonListener

Constructors
JsonListener()
Methods
arrayElement(): void
beginArray(): void
beginObject(): void
endArray(): void
endObject(): void
fail(String source, int position, String message): void
handleBool(bool value): void
handleNull(): void
handleNumber(num value): void
handleString(String value): void
propertyName(): void
propertyValue(): void

/ Implementation ///////////////////////////////////////////////////////////

Constructors

JsonListener()

Methods

void arrayElement()
void beginArray()
void beginObject()
void endArray()
void endObject()
void fail(String source, int position, String message)

Called on failure to parse source.

void handleBool(bool value)
void handleNull()
void handleNumber(num value)
void handleString(String value)
void propertyName()
void propertyValue()

Class JsonParser

Static Fields
AFTER_COLON: int
ALLOW_STRING_MASK: int
ALLOW_VALUE: int
ALLOW_VALUE_MASK: int
BACKSLASH: int
BACKSPACE: int
CARRIAGE_RETURN: int
CHAR_0: int
CHAR_9: int
CHAR_E: int
CHAR_a: int
CHAR_b: int
CHAR_e: int
CHAR_f: int
CHAR_l: int
CHAR_n: int
CHAR_r: int
CHAR_s: int
CHAR_t: int
CHAR_u: int
COLON: int
COMMA: int
DECIMALPOINT: int
EMPTY: int
EMPTY_MASK: int
FORM_FEED: int
INSIDE_ARRAY: int
INSIDE_OBJECT: int
LBRACE: int
LBRACKET: int
MINUS: int
NEWLINE: int
NON_EMPTY: int
NO_VALUES: int
PLUS: int
QUOTE: int
RBRACE: int
RBRACKET: int
SLASH: int
SPACE: int
STATE_ARRAY_COMMA: int
STATE_ARRAY_EMPTY: int
STATE_ARRAY_VALUE: int
STATE_END: int
STATE_INITIAL: int
STATE_OBJECT_COLON: int
STATE_OBJECT_COMMA: int
STATE_OBJECT_EMPTY: int
STATE_OBJECT_KEY: int
STATE_OBJECT_VALUE: int
STRING_ONLY: int
TAB: int
VALUE_READ_BITS: int
Fields
listener: JsonListener
source: String
Constructors
JsonParser(String source, JsonListener listener)
Methods
fail(int position, String message): void
parse(): void
parseFalse(int position): int
parseNull(int position): int
parseNumber(int char, int position): int
parseString(int position): int
parseTrue(int position): int

Static Fields

static const int AFTER_COLON = 3
static const int ALLOW_STRING_MASK = 8
static const int ALLOW_VALUE = 0
static const int ALLOW_VALUE_MASK = 4
static const int BACKSLASH = 92
static const int BACKSPACE = 8
static const int CARRIAGE_RETURN = 13
static const int CHAR_0 = 48
static const int CHAR_9 = 57
static const int CHAR_E = 69
static const int CHAR_a = 97
static const int CHAR_b = 98
static const int CHAR_e = 101
static const int CHAR_f = 102
static const int CHAR_l = 108
static const int CHAR_n = 110
static const int CHAR_r = 114
static const int CHAR_s = 115
static const int CHAR_t = 116
static const int CHAR_u = 117
static const int COLON = 58
static const int COMMA = 44
static const int DECIMALPOINT = 46
static const int EMPTY = 0
static const int EMPTY_MASK = 16
static const int FORM_FEED = 12
static const int INSIDE_ARRAY = 1
static const int INSIDE_OBJECT = 2
static const int LBRACE = 123
static const int LBRACKET = 91
static const int MINUS = 45
static const int NEWLINE = 10
static const int NON_EMPTY = 16
static const int NO_VALUES = 12
static const int PLUS = 43
static const int QUOTE = 34
static const int RBRACE = 125
static const int RBRACKET = 93
static const int SLASH = 47
static const int SPACE = 32
static const int STATE_ARRAY_COMMA = 17
static const int STATE_ARRAY_EMPTY = 1
static const int STATE_ARRAY_VALUE = 29
static const int STATE_END = 28
static const int STATE_INITIAL = 0
static const int STATE_OBJECT_COLON = 19
static const int STATE_OBJECT_COMMA = 22
static const int STATE_OBJECT_EMPTY = 6
static const int STATE_OBJECT_KEY = 30
static const int STATE_OBJECT_VALUE = 31
static const int STRING_ONLY = 4
static const int TAB = 9
static const int VALUE_READ_BITS = 28

Fields

final JsonListener listener
final String source

Constructors

JsonParser(String source, JsonListener listener)

Methods

void fail(int position, String message)
void parse()

Parses source, or throws if it fails.

int parseFalse(int position)

Parses a "false" literal starting at position.

sourceposition must be "f".

int parseNull(int position)

Parses a "null" literal starting at position.

sourceposition must be "n".

int parseNumber(int char, int position)
int parseString(int position)
int parseTrue(int position)

Parses a "true" literal starting at position.

sourceposition must be "t".


Class ReviverJsonListener extends BuildJsonListener

Fields
result: dynamic
reviver: _Reviver
Getters and Setters
result: dynamic
Constructors
ReviverJsonListener( reviver)
Methods
arrayElement(): void
propertyValue(): void

Fields

final dynamic result
final _Reviver reviver

Getters and Setters

dynamic get result

Constructors

ReviverJsonListener( reviver)

Methods

void arrayElement()
void propertyValue()