JSON 序列化“混合”(FHIR 和非 FHIR 对象)

问题描述 投票:0回答:1

因此,如果我有一个“简单”(非 FHIR)对象。

import java.io.Serializable;
import java.util.Date;

public record SimpleSerializablePoco(String someString,
 int someInt,
 Date someDate,
 long someLong) implements Serializable {
}

我可以使用 Jackson 或 Gson 并将其序列化为字符串。

    //import com.fasterxml.jackson.databind.ObjectMapper;
    //import java.util.Date;

    SimpleSerializablePoco innerTestItem = new SimpleSerializablePoco("someString", 123, new Date(), 234L);
    
    try {
        String asJson = new ObjectMapper().writeValueAsString(simplePoco);
        String temp = asJson;
       /* works as expected */
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }

结果:

{
  "someString" : "someString",
  "someInt" : 123,
  "someDate" : 1736350952157,
  "someLong" : 234
}

...

如果我使用 FHIR,我可以使用 Hapi-FHIR Json 序列化器。

//import ca.uhn.fhir.context.FhirContext;
//import ca.uhn.fhir.parser.IParser;
//import org.hl7.fhir.r4.model.Bundle;
//import org.hl7.fhir.r4.model.Observation;
//import org.hl7.fhir.r4.model.Patient;
//import org.hl7.fhir.r4.model.Reference;
//        
        Bundle theBundle = new Bundle();
        theBundle.setId("my-bundle-fhir-logical-id-123");

        Patient pat1 = new Patient();
        pat1.setId("my-patient-fhir-logical-id-234");
        theBundle.addEntry(new Bundle.BundleEntryComponent().setResource(pat1));

        Observation obs1 = new Observation();
        obs1.setId("my-observation-fhir-logical-id-345");
        obs1.setSubject(new Reference(pat1.getId()));
        theBundle.addEntry(new Bundle.BundleEntryComponent().setResource(obs1));

        FhirContext.forR4Cached().getParserOptions().setStripVersionsFromReferences(true);
        IParser parser = FhirContext.forR4Cached().newJsonParser();
        String jsonString = parser.encodeResourceToString(theBundle);

结果:

{
  "resourceType" : "Bundle",
  "id" : "my-bundle-fhir-logical-id-123",
  "entry" : [ {
    "resource" : {
      "resourceType" : "Patient",
      "id" : "my-patient-fhir-logical-id-234"
    }
  }, {
    "resource" : {
      "resourceType" : "Observation",
      "id" : "my-observation-fhir-logical-id-345",
      "subject" : {
        "reference" : "my-patient-fhir-logical-id-234"
      }
    }
  } ]
}

据我所知,“FHIR Json”解析器...只能与实现的对象一起使用

import org.hl7.fhir.instance.model.api.IBaseResource;

............

所以我的问题是......有没有人解决了 json 序列化“混合”对象的问题,如下所示:

import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Patient;

import java.io.Serializable;
import java.util.Date;

public record HybridFhirAndNonFhirObject(String someString,
 int someInt,
 Date someDate,
 long someLong,
 SimpleSerializablePoco simpleSerializablePoco,
 Bundle bund,
 Patient patient) implements Serializable {
}

为了清楚起见,这不是一个“学术”问题。 (本题末尾列出了真正的混合对象)

但是如果我尝试使用 Jackson,它会失败......(当然......这就是为什么我们需要特殊的 FHIR 解析器来开始:))。

尝试使用 FHIR IParser 时代码将无法编译。

代码尝试:

//import ca.uhn.fhir.context.FhirContext;
//import ca.uhn.fhir.parser.IParser;
//import org.hl7.fhir.r4.model.Bundle;
//import org.hl7.fhir.r4.model.Observation;
//import org.hl7.fhir.r4.model.Patient;
//import org.hl7.fhir.r4.model.Reference;
//import java.util.Date;

        SimpleSerializablePoco simplePoco = new SimpleSerializablePoco("someString", 1001, new Date(), 1002L);

        Bundle theBundle = new Bundle();
        theBundle.setId("my-bundle-fhir-logical-id-123");

        Patient pat1 = new Patient();
        pat1.setId("my-patient-fhir-logical-id-234");
        theBundle.addEntry(new Bundle.BundleEntryComponent().setResource(pat1));

        Observation obs1 = new Observation();
        obs1.setId("my-observation-fhir-logical-id-345");
        obs1.setSubject(new Reference(pat1.getId()));
        theBundle.addEntry(new Bundle.BundleEntryComponent().setResource(obs1));

        /* an object (like cds-hooks object) that is a mixture of fhir and non-fhir objects */
        HybridFhirAndNonFhirObject hybridObject = new HybridFhirAndNonFhirObject("someString", 2001, new Date(), 2002L, simplePoco, theBundle, pat1);

        try {

            FhirContext.forR4Cached().getParserOptions().setStripVersionsFromReferences(true);
            IParser parser = FhirContext.forR4Cached().newJsonParser();
            //Below will not compile
            //String jsonString = parser.encodeResourceToString(hybridObject);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            String asJson = new ObjectMapper().writeValueAsString(hybridObject);
            String temp = asJson;
        } catch (JsonProcessingException e) {
            /* pick your poison on myriad of exceptions here */
            throw new RuntimeException(e);
        }

主要问题:

有人知道如何 JSON 序列化“混合”(常规属性和 FHIR 模型的混合包)吗?

....

额外的东西:

“真实用例”混合对象。 CdsHooks,具有简单的属性“hookInstance”、“fhirServer”等...但有一个“内部对象”,即(R4)患者。

您可以在“请求对象”下看到它:https://sandbox.cds-hooks.org/

{
    "hookInstance": "e675aa55-86fd-4e04-a09c-7d0f1dc14ac7",
    "hook": "patient-view",
    "fhirServer": "https://launch.smarthealthit.org/v/r2/fhir",
    "context": {
      "patientId": "smart-1288992",
      "userId": "Practitioner/COREPRACTITIONER1"
    },
    "prefetch": {
      "patient": {
        "resourceType": "Patient",
        "id": "smart-1288992",
        "identifier": [
          {
            "use": "usual",
            "type": {
              "coding": [
                {
                  "system": "http://hl7.org/fhir/v2/0203",
                  "code": "MR",
                  "display": "Medical record number"
                }
              ],
              "text": "Medical record number"
            },
            "system": "http://hospital.smarthealthit.org",
            "value": "1288992"
          }
        ],
        "active": true,
        "name": [
          {
            "use": "official",
            "family": [
              "Adams"
            ],
            "given": [
              "Somebody",
              "X."
            ]
          }
        ],
        "telecom": [
          {
            "system": "email",
            "value": "[email protected]"
          }
        ],
        "gender": "male",
        "birthDate": "1929-08-16",
        "address": [
          {
            "use": "home",
            "line": [
              "1 Hill AveApt 14"
            ],
            "city": "SomeCity",
            "state": "OK",
            "postalCode": "74117",
            "country": "USA"
          }
        ]
      }
    }
  }
hl7-fhir hapi-fhir
1个回答
0
投票

如果您想将非 FHIR 数据作为 FHIR 实例的一部分传送,唯一的解决方案是对其进行 Base-64 编码并将其作为二进制文件或附件发送。 不允许将非 FHIR JSON 内联作为 FHIR 实例的一部分发送。

© www.soinside.com 2019 - 2024. All rights reserved.