-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New json serializer serializes empty complex types as {}, which fails to deserialize with new deserializer #2235
Comments
That's right. If have documented this behaviour here: https://docs.fire.ly/projects/Firely-NET-SDK/parsing/system-text-json-serialization.html#serialization-with-pocos-and-system-text-json. The serializer does not do any validation and can easily be (misused) to generate incorrect json when pushed to do so. The serializer will serialize what you have added to the POCO, in this case an empty CodeableConcept. Trying to avoid serializing empty objects would cost extra computation time, e.g. to first traverse the children to see whether the object is empty or not, before starting to serialize the object. Basically, making serialization slower in the general case to fix this specific case. The idea is that it is easier (and cheaper) for the caller of the serializer to avoid adding empty objects in the first place... But I am all ears to hear why this is a wrong assumption. |
I have been thinking about this a bit. The problem of optimizing for empty objects is that we only know an object is not empty when we hit the first property that is set (which could possibly be buried deeply in nested "almost empty" objects). Whether we ever find that first property or not, we will already have written the opening brackets to the Utf8JsonWriter for each (nested) object by then. A possible solution would be to delay writing the opening brackets (basically caching them up in some kind of serialization state) until we hit the first property, and then flush out all the brackets. If we never encounter a single property, we just clear that cached set of brackets. |
Yeah, I am reading the FhirJsonPocoSerializer, and I think there may be places here to avoid empty arrays and objects by using writer.Reset() as you are alluding to. Not sure if the performance cost here is an issue, but we can always write benchmarks to test this. We found this issue because in our web application, our developer wrote code during development to modify a resource before saving it to the document db. Naturally, it was able to serialize to db but failed during deserialization. |
Why not set the |
Though this looks nice, I think this is only applicable when you do the "built-in" object -> json serialization. FHIR Json serialization is complex enough that we wrote our own serialization (so using the Utf8JsonWriter directly) - I cannot imagine this setting is applicable to that lower-level API. |
So, to finalize this discussion: On the serializerIt is a feature that the serializer is able to serialize incorrect data, for three reasons:
This behaviour is also documented. On the parser
So, I think we have all ingredients to handle these situations. The only thing that is missing is that it's not obvious that all of this is possible. It's documented, but the API does not make it obvious or easy to ignore specific errors. What I will do is create a new feature request for the next update of the SDK to improve the interface so this becomes easy to do. |
See also:
I'd love to get feedback on the proposal before we implement it, to see if it fits your usecase! |
The new System.Text.Json serializer serializes empty complex types as empty JSON objects
{}
which fail to deserialize with the System.Text.Json deserializer. From what I understand, the new deserializer is supposed to fail on empty objects and empty lists, so the serializer should have treated these as null like in the older serializer.To Reproduce
Version used:
The text was updated successfully, but these errors were encountered: