
001package org.hl7.fhir.r5.liquid; 002 003import org.hl7.fhir.exceptions.FHIRException; 004import org.hl7.fhir.r5.model.Base; 005import org.hl7.fhir.utilities.FhirPublication; 006import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage; 007import org.hl7.fhir.utilities.json.model.JsonArray; 008import org.hl7.fhir.utilities.json.model.JsonElement; 009 010// this class exists to allow the Liquid Engine to be used against raw JSON 011 012@MarkedToMoveToAdjunctPackage 013public class BaseJsonWrapper extends Base { 014 015 private static final long serialVersionUID = 1L; 016 private JsonElement j; 017 018 public BaseJsonWrapper(JsonElement j) { 019 super(); 020 this.j = j; 021 } 022 023 @Override 024 public String fhirType() { 025 switch (j.type()) { 026 case BOOLEAN: return "boolean"; 027 case NUMBER: return "decimal"; 028 case OBJECT: return "Object"; 029 case STRING: return "string"; 030 default: 031 throw new Error("Shouldn't get here"); 032 } 033 } 034 035 @Override 036 public String getIdBase() { 037 if (j.isJsonObject()) { 038 return j.asJsonObject().asString("id"); 039 } else { 040 return null; 041 } 042 } 043 044 @Override 045 public void setIdBase(String value) { 046 throw new Error("BaseJsonWrapper is read only"); 047 } 048 049 @Override 050 public Base copy() { 051 throw new Error("BaseJsonWrapper is read only"); 052 } 053 054 @Override 055 public FhirPublication getFHIRPublicationVersion() { 056 return FhirPublication.R5; 057 } 058 059 public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException { 060 if (j.isJsonObject() && j.asJsonObject().has(name)) { 061 JsonElement e = j.asJsonObject().get(name); 062 if (e.isJsonArray()) { 063 JsonArray a = e.asJsonArray(); 064 Base[] l = new Base[a.size()]; 065 for (int i = 0; i < a.size(); i++) { 066 l[i] = new BaseJsonWrapper(a.get(i)); 067 } 068 return l; 069 } else { 070 Base[] l = new Base[1]; 071 l[0] = new BaseJsonWrapper(e); 072 return l; 073 } 074 } else { 075 return super.getProperty(hash, name, checkValid); 076 } 077 } 078 079 @Override 080 public String toString() { 081 if (j.isJsonPrimitive()) { 082 return j.asString(); 083 } else { 084 return super.toString(); 085 } 086 } 087 088 089 @Override 090 public boolean isPrimitive() { 091 return j.isJsonPrimitive(); 092 } 093 094 095 @Override 096 public String primitiveValue() { 097 return toString(); 098 } 099}