Currently jsontool package supports two models of iterating through the source values:
- expectX - expectNull, expectString, expectNum, etc - assumes that the current source value is of a specific type/kind (null, string, num, etc) and then either consumes it or throws an error
- tryX - tryNull, tryString, tryNum, etc - checks that the current source value is of a specific type/kind and then consumes it, otherwise returns null and don't consume anything.
Unfortunately when json data is not strongly typed or even when it has a lot of nullable string values, you have to wrap each value retrieval with additional checks. E.g. to read nullable string values you have to do tryNull() first and then tryString(). That adds some significant overhead.
Some other json processing libraries (e.g. a popular Java's Jackson) provide API to get value in a desired data type regardless of json source value. So, it would be great if jsontool could provide some similar API. For example:
- String? getString() - returns a null or a
String value - even if json source has null or a non-quoted value, such as boolean, int or a double
- num? getNum() - returns a null or a
num value if json source value is an int, double or a string that can be parsed to an int or a double - num.tryParse()
- int? getInt() - similar for int -
int.tryParse()
- double? getDouble() - similar for double -
double.tryParse()
Then json readers could directly return values without a need for additional checks. For example, the getString() in JsonByteReader could do something like this:
String? getString([List<String>? candidates]) {
var next = _nextNonWhitespaceChar();
if (next == $quot) {
if (candidates != null) {
return _tryCandidateString(candidates);
}
return _scanString();
} else if (next == $n) {
assert(_startsWith("ull", _source, _index + 1));
_index += 4;
}
return _scanString();
}
Currently
jsontoolpackage supports two models of iterating through the source values:Unfortunately when json data is not strongly typed or even when it has a lot of nullable string values, you have to wrap each value retrieval with additional checks. E.g. to read nullable string values you have to do
tryNull()first and thentryString(). That adds some significant overhead.Some other json processing libraries (e.g. a popular Java's Jackson) provide API to get value in a desired data type regardless of json source value. So, it would be great if
jsontoolcould provide some similar API. For example:Stringvalue - even if json source has null or a non-quoted value, such as boolean, int or a doublenumvalue if json source value is an int, double or a string that can be parsed to an int or a double -num.tryParse()int.tryParse()double.tryParse()Then json readers could directly return values without a need for additional checks. For example, the
getString()inJsonByteReadercould do something like this: