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
write
method with the optionalrq
parameter. - ObjectId: Converts to its string representation via the
oid
property. - DateTime: Converts to its ISO-8601 string representation.
- Duration: Converts to its string representation.
data
is the object to be encoded.
rq
is an optional WebRequest object used for encoding TString instances.
Returns a String containing the JSON-encoded representation of the data
object.
Implementation
static String jsonEncoder(Object data, {WebRequest? rq}) {
return jsonEncode(data, toEncodable: (obj) {
if (obj == null) {
return null;
}
if (obj is TString) {
return obj.write(rq!);
}
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, rq: rq);
}
if (obj is Symbol) {
return symbolToKey(obj);
}
if (obj is int) {
return obj;
}
return obj.toString();
});
}