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);
}
}
}
}
}
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));
}
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());
}
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());
}
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());
}
Aggregations