Stream: dotnet
Topic: POCO Serialisation
Richard Kavanagh (Nov 05 2019 at 14:50):
Using the existing libraries is it possible to serialise any POCO (that uses FHIR datatypes) or just the supplier resource POCOs?
We are looking at some use cases where we might want to have some internal structures derived from the FHIR resources and may want to convert them to/from JSON.
Externally we'd still use the full-blown FHIR Resources
Gino Canessa (Nov 05 2019 at 15:17):
As long as your classes are built (nested, named, and annotated), any JSON serializer will work. For Newtonsoft, you'll want to use a CamelCasePropertyNamesContractResolver
and set NullValueHandling
to NullValueHandling.Ignore
. The largest problem I ran into is that you can't mix the two (things done in Firely need to be serialized in Firely, things not in Firely cannot be serialized in Firely).
I ended up writing a basic prototyping language generator to spit out what I needed. One of the projects using it is here - it's a server proxy for R5 subscriptions which uses the Firely API for all the R4 work and generated classes for the rest.
Note that the classes it generates are basic - they don't have all the nice validation and quality of life things the standard library have. I meant it for use primarily in prototyping and as a base for things like you are talking about.
Christiaan Knaap (Nov 06 2019 at 10:02):
It might work if you add all the attributes needed for serialization as they are present on normal POCO's.
And then you may need to add your classes to ModelInfo.FhirTypeToCsType and maybe other lists / mappings in ModelInfo.
But to be sure you'd have to try. You may also run into internal classes or properties that prevent this.
Last updated: Apr 12 2022 at 19:14 UTC