encodeJsonTree method
Converts instance
to a JSON tree.
Before calling this method, you should call isInstance to check whether
instance
is an instance of T
.
Implementation
@override
Object? encodeJsonTree(T instance) {
if (instance == null) {
return null;
}
final kind = findKindByInstance(instance);
final name = kind.name;
final jsonTree = kind.encodeJsonTree(instance);
if (jsonTree is Map) {
assert(jsonTree[jsonDiscriminator] == null);
jsonTree[jsonDiscriminator] = name;
return jsonTree;
} else {
return {
jsonDiscriminator: name,
'value': jsonTree,
};
}
}