FHIR Chat · Applying JSON patch to resource · implementers

Stream: implementers

Topic: Applying JSON patch to resource


view this post on Zulip Mattias Flodin (May 04 2017 at 06:38):

I have a JSON patch that I want to apply to a resource. Is there any code in, say, fhir-net-api for helping with this? My initial problem is that I have a path and a value, and I want to parse the value into some fhir object (say, with FhirJsonParser.Parse). But if it's a BackboneElement then there's no resourceType member. The only way to determine the type of object to parse is by looking at the root resource type and the path attribute of the patch. I'd rather not have to implement all of that by myself.

view this post on Zulip Mattias Flodin (May 04 2017 at 06:49):

I made an element like this to reflect a json patch, but I'm pretty sure FhirJsonParser.Parse is not going to be able to figure out which type to instantiate for Value when parsing a json patch.

public class Patch : Hl7.Fhir.Model.BackboneElement
{
    public string Op;
    public string Path;
    public Base Value;
}

view this post on Zulip Grahame Grieve (May 04 2017 at 09:28):

in my server, I apply the json/xml patches to XML/JSON directly, and then parse to objects - for exactly the reason you identified

view this post on Zulip Mattias Flodin (May 04 2017 at 11:46):

Actually, how do I even serialize to a JSON patch? If I make a class to represent a patch item, which in turn contains a Resource as its value, I can't just call FhirSerializer.SerializeResourceToJson on the patch object since it isn't marked as an Fhir type for the introspection and doesn't inherit from Base.

view this post on Zulip Mattias Flodin (May 04 2017 at 11:47):

FhirJsonParser.Parse() lets you pass in a JsonReader as argument so you can have contained Fhir resources in a larger Json structure. But FhirSerializer.SerializeToJson does not seem to provide a similar way to pass in a JsonWriter?

view this post on Zulip Mattias Flodin (May 04 2017 at 11:50):

oh wait yes there is, SerializeResource does that. But what if it's a BackboneElement?

view this post on Zulip Grahame Grieve (May 04 2017 at 12:37):

so I handle Json patch as pure JSON. THe language has no representation as a resource.

view this post on Zulip Grahame Grieve (May 04 2017 at 12:37):

If you want a resource based patch, look at FHIRPath patch

view this post on Zulip Mattias Flodin (May 04 2017 at 12:59):

perhaps I could make some minor changes to the reference implementation

view this post on Zulip Brian Postlethwaite (Jun 05 2017 at 10:57):

How have you gone @Mattias Flodin, interested if you'ev managed to get any of this working, and if you're likely to contribute it back ot the fhir-net-api project.

view this post on Zulip Mattias Flodin (Jun 07 2017 at 12:35):

Yes @Brian Postlethwaite I have some modifications and it's working, but it's not pretty. I intend to contribute them back to fhir-net-api but I doubt they can be used as-is. They will need to be improved on by someone who knows the architecture better.

view this post on Zulip Mattias Flodin (Jun 07 2017 at 12:45):

The architecture of fhir-net-api serialization is such that you always have to know the container resource in order to serialize a single attribute. This complicates things a lot, sometimes you have to create dummy objects just to get serialization working for nested elements. I think it would be possible to do something better using json.net converters and possibly some context information passed into the converter.

view this post on Zulip Mattias Flodin (Jun 07 2017 at 12:45):

But doing that would require pretty serious refactoring / redesign and it's not really my place to do that.

view this post on Zulip Theo Stolker (Jun 20 2017 at 12:25):

@Brian Postlethwaite, I would be interested in having a method that applies a FHIRPath Patch, i.e., a Parameters resource, to a resource. Is that something that is planned for the fhir-net-api?

view this post on Zulip Brian Postlethwaite (Jul 02 2017 at 10:32):

We'd like to add this yes, but no-one I know of has it working on .net yet (and not at the top of the priority list yet)

view this post on Zulip Ewout Kramer (Jul 03 2017 at 10:04):

I would welcome a PR ;-)

view this post on Zulip Brian Postlethwaite (Jul 04 2017 at 04:13):

I stumbled across a dotnetcore jsonpatch bit of code I'll take a look at.
https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.jsonpatch.jsonpatchdocument
(and this older one, don't recall if I found it last time I looked https://michael-mckenna.com/how-to-add-json-patch-support-to-asp-net-web-api/)

view this post on Zulip Ewout Kramer (Jul 04 2017 at 11:02):

So, that library seems to be a Microsoft product? I was thinking about FhirPatch, which is a bit harder to find libraries for....but yes, this library would help.

view this post on Zulip Brian Postlethwaite (Jul 05 2017 at 05:46):

Yes, not till one of us does it ;)


Last updated: Apr 12 2022 at 19:14 UTC