encodeObject<T> method
Encodes an object of type T
.
This tries to encode the object using one of the following ways:
- If the
using
parameter is provided, it forwards the encoding to the provided Encodable implementation. - If the object is a SelfEncodable, it calls SelfEncodable.encode on the object.
- If the object is a supported primitive value, it encodes it as such.
- If none of the above applies, an error is thrown.
This should only be called if the format returned true
from canEncodeCustom
Implementation
@override
void encodeObject<T>(T value, {Encodable<T>? using}) {
if (using != null) {
using.encode(value, this);
} else if (value is SelfEncodable) {
value.encode(this);
} else if (value == null) {
encodeNull();
} else if (value is bool) {
encodeBool(value);
} else if (value is num) {
encodeNum(value);
} else if (value is String) {
encodeString(value);
} else if (value is List) {
encodeIterable<dynamic>(value);
} else if (value is Map<String, dynamic>) {
encodeMap<String, dynamic>(value);
} else {
throw ArgumentError('Unsupported type: ${value.runtimeType}');
}
}