dcarbone / php-fhir

Tools for consuming data from a FHIR server with PHP
Apache License 2.0
129 stars 40 forks source link

Collection type fields are not being properly JSON serialized #140

Closed Aravind-MJ closed 2 months ago

Aravind-MJ commented 2 months ago

In Questionnaire, we have list of items. When calling ->jsonSerialize on Questionnaire, it returns a \stdClass object but the values in item field is an array of QuestionnaireItem instances. Shouldn't they also be jsonSerialized? For now I have a helper function nestedJsonSerialize that I use for JSON encoding. Not sure about XMLSerialize though.

dcarbone commented 2 months ago

No. In PHP, that method is the implementation of the JsonSerializable interface and is intended to collect the fields to be encoded when passing the object to json_encode.

To perform the actual serialization, use json_encode.

Aravind-MJ commented 2 months ago

Ah so I don't have to call that function before passing the object into the json_encode. I understood that wrong. Gotcha.