View Javadoc
1   package ca.uhn.fhir.parser;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   * http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.io.IOException;
24  
25  import org.hl7.fhir.instance.model.api.IAnyResource;
26  import org.hl7.fhir.instance.model.api.IBaseResource;
27  
28  import ca.uhn.fhir.model.api.IResource;
29  import ca.uhn.fhir.parser.json.JsonLikeStructure;
30  import ca.uhn.fhir.parser.json.JsonLikeWriter;
31  
32  /**
33   * An extension to the parser interface that is implemented by parsers that understand a generalized form of
34   * JSON data. This generalized form uses Map-like, List-like, and scalar elements to construct resources.
35   * <p>
36   * Thread safety: <b>Parsers are not guaranteed to be thread safe</b>. Create a new parser instance for every thread or
37   * every message being parsed/encoded.
38   * </p>
39   */
40  public interface IJsonLikeParser extends IParser {
41  
42  	void encodeResourceToJsonLikeWriter(IBaseResource theResource, JsonLikeWriter theJsonLikeWriter) throws IOException, DataFormatException;
43  
44  	/**
45  	 * Parses a resource from a JSON-like data structure
46  	 * 
47  	 * @param theResourceType
48  	 *           The resource type to use. This can be used to explicitly specify a class which extends a built-in type
49  	 *           (e.g. a custom type extending the default Patient class)
50  	 * @param theJsonLikeStructure
51  	 *           The JSON-like structure to parse
52  	 * @return A parsed resource
53  	 * @throws DataFormatException
54  	 *            If the resource can not be parsed because the data is not recognized or invalid for any reason
55  	 */
56  	<T extends IBaseResource> T parseResource(Class<T> theResourceType, JsonLikeStructure theJsonLikeStructure) throws DataFormatException;
57  
58  	/**
59  	 * Parses a resource from a JSON-like data structure
60  	 * 
61  	 * @param theJsonLikeStructure
62  	 *           The JSON-like structure to parse
63  	 * @return A parsed resource. Note that the returned object will be an instance of {@link IResource} or
64  	 *         {@link IAnyResource} depending on the specific FhirContext which created this parser.
65  	 * @throws DataFormatException
66  	 *            If the resource can not be parsed because the data is not recognized or invalid for any reason
67  	 */
68  	IBaseResource parseResource(JsonLikeStructure theJsonLikeStructure) throws DataFormatException;
69  
70  }