jsonEncoder static method
Converts an object to a JSON-encoded string.
This method uses jsonEncode to serialize the data object into a JSON string.
It provides custom encoding for specific types:
- TString: Uses the
writemethod with the optionalrqparameter. - ObjectId: Converts to its string representation via the
oidproperty. - DateTime: Converts to its ISO-8601 string representation.
- Duration: Converts to its string representation.
data is the object to be encoded.
Returns a String containing the JSON-encoded representation of the data object.
Implementation
static String jsonEncoder(Object data) {
return jsonEncode(data, toEncodable: (obj) {
if (obj == null) {
return null;
}
if (obj is TString) {
return obj.write();
}
if (obj is ObjectId) {
return obj.oid;
}
if (obj is DateTime) {
return obj.toString();
}
if (obj is Duration) {
return obj.toString();
}
if (obj is Map) {
return encodeMaps(obj);
}
if (obj is Symbol) {
return symbolToKey(obj);
}
if (obj is int) {
return obj;
}
return obj.toString();
});
}