Correct way to JSON serialize a list of resources (dtsu3)

906 views
Skip to first unread message

resend...@gmail.com

unread,
Oct 11, 2016, 2:51:27 PM10/11/16
to HAPI FHIR
I've produced a list of entries, List<AllergyIntolerance> allergyList. I was wanting to serialize them as json array  eg:  [ {a1}, {a2}, ... ]

With the goal of having an endpoint that returns a list of allergies for a given patient. My initial approach was something like "gson.toJson(allergyList)".. but the output was really noisy.

I found that I should be using FhirContext.forDstu3().newJsonParser().

I'm having trouble finding an ideal method signature to use. I see a signature for encoding a model.api.Bundle to String, but way to encode a dstu3.model.Bundle to String. I see that I can encode an IBaseResource to String, which is what I want to do; but, I have to loop through each item individually and append to my own StringBuilder (I couldn't find any obvious way to make model.api.Bundle objects accept entries that are IBaseResource implementors... only IResource implementors).

Thanks,

Rob

resend...@gmail.com

unread,
Oct 11, 2016, 3:06:59 PM10/11/16
to HAPI FHIR, resend...@gmail.com
After posting this I realized dstu3.model.Bundle i just an IBaseResource, so the "String encodeResourceToString(IBaseResource ibr)" signature should work fine. I'm still not totally sure whether it is correct to return a Bundle object or a simple JSON array. The SMART on FHIR endpoints seem to return bundle objects, as do the hapi demo endpoints.

James Agnew

unread,
Oct 11, 2016, 9:50:39 PM10/11/16
to resend...@gmail.com, HAPI FHIR
Hi Rob,

For lists of resources, having your FHIR service return a "plain JSON list" isn't really a FHIR way of doing it, but there are a few acceptable ways of doing do. You can certainly return a bundle, and that's how most FHIR operations work. For something like a current allergy list, you're probably actually looking for the List resource. 

There's some discussion here: http://hl7-fhir.github.io/lifecycle.html#current and some examples of this here: http://hl7-fhir.github.io/list-examples.html

We don't currently have any examples on the site of how to implement this particular pattern (contributions welcome of course :) ) but it could be done as a standard search method in HAPI server like any other.

Cheers,
James


--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+unsubscribe@googlegroups.com.
To post to this group, send email to hapi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/c5fa7344-3a14-4929-8fb9-1fdcc4cbfe30%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

resend...@gmail.com

unread,
Oct 12, 2016, 9:45:34 AM10/12/16
to HAPI FHIR, resend...@gmail.com
James, Thanks for the help and resources... very much appreciated!


On Tuesday, October 11, 2016 at 8:50:39 PM UTC-5, James Agnew wrote:
Hi Rob,

For lists of resources, having your FHIR service return a "plain JSON list" isn't really a FHIR way of doing it, but there are a few acceptable ways of doing do. You can certainly return a bundle, and that's how most FHIR operations work. For something like a current allergy list, you're probably actually looking for the List resource. 

There's some discussion here: http://hl7-fhir.github.io/lifecycle.html#current and some examples of this here: http://hl7-fhir.github.io/list-examples.html

We don't currently have any examples on the site of how to implement this particular pattern (contributions welcome of course :) ) but it could be done as a standard search method in HAPI server like any other.

Cheers,
James

On Tue, Oct 11, 2016 at 3:06 PM, <resend...@gmail.com> wrote:
After posting this I realized dstu3.model.Bundle i just an IBaseResource, so the "String encodeResourceToString(IBaseResource ibr)" signature should work fine. I'm still not totally sure whether it is correct to return a Bundle object or a simple JSON array. The SMART on FHIR endpoints seem to return bundle objects, as do the hapi demo endpoints.


On Tuesday, October 11, 2016 at 1:51:27 PM UTC-5, resend...@gmail.com wrote:
I've produced a list of entries, List<AllergyIntolerance> allergyList. I was wanting to serialize them as json array  eg:  [ {a1}, {a2}, ... ]

With the goal of having an endpoint that returns a list of allergies for a given patient. My initial approach was something like "gson.toJson(allergyList)".. but the output was really noisy.

I found that I should be using FhirContext.forDstu3().newJsonParser().

I'm having trouble finding an ideal method signature to use. I see a signature for encoding a model.api.Bundle to String, but way to encode a dstu3.model.Bundle to String. I see that I can encode an IBaseResource to String, which is what I want to do; but, I have to loop through each item individually and append to my own StringBuilder (I couldn't find any obvious way to make model.api.Bundle objects accept entries that are IBaseResource implementors... only IResource implementors).

Thanks,

Rob

--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.

To post to this group, send email to hapi...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages