FHIR Chat · STU3 Serialisation issue · dotnet

Stream: dotnet

Topic: STU3 Serialisation issue


view this post on Zulip Richard Kavanagh (Aug 25 2017 at 13:18):

I'm using the latest STU3 NuGet package in a small demo app.

The App creates its own version of "Bundle" inherited from the core Bundle

   public class ITKBundle : Bundle
  {
    .....
  }

Later on it attempts to serialise this to XML using

  FhirSerializer.SerializeResourceToXml(xx);

When it does this I get an error as attached. Any clues what I am doing wrong....

pasted image

view this post on Zulip Michel Rutten (Aug 25 2017 at 13:31):

Hi @Richard Kavanagh, I'm not sure if the API serializer supports derived classes. Apparently not...

view this post on Zulip Richard Kavanagh (Aug 25 2017 at 13:34):

It would appear not :-) Will have to think of a way around this. My "derived class" just has a bunch of helper methods so I'd hoped it would work

view this post on Zulip Michel Rutten (Aug 25 2017 at 13:36):

I can reproduce the exception in a unit test. Let me check if there's a workaround...

view this post on Zulip Michel Rutten (Aug 25 2017 at 13:40):

Hi @Richard Kavanagh, try to add the following attribute to your derived class:

[Hl7.Fhir.Introspection.FhirType("Bundle", IsResource = true)]

Now serialization should work.

view this post on Zulip Richard Kavanagh (Aug 25 2017 at 13:57):

@Michel Rutten Thanks for the suggestion, unfortunately it did not work..

view this post on Zulip Michel Rutten (Aug 25 2017 at 14:02):

I can succesfully run the following logic:

[FhirType("Bundle", IsResource = true)]
public class CustomBundle : Bundle
{
    public CustomBundle() : base() { }
}

[TestMethod]
public void TestDerivedPoCoSerialization()
{
    var bundle = new CustomBundle()
    {
        Type = Bundle.BundleType.Collection,
        Id = "MyBundle"
    };

    var xml = FhirSerializer.SerializeResourceToXml(bundle);
    Assert.IsNotNull(xml);

    var json = FhirSerializer.SerializeResourceToJson(bundle);
    Assert.IsNotNull(json);
}

view this post on Zulip Michel Rutten (Aug 25 2017 at 14:03):

Maybe you should also add [DataContract] attribute, but in my quick test, this didn't seem to be necessary.

view this post on Zulip Michel Rutten (Aug 25 2017 at 14:04):

BTW You should also add the [NotMapped] attribute to all public (helper) members that you add.

view this post on Zulip Richard Kavanagh (Aug 25 2017 at 14:18):

@Michel Rutten perfect! it was the [NotMapped] I was missing.

view this post on Zulip Michel Rutten (Aug 25 2017 at 14:18):

:+1:

view this post on Zulip Christiaan Knaap (Sep 05 2017 at 17:41):

And if the helper methods do not require state besides what is already in the Bundle, you could use extension methods. Then you don't need a derived class at all.

view this post on Zulip Brian Postlethwaite (Sep 05 2017 at 22:24):

Yeah, missing the NotMapped attribute when developers add stuff to it makes things awkward.
Also, when deserializing it won't use your class. Especially if you want to have multiple derived bundle types for different purposes.


Last updated: Apr 12 2022 at 19:14 UTC