001package ca.uhn.fhir.parser; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.model.api.IResource; 024import ca.uhn.fhir.parser.json.BaseJsonLikeWriter; 025import ca.uhn.fhir.parser.json.JsonLikeStructure; 026import org.hl7.fhir.instance.model.api.IAnyResource; 027import org.hl7.fhir.instance.model.api.IBaseResource; 028 029import java.io.IOException; 030 031/** 032 * An extension to the parser interface that is implemented by parsers that understand a generalized form of 033 * JSON data. This generalized form uses Map-like, List-like, and scalar elements to construct resources. 034 * <p> 035 * Thread safety: <b>Parsers are not guaranteed to be thread safe</b>. Create a new parser instance for every thread or 036 * every message being parsed/encoded. 037 * </p> 038 */ 039public interface IJsonLikeParser extends IParser { 040 041 void encodeResourceToJsonLikeWriter(IBaseResource theResource, BaseJsonLikeWriter theJsonLikeWriter) throws IOException, DataFormatException; 042 043 /** 044 * Parses a resource from a JSON-like data structure 045 * 046 * @param theResourceType 047 * The resource type to use. This can be used to explicitly specify a class which extends a built-in type 048 * (e.g. a custom type extending the default Patient class) 049 * @param theJsonLikeStructure 050 * The JSON-like structure to parse 051 * @return A parsed resource 052 * @throws DataFormatException 053 * If the resource can not be parsed because the data is not recognized or invalid for any reason 054 */ 055 <T extends IBaseResource> T parseResource(Class<T> theResourceType, JsonLikeStructure theJsonLikeStructure) throws DataFormatException; 056 057 /** 058 * Parses a resource from a JSON-like data structure 059 * 060 * @param theJsonLikeStructure 061 * The JSON-like structure to parse 062 * @return A parsed resource. Note that the returned object will be an instance of {@link IResource} or 063 * {@link IAnyResource} depending on the specific FhirContext which created this parser. 064 * @throws DataFormatException 065 * If the resource can not be parsed because the data is not recognized or invalid for any reason 066 */ 067 IBaseResource parseResource(JsonLikeStructure theJsonLikeStructure) throws DataFormatException; 068 069}