How do I serialize a custom high precision number type (eg BigDecimal ) to json without having quotation marks added to the output? ie {"precise":0.123456789012345678901234567890}
For example:
class MyPayload{
@JsonKey(
name: 'precise',
includeIfNull: false,
fromJson: bigDecimalFromJson,
toJson: bigDecimalToJson)
BigDecimal? precise;
MyPayload(this.precise);
static const fromJsonFactory = _$MyPayloadFromJson;
static const toJsonFactory = _$MyPayloadToJson;
Map<String, dynamic> toJson() => _$MyPayloadToJson(this);
}
BigDecimal? bigDecimalFromJson(dynamic jsonElement) =>
jsonElement == null ? null : BigDecimal.parse(jsonElement.toString());
dynamic bigDecimalToJson(BigDecimal? value) =>
value == null ? null : value.toString();
// test:
test('json encode BigDecimal', () {
var task = MyPayload(
precise: BigDecimal.parse("0.123456789012345678901234567890"));
var jsonStr = json.encode(task.toJson()); //or json.encode(task) gives same result
print(jsonStr);
expect(
jsonStr,
'{"precise":0.123456789012345678901234567890}', //expect a json number type, meaning no quotes
);
});
Result:
Expected: '{"precise":0.123456789012345678901234567890}'
Actual: '{"precise":"0.123456789012345678901234567890"}'
Which: is different.
Thinking the serializer relies on some sort of is num check, I considered trying to put BigDecimal in a wrapper that extends num, but that's impossible:
class BigDecimalWrapper extends num{
final BigDecimal? value;
String toString()=> value.toString();
String toStringAsPrecision(int precision)=>value.toString();
//etc
BigDecimalWrapper(this.value);
}
Result:
classes can't extend num
The recommendation from related issue #943 is to use a custom data type, which I'm doing. Now what?
How do I serialize a custom high precision number type (eg BigDecimal ) to json without having quotation marks added to the output? ie
{"precise":0.123456789012345678901234567890}
For example:
Result:
Thinking the serializer relies on some sort of
is num
check, I considered trying to putBigDecimal
in a wrapper that extendsnum
, but that's impossible:Result:
The recommendation from related issue #943 is to use a custom data type, which I'm doing. Now what?
Cheers