jsonEncode function

String jsonEncode(
  1. List args
)

Provides a jsonEncode() function that encodes a map object to a JSON string. Supports conversion of DateTime objects to strings.

object Map -> String

Example

var json = jsonEncode({"name": "John", "age": 30});
print(json is String); // true
print("JSON: $json");

Implementation

String jsonEncode(List args) {
  if (args[0] is! Map) {
    throw RuntimeError(
      "jsonEncode: Expected a map object, but got ${args[0].runtimeType}",
    );
  }

  _arityCheck(1, args.length);
  return json.jsonEncode(
    args[0],
    toEncodable: (object) {
      if (object is DateTime) {
        return object.toString();
      }
      return object;
    },
  );
}