/** * Create and return a new JSON parser. * * <p> * Thread safety: <b>Parsers are not guaranteed to be thread safe</b>. Create a new parser instance for every thread * or every message being parsed/encoded. * </p> * <p> * Performance Note: <b>This method is cheap</b> to call, and may be called once for every message being processed * without incurring any performance penalty * </p> */ public IParser newJsonParser() { return new JsonParser(this, myParserErrorHandler); }
theEventWriter.beginObject(); } else { beginObject(theEventWriter, theObjectNameOrNull); write(theEventWriter, "resourceType", resDef.getName()); if (theResourceId != null && theResourceId.hasIdPart()) { write(theEventWriter, "id", theResourceId.getIdPart()); final List<HeldExtension> extensions = new ArrayList<>(0); final List<HeldExtension> modifierExtensions = new ArrayList<>(0); extractUndeclaredExtensions(theResourceId, extensions, modifierExtensions, null, null); boolean haveExtension = false; if (!extensions.isEmpty()) { beginObject(theEventWriter, "_id"); if (theResourceId.hasFormatComment()) { writeCommentsPreAndPost(theResourceId, theEventWriter); writeExtensionsAsDirectChild(theResource, theEventWriter, theResDef, extensions, modifierExtensions); List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS); List<? extends IIdType> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); profiles = super.getProfileTagsForEncoding(resource, profiles); TagList tags = getMetaTagsForEncoding(resource); InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); IdDt resourceId = resource.getId(); List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensionMetadataKeys = getExtensionMetadataKeys(resource);
@Override protected void doEncodeResourceToWriter(IBaseResource theResource, Writer theWriter) throws IOException { JsonLikeWriter eventWriter = createJsonWriter(theWriter); doEncodeResourceToJsonLikeWriter(theResource, eventWriter); }
String elementId = getCompositeElementId(theElement); if (isNotBlank(elementId)) { write(theEventWriter, "id", elementId); || nextChild instanceof RuntimeChildDeclaredExtensionDefinition) { if (!haveWrittenExtensions) { extractAndWriteExtensionsAsDirectChild(theElement, theEventWriter, myContext.getElementDefinition(theElement.getClass()), theResDef, theResource, nextChildElem, theParent); haveWrittenExtensions = true; String childName = nextChild.getChildNameByDatatype(child.getDatatype()); BaseRuntimeElementDefinition<?> type = child.getChildByName(childName); encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, narr, type, childName, theContainedResource, theSubResource, nextChildElem, false); continue; String childName = nextChild.getValidChildNames().iterator().next(); BaseRuntimeElementDefinition<?> child = nextChild.getChildByName(childName); encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, null, child, childName, theContainedResource, theSubResource, nextChildElem, false); continue; if (theContainedResource || getContainedResources().isEmpty()) { continue; if (nextValue instanceof ISupportsUndeclaredExtensions) { List<ExtensionDt> ext = ((ISupportsUndeclaredExtensions) nextValue).getUndeclaredExtensions(); force |= addToHeldExtensions(valueIdx, ext, extensions, false, nextChildElem, theParent); force |= addToHeldExtensions(valueIdx, ext, modifierExtensions, true, nextChildElem, theParent); } else { if (nextValue instanceof IBaseHasExtensions) {
write(theEventWriter, theChildName, encodedValue); } else { theEventWriter.write(encodedValue); write(theEventWriter, theChildName, ((IBaseIntegerDatatype) value).getValue()); } else { theEventWriter.write(((IBaseIntegerDatatype) value).getValue()); write(theEventWriter, theChildName, decimalValue); } else { theEventWriter.write(decimalValue); write(theEventWriter, theChildName, ((IBaseBooleanDatatype) value).getValue()); } else { Boolean booleanValue = ((IBaseBooleanDatatype) value).getValue(); String valueStr = value.getValueAsString(); if (theChildName != null) { write(theEventWriter, theChildName, valueStr); } else { theEventWriter.write(valueStr); encodeCompositeElementToStreamWriter(theResDef, theResource, theNextValue, theEventWriter, theContainedResource, theSubResource, theChildElem); theEventWriter.endObject(); break; List<IBaseResource> containedResources = getContainedResources().getContainedResources(); if (containedResources.size() > 0) { beginArray(theEventWriter, theChildName);
@Override public <T extends IBaseResource> T doParseResource(Class<T> theResourceType, Reader theReader) { JsonLikeStructure jsonStructure = new GsonStructure(); jsonStructure.load(theReader); T retVal = doParseResource(theResourceType, jsonStructure); return retVal; }
private void writeExtensionsAsDirectChild(IBaseResource theResource, JsonLikeWriter theEventWriter, RuntimeResourceDefinition resDef, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) throws IOException { if (extensions.isEmpty() == false) { beginArray(theEventWriter, "extension"); for (HeldExtension next : extensions) { next.write(resDef, theResource, theEventWriter); } theEventWriter.endArray(); } if (modifierExtensions.isEmpty() == false) { beginArray(theEventWriter, "modifierExtension"); for (HeldExtension next : modifierExtensions) { next.write(resDef, theResource, theEventWriter); } theEventWriter.endArray(); } }
@Override public void encodeResourceToJsonLikeWriter(IBaseResource theResource, JsonLikeWriter theJsonLikeWriter) throws IOException, DataFormatException { Validate.notNull(theResource, "theResource can not be null"); Validate.notNull(theJsonLikeWriter, "theJsonLikeWriter can not be null"); if (theResource.getStructureFhirVersionEnum() != myContext.getVersion().getVersion()) { throw new IllegalArgumentException( "This parser is for FHIR version " + myContext.getVersion().getVersion() + " - Can not encode a structure for version " + theResource.getStructureFhirVersionEnum()); } doEncodeResourceToJsonLikeWriter(theResource, theJsonLikeWriter); }
String elementId = getCompositeElementId(theElement); if (isNotBlank(elementId)) { write(theEventWriter, "id", elementId); || nextChild instanceof RuntimeChildDeclaredExtensionDefinition) { if (!haveWrittenExtensions) { extractAndWriteExtensionsAsDirectChild(theElement, theEventWriter, myContext.getElementDefinition(theElement.getClass()), theResDef, theResource, nextChildElem, theParent, theEncodeContext); haveWrittenExtensions = true; String childName = nextChild.getChildNameByDatatype(child.getDatatype()); BaseRuntimeElementDefinition<?> type = child.getChildByName(childName); encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, narr, type, childName, theContainedResource, nextChildElem, false, theEncodeContext); continue; String childName = nextChild.getValidChildNames().iterator().next(); BaseRuntimeElementDefinition<?> child = nextChild.getChildByName(childName); encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, null, child, childName, theContainedResource, nextChildElem, false, theEncodeContext); continue; if (theContainedResource || getContainedResources().isEmpty()) { continue; if (nextValue instanceof ISupportsUndeclaredExtensions) { List<ExtensionDt> ext = ((ISupportsUndeclaredExtensions) nextValue).getUndeclaredExtensions(); force |= addToHeldExtensions(valueIdx, ext, extensions, false, nextChildElem, theParent); force |= addToHeldExtensions(valueIdx, ext, modifierExtensions, true, nextChildElem, theParent); } else { if (nextValue instanceof IBaseHasExtensions) {
write(theEventWriter, theChildName, encodedValue); } else { theEventWriter.write(encodedValue); write(theEventWriter, theChildName, ((IBaseIntegerDatatype) value).getValue()); } else { theEventWriter.write(((IBaseIntegerDatatype) value).getValue()); write(theEventWriter, theChildName, decimalValue); } else { theEventWriter.write(decimalValue); write(theEventWriter, theChildName, ((IBaseBooleanDatatype) value).getValue()); } else { Boolean booleanValue = ((IBaseBooleanDatatype) value).getValue(); String valueStr = value.getValueAsString(); if (theChildName != null) { write(theEventWriter, theChildName, valueStr); } else { theEventWriter.write(valueStr); encodeCompositeElementToStreamWriter(theResDef, theResource, theNextValue, theEventWriter, theContainedResource, theChildElem, theEncodeContext); theEventWriter.endObject(); break; List<IBaseResource> containedResources = getContainedResources().getContainedResources(); if (containedResources.size() > 0) { beginArray(theEventWriter, theChildName);
T retVal = doParseResource(theResourceType, theJsonLikeStructure);
private void writeCommentsPreAndPost(IBase theNextValue, JsonLikeWriter theEventWriter) throws IOException { if (theNextValue.hasFormatComment()) { beginArray(theEventWriter, "fhir_comments"); List<String> pre = theNextValue.getFormatCommentsPre(); if (pre.isEmpty() == false) { for (String next : pre) { theEventWriter.write(next); } } List<String> post = theNextValue.getFormatCommentsPost(); if (post.isEmpty() == false) { for (String next : post) { theEventWriter.write(next); } } theEventWriter.endArray(); } }
@Override public void encodeResourceToJsonLikeWriter(IBaseResource theResource, JsonLikeWriter theJsonLikeWriter) throws IOException, DataFormatException { Validate.notNull(theResource, "theResource can not be null"); Validate.notNull(theJsonLikeWriter, "theJsonLikeWriter can not be null"); if (theResource.getStructureFhirVersionEnum() != myContext.getVersion().getVersion()) { throw new IllegalArgumentException( "This parser is for FHIR version " + myContext.getVersion().getVersion() + " - Can not encode a structure for version " + theResource.getStructureFhirVersionEnum()); } EncodeContext encodeContext = new EncodeContext(); String resourceName = myContext.getResourceDefinition(theResource).getName(); encodeContext.pushPath(resourceName, true); doEncodeResourceToJsonLikeWriter(theResource, theJsonLikeWriter, encodeContext); }
theEventWriter.beginObject(); } else { beginObject(theEventWriter, theObjectNameOrNull); write(theEventWriter, "resourceType", resDef.getName()); if (theResourceId != null && theResourceId.hasIdPart()) { write(theEventWriter, "id", theResourceId.getIdPart()); final List<HeldExtension> extensions = new ArrayList<>(0); final List<HeldExtension> modifierExtensions = new ArrayList<>(0); extractUndeclaredExtensions(theResourceId, extensions, modifierExtensions, null, null); boolean haveExtension = false; if (!extensions.isEmpty()) { beginObject(theEventWriter, "_id"); if (theResourceId.hasFormatComment()) { writeCommentsPreAndPost(theResourceId, theEventWriter); writeExtensionsAsDirectChild(theResource, theEventWriter, theResDef, extensions, modifierExtensions, theEncodeContext); List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS); List<? extends IIdType> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); profiles = super.getProfileTagsForEncoding(resource, profiles); TagList tags = getMetaTagsForEncoding(resource, theEncodeContext); InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); IdDt resourceId = resource.getId(); List<Map.Entry<ResourceMetadataKeyEnum<?>, Object>> extensionMetadataKeys = getExtensionMetadataKeys(resource);
@Override protected void doEncodeResourceToWriter(IBaseResource theResource, Writer theWriter, EncodeContext theEncodeContext) throws IOException { JsonLikeWriter eventWriter = createJsonWriter(theWriter); doEncodeResourceToJsonLikeWriter(theResource, eventWriter, theEncodeContext); }
@Override public <T extends IBaseResource> T doParseResource(Class<T> theResourceType, Reader theReader) { JsonLikeStructure jsonStructure = new GsonStructure(); jsonStructure.load(theReader); T retVal = doParseResource(theResourceType, jsonStructure); return retVal; }
private void writeExtensionsAsDirectChild(IBaseResource theResource, JsonLikeWriter theEventWriter, RuntimeResourceDefinition resDef, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions, EncodeContext theEncodeContext) throws IOException { if (extensions.isEmpty() == false) { beginArray(theEventWriter, "extension"); for (HeldExtension next : extensions) { next.write(resDef, theResource, theEventWriter, theEncodeContext); } theEventWriter.endArray(); } if (modifierExtensions.isEmpty() == false) { beginArray(theEventWriter, "modifierExtension"); for (HeldExtension next : modifierExtensions) { next.write(resDef, theResource, theEventWriter, theEncodeContext); } theEventWriter.endArray(); } }
/** * Create and return a new JSON parser. * * <p> * Thread safety: <b>Parsers are not guaranteed to be thread safe</b>. Create a new parser instance for every thread * or every message being parsed/encoded. * </p> * <p> * Performance Note: <b>This method is cheap</b> to call, and may be called once for every message being processed * without incurring any performance penalty * </p> */ public IParser newJsonParser() { return new JsonParser(this, myParserErrorHandler); }
T retVal = doParseResource(theResourceType, theJsonLikeStructure);
private void writeCommentsPreAndPost(IBase theNextValue, JsonLikeWriter theEventWriter) throws IOException { if (theNextValue.hasFormatComment()) { beginArray(theEventWriter, "fhir_comments"); List<String> pre = theNextValue.getFormatCommentsPre(); if (pre.isEmpty() == false) { for (String next : pre) { theEventWriter.write(next); } } List<String> post = theNextValue.getFormatCommentsPost(); if (post.isEmpty() == false) { for (String next : post) { theEventWriter.write(next); } } theEventWriter.endArray(); } }