Dart API Referencedart:jsonJSON

JSON class

Utility class to parse JSON and serialize objects to JSON.

class JSON {
  /**
   * 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.
   *
   * Throws [JSONParseException] if the input is not valid JSON text.
   */
  static parse(String json) {
    return _JsonParser.parse(json);
  }

  /**
   * Serializes [object] into a JSON string.
   *
   * Directly serializable types are [num], [String], [bool], [Null], [List]
   * and [Map].
   * 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 type, 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.
   *
   * 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.
   */
  static String stringify(Object object) {
    return _JsonStringifier.stringify(object);
  }

  /**
   * Serializes [object] into [output] stream.
   *
   * Performs the same operations as [stringify] but outputs the resulting
   * string to an existing [StringBuffer] 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 void printOn(Object object, StringBuffer output) {
    return _JsonStringifier.printOn(object, output);
  }
}

Static Methods

parse(String json) #

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.

Throws JSONParseException if the input is not valid JSON text.

static parse(String json) {
  return _JsonParser.parse(json);
}

String stringify(Object object) #

Serializes object into a JSON string.

Directly serializable types are num, String, bool, Null, List and Map. 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 type, 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.

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.

static String stringify(Object object) {
  return _JsonStringifier.stringify(object);
}

void printOn(Object object, StringBuffer output) #

Serializes object into output stream.

Performs the same operations as stringify but outputs the resulting string to an existing StringBuffer 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 void printOn(Object object, StringBuffer output) {
  return _JsonStringifier.printOn(object, output);
}