001package ca.uhn.fhir.parser;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
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 java.io.IOException;
024
025import org.hl7.fhir.instance.model.api.IAnyResource;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027
028import ca.uhn.fhir.model.api.Bundle;
029import ca.uhn.fhir.model.api.IResource;
030import ca.uhn.fhir.model.api.TagList;
031import ca.uhn.fhir.parser.json.JsonLikeStructure;
032import ca.uhn.fhir.parser.json.JsonLikeWriter;
033
034/**
035 * An extension to the parser interface that is implemented by parsers that understand a generalized form of
036 * JSON data. This generalized form uses Map-like, List-like, and scalar elements to construct resources.
037 * <p>
038 * Thread safety: <b>Parsers are not guaranteed to be thread safe</b>. Create a new parser instance for every thread or
039 * every message being parsed/encoded.
040 * </p>
041 */
042public interface IJsonLikeParser extends IParser {
043
044        void encodeBundleToJsonLikeWriter(Bundle theBundle, JsonLikeWriter theJsonLikeWriter) throws IOException, DataFormatException;
045
046        void encodeResourceToJsonLikeWriter(IBaseResource theResource, JsonLikeWriter theJsonLikeWriter) throws IOException, DataFormatException;
047
048        void encodeTagListToJsonLikeWriter(TagList theTagList, JsonLikeWriter theJsonLikeWriter) throws IOException;
049
050
051        /**
052         * Parse a DSTU1 style Atom Bundle. Note that as of DSTU2, Bundle is a resource so you should use
053         * {@link #parseResource(Class, JsonLikeStructure)} with the Bundle class found in the
054         * <code>ca.uhn.hapi.fhir.model.[version].resource</code> package instead.
055         */
056        <T extends IBaseResource> Bundle parseBundle(Class<T> theResourceType, JsonLikeStructure theJsonLikeStructure);
057
058        /**
059         * Parse a DSTU1 style Atom Bundle. Note that as of DSTU2, Bundle is a resource so you should use
060         * {@link #parseResource(Class, JsonLikeStructure)} with the Bundle class found in the
061         * <code>ca.uhn.hapi.fhir.model.[version].resource</code> package instead.
062         */
063        Bundle parseBundle(JsonLikeStructure theJsonLikeStructure) throws DataFormatException;
064
065        /**
066         * Parses a resource from a JSON-like data structure
067         * 
068         * @param theResourceType
069         *           The resource type to use. This can be used to explicitly specify a class which extends a built-in type
070         *           (e.g. a custom type extending the default Patient class)
071         * @param theJsonLikeStructure
072         *           The JSON-like structure to parse
073         * @return A parsed resource
074         * @throws DataFormatException
075         *            If the resource can not be parsed because the data is not recognized or invalid for any reason
076         */
077        <T extends IBaseResource> T parseResource(Class<T> theResourceType, JsonLikeStructure theJsonLikeStructure) throws DataFormatException;
078
079
080        /**
081         * Parses a resource from a JSON-like data structure
082         * 
083         * @param theJsonLikeStructure
084         *           The JSON-like structure to parse
085         * @return A parsed resource. Note that the returned object will be an instance of {@link IResource} or
086         *         {@link IAnyResource} depending on the specific FhirContext which created this parser.
087         * @throws DataFormatException
088         *            If the resource can not be parsed because the data is not recognized or invalid for any reason
089         */
090        IBaseResource parseResource(JsonLikeStructure theJsonLikeStructure) throws DataFormatException;
091
092        /**
093         * Parses a tag list from a JSON-like data structure
094         * 
095         * @param theJsonLikeStructure
096         *           The JSON-like structure to parse
097         * @return A parsed tag list
098         */
099        TagList parseTagList(JsonLikeStructure theJsonLikeStructure);
100
101}