New serializer cannot parse quoted integers which was parsable in the Newtonsoft serializer #2231
Replies: 5 comments 1 reply
-
This is actually not a bug, but a feature. Using quotes around these integer is incorrect according to the FHIR specification, but the old parsers where by default operating in "permissive" mode, which made them accept recoverable mistakes. This was a historical decision in order not to break existing software, which would then stop accepting data that was acceptable before. You can change the behaviour in the old parsers by setting the "PermissiveParsing" option to "false" - you will then also get an error that these string decimals are incorrect. Having the default to "off" also meant the parsers accept data that is actually not fully correct. With the new parsers we had the chance to rethink this For the new parsers we have rectified this situation. They will now always operate strictly - this is why you are getting this error. However, it is easy to get back the original behaviour: the exception returned now has coded errors and also a property See here: https://docs.fire.ly/projects/Firely-NET-SDK/parsing/system-text-json-deserialization.html#finding-errors-while-deserializing for more details. Since this is a new approach, and we have just published these new parsers we are of course interested in feedback on the actual use of this new system, so let us know whether this works for you! |
Beta Was this translation helpful? Give feedback.
-
I am ok with fixing our tests to force the users to give the correct format. However it seems like both the firely and hapi test servers accept quoted numbers. Since consumers of our FHIR web services may be testing with public FHIR servers, they may be surprised once they use our services. I also found initially that the System.Text.Json deserializer didn't like inline comments in the json payload. Since our developers and testers frequently add comments to their json payloads for testing, I had to support it. Luckily the comments could be handled by setting the property |
Beta Was this translation helpful? Give feedback.
-
Since the new API will already continue parsing, it does actually parse with or without the quotes and it does produce a POCO. All the setting would do is decide whether or not a warning/error is added to the DeserializationFailedException.Exceptions list. The goal of this new design is that many users of the API have different needs of what they want to allow, hence we went for a design where you can decide for yourself, based on the coded errors the parser resturns: there will never be enough settings to satisfy everyone otherwise (this is what we learned from the "old" API). Maybe the fact that the parse results in an exception is putting you off? All the API tries to communicate is that a) there is an error in your document (there is!), but b) here is the coded list of warnings/errors - decide what you want to allow and c) here is the poco that resulted from deserialization. In your concrete case, I can see that the error returned is "JSON110" - Unexpected Json token. We could split those warnings up into more codes (e.g. one for encountering a string where a number was expected - say JSON999), then you could do:
|
Beta Was this translation helpful? Give feedback.
-
(by the way, it's interesting that the Java library accepts these strings too - it must mean they are operating in lenient mode - which then in turns means there must be databases full of incorrect FHIR data). I'll ask James about it next week at the WGM. |
Beta Was this translation helpful? Give feedback.
-
Thanks @ewoutkramer , I managed to resolve this issue by configuring the callback private static (object? Value, FhirJsonException? Exception) TryParseQuotedNumber(
ref Utf8JsonReader reader,
Type targetType,
object? originalValue,
FhirJsonException originalException)
{
if (originalException.ErrorCode == FhirJsonException.UNEXPECTED_JSON_TOKEN_CODE &&
reader.TokenType == JsonTokenType.String &&
originalValue is string str)
{
if (targetType == typeof(int))
{
return (int.Parse(str), null);
}
else if (targetType == typeof(decimal))
{
return (decimal.Parse(str), null);
}
}
return (null, originalException);
} |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
The System.Text.Json Fhir deserializer throws an exception when numeric fields are quoted unlike in the Newtonsoft serializer.
This is breaking our automated UI tests after upgrading to the latest version of the SDK and using the newer serializer.
Setting the NumberHandling property in the JsonSerializerOptions to
AllowReadingFromString
has no effect.To Reproduce
Create the following resource:
Exception thrown:
Expected behavior
System.Text.Json should also be able to parse successfully when NumberHandling is set to AllowReadingFromString
Version used:
Beta Was this translation helpful? Give feedback.
All reactions