convertEncode function

String convertEncode(
  1. Object value,
  2. {ToEncodable toEncodable}
)

Converts value to a JSON string.

If value contains objects that are not directly encodable to a JSON string (a value that is not a number, boolean, string, null, list or a map with string keys), the toEncodable function is used to convert it to an object that must be directly encodable.

If toEncodable is omitted, it defaults to a function that returns the result of calling .toJson() on the unencodable object.

Throws a JsonEncodingError if value results impossible to encode.

Implementation

String convertEncode(Object value, {ToEncodable toEncodable}) {
  try {
    return json.encode(value, toEncodable: toEncodable);
  } on JsonUnsupportedObjectError catch (e) {
    throw JsonEncodingError.fromUnsupportedObject(e);
  }
}