Search in sources :

Example 1 with LightRawJsonEncoder

use of org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder in project ehrbase by ehrbase.

the class RawJsonTransform method toRawJson.

public static void toRawJson(Result<Record> result) {
    if (result.isEmpty())
        return;
    for (Record record : result) {
        for (Field field : record.fields()) {
            // get associated value
            if (record.getValue(field) instanceof String || record.getValue(field) instanceof JSONB) {
                String value = record.getValue(field).toString();
                String jsonbOrigin = null;
                if (value.startsWith("[")) {
                    // check if this is a valid array
                    JsonReader jsonReader = Json.createReader(new StringReader(value));
                    try {
                        jsonReader.readArray();
                        jsonbOrigin = "{\"$array$\":" + value + "}";
                    } catch (JsonException e) {
                    // not a json array, do nothing
                    } finally {
                        jsonReader.close();
                    }
                } else if (value.startsWith("{")) {
                    JsonReader jsonReader = Json.createReader(new StringReader(value));
                    try {
                        jsonReader.readObject();
                        jsonbOrigin = value;
                    } catch (JsonException e) {
                    // not a json object, do nothing
                    } finally {
                        jsonReader.close();
                    }
                }
                // apply the transformation
                if (jsonbOrigin != null) {
                    JsonElement jsonElement = new LightRawJsonEncoder(jsonbOrigin).encodeContentAsJson(null);
                    if (jsonElement.getAsJsonObject().has(ARRAY_MARKER)) {
                        jsonElement = jsonElement.getAsJsonObject().getAsJsonArray(ARRAY_MARKER);
                    }
                    record.setValue(field, jsonElement);
                }
            }
        }
    }
}
Also used : JsonException(jakarta.json.JsonException) Field(org.jooq.Field) JSONB(org.jooq.JSONB) LightRawJsonEncoder(org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder) JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Record(org.jooq.Record)

Example 2 with LightRawJsonEncoder

use of org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder in project openEHR_SDK by ehrbase.

the class DBEncodeTest method testOtherParticipationsPartyRef.

@Test
public void testOtherParticipationsPartyRef() throws IOException {
    Composition composition = new CanonicalJson().unmarshal(IOUtils.toString(CompositionTestDataCanonicalJson.OTHER_PARTICIPATIONS.getStream(), UTF_8), Composition.class);
    assertNotNull(composition);
    CompositionSerializer compositionSerializerRawJson = new CompositionSerializer();
    String db_encoded = compositionSerializerRawJson.dbEncode(composition);
    assertNotNull(db_encoded);
    JsonElement converted = new LightRawJsonEncoder(db_encoded).encodeContentAsJson("composition");
    // see if this can be interpreted by Archie
    Composition composition2 = new CanonicalJson().unmarshal(converted.toString(), Composition.class);
    assertNotNull(composition2);
    assertEquals("PERSON", composition2.itemsAtPath("/content[openEHR-EHR-ACTION.minimal.v1]/other_participations/performer/external_ref/type").get(0));
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) CompositionTestDataCanonicalJson(org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson) LightRawJsonEncoder(org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder) JsonElement(com.google.gson.JsonElement) Test(org.junit.Test)

Example 3 with LightRawJsonEncoder

use of org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder in project openEHR_SDK by ehrbase.

the class DBEncodeTest method testDBEncodeDecodeInstruction.

@Test
public void testDBEncodeDecodeInstruction() throws IOException {
    Composition composition = new CanonicalJson().unmarshal(IOUtils.toString(CompositionTestDataCanonicalJson.NESTED_EN_V1.getStream(), UTF_8), Composition.class);
    CompositionSerializer compositionSerializer = new CompositionSerializer();
    String encoded = compositionSerializer.dbEncode(composition);
    Assert.assertTrue(encoded.contains(CompositionSerializer.TAG_EXPIRY_TIME));
    String json = new LightRawJsonEncoder(encoded).encodeCompositionAsString();
    Composition result = new CanonicalJson().unmarshal(json, Composition.class);
    DvDateTime expiryTime = ((Instruction) ((Section) result.getContent().get(0)).getItems().get(0)).getExpiryTime();
    Assert.assertNotNull(expiryTime);
    Assert.assertEquals(OffsetDateTime.parse("2021-05-18T13:13:09.780+03:00"), expiryTime.getValue());
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) CompositionTestDataCanonicalJson(org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson) LightRawJsonEncoder(org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder) Instruction(com.nedap.archie.rm.composition.Instruction) Section(com.nedap.archie.rm.composition.Section) DvDateTime(com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime) Test(org.junit.Test)

Example 4 with LightRawJsonEncoder

use of org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder in project openEHR_SDK by ehrbase.

the class DBEncodeTest method testDateTimeEncodeDecode.

@Test
public void testDateTimeEncodeDecode() throws IOException {
    Composition composition = new CanonicalJson().unmarshal(IOUtils.toString(CompositionTestDataCanonicalJson.DATE_TIME_TESTS.getStream(), UTF_8), Composition.class);
    assertNotNull(composition);
    CompositionSerializer compositionSerializerRawJson = new CompositionSerializer();
    String db_encoded = compositionSerializerRawJson.dbEncode(composition);
    assertNotNull(db_encoded);
    JsonElement converted = new LightRawJsonEncoder(db_encoded).encodeContentAsJson("composition");
    // see if this can be interpreted by Archie
    Composition composition2 = new CanonicalJson().unmarshal(converted.toString(), Composition.class);
    assertNotNull(composition2);
    String dvtestPrefix = "/content[openEHR-EHR-OBSERVATION.test_all_types.v1]/data[at0001]/events[at0002]/data[at0003]";
    assertEquals("2019-01-28", composition2.itemsAtPath(dvtestPrefix + "/items[at0010.1]/value/value").get(0).toString());
    assertEquals("2019-01-28T10:00", composition2.itemsAtPath(dvtestPrefix + "/items[at0010.2]/value/value").get(0).toString());
    assertEquals("2019-01-28T10:00+07:00", composition2.itemsAtPath(dvtestPrefix + "/items[at0010.21]/value/value").get(0).toString());
    assertEquals("2019-01", composition2.itemsAtPath(dvtestPrefix + "/items[at0010.3]/value/value").get(0).toString());
    assertEquals("2019", composition2.itemsAtPath(dvtestPrefix + "/items[at0010.4]/value/value").get(0).toString());
    assertEquals("2019-01-28T21:22:49.427+07:00", composition2.itemsAtPath(dvtestPrefix + "/items[at0011]/value/value").get(0).toString());
    assertEquals("18:36:49", composition2.itemsAtPath(dvtestPrefix + "/items[at0012.1]/value/value").get(0).toString());
    assertEquals("18:36", composition2.itemsAtPath(dvtestPrefix + "/items[at0012.2]/value/value").get(0).toString());
    assertEquals("18:00", composition2.itemsAtPath(dvtestPrefix + "/items[at0012.3]/value/value").get(0).toString());
    assertEquals("18:36+07:00", composition2.itemsAtPath(dvtestPrefix + "/items[at0012.4]/value/value").get(0).toString());
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) CompositionTestDataCanonicalJson(org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson) LightRawJsonEncoder(org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder) JsonElement(com.google.gson.JsonElement) Test(org.junit.Test)

Example 5 with LightRawJsonEncoder

use of org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder in project openEHR_SDK by ehrbase.

the class DBEncodeTest method testNestedLanguage.

@Test
public void testNestedLanguage() throws IOException {
    Composition composition = new CanonicalJson().unmarshal(IOUtils.toString(CompositionTestDataCanonicalJson.SUBJECT_PARTY_SELF.getStream(), UTF_8), Composition.class);
    assertNotNull(composition);
    CompositionSerializer compositionSerializerRawJson = new CompositionSerializer();
    String db_encoded = compositionSerializerRawJson.dbEncode(composition);
    assertNotNull(db_encoded);
    JsonElement converted = new LightRawJsonEncoder(db_encoded).encodeContentAsJson("composition");
    // see if this can be interpreted by Archie
    Composition composition2 = new CanonicalJson().unmarshal(converted.toString(), Composition.class);
    assertNotNull(composition2);
    assertEquals("de", composition2.itemsAtPath("/content[openEHR-EHR-SECTION.allgemeine_angaben.v1]/items[openEHR-EHR-EVALUATION.problem_diagnosis_covid.v1]/language/code_string").get(0).toString());
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) CompositionTestDataCanonicalJson(org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson) LightRawJsonEncoder(org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder) JsonElement(com.google.gson.JsonElement) Test(org.junit.Test)

Aggregations

LightRawJsonEncoder (org.ehrbase.serialisation.dbencoding.rawjson.LightRawJsonEncoder)39 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)35 Test (org.junit.Test)34 Composition (com.nedap.archie.rm.composition.Composition)30 CompositionTestDataCanonicalJson (org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson)27 JsonElement (com.google.gson.JsonElement)22 CanonicalXML (org.ehrbase.serialisation.xmlencoding.CanonicalXML)10 CompositionTestDataCanonicalXML (org.ehrbase.test_data.composition.CompositionTestDataCanonicalXML)9 RMDataFormat (org.ehrbase.serialisation.RMDataFormat)7 FlatJasonProvider (org.ehrbase.serialisation.flatencoding.FlatJasonProvider)7 TestDataTemplateProvider (org.ehrbase.serialisation.templateprovider.TestDataTemplateProvider)6 Ignore (org.junit.Ignore)5 DvDateTime (com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime)2 CompositionSerializer (org.ehrbase.serialisation.dbencoding.CompositionSerializer)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FeederAuditDetails (com.nedap.archie.rm.archetyped.FeederAuditDetails)1 AdminEntry (com.nedap.archie.rm.composition.AdminEntry)1 Instruction (com.nedap.archie.rm.composition.Instruction)1 Section (com.nedap.archie.rm.composition.Section)1