Open
Description
The current support for custom encoding (toEncodable
param and supporting toJson
on classes) and decoding (reviver
) deal with JSON literal types (String
, num
, Map
, List
, etc) requiring intermediate representations be created for all complex objects.
Propose to add three members (see below) to dart:convert
and then update the JsonCodec
, JsonEncoder
, JsonDecoder
classes to support then as alternatives to toEncodable
and reviver
.
Note: I don't expect devs to hand-write implementations of these types. Instead, code generators – such as json_serializable – could be updated to target the new API.
typedef WriteJson = bool Function(Object source, JsonWriter writer);
// Inspired by SDK code
// https://github.com/dart-lang/sdk/blob/e7bd3edc9f/sdk/lib/convert/json.dart#L513
abstract class JsonWriter {
void writeObject(Object object);
void startMap();
void writeKeyValue(String key, Object value);
void endMap();
void startList();
void writeListValue(Object value);
void endList();
}
// Note: already exists in the SDK sources
// https://github.com/dart-lang/sdk/blob/e7bd3edc/runtime/lib/convert_patch.dart#L79-L101
abstract class JsonReader<T> {
void handleString(String value);
void handleNumber(num value);
void handleBool(bool value);
void handleNull();
void objectStart();
void propertyName();
void propertyValue();
void objectEnd();
void arrayStart();
void arrayElement();
void arrayEnd();
T get result;
}
Related issues: