001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 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.i18n.Msg;
024import ca.uhn.fhir.model.api.IModelJson;
025import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
026import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
027import com.fasterxml.jackson.annotation.JsonInclude;
028import com.fasterxml.jackson.core.JsonProcessingException;
029import com.fasterxml.jackson.databind.ObjectMapper;
030import com.fasterxml.jackson.databind.SerializationFeature;
031
032import javax.annotation.Nonnull;
033import java.io.IOException;
034import java.io.StringWriter;
035import java.io.Writer;
036import java.util.List;
037
038public class JsonUtil {
039
040        private static final ObjectMapper ourMapperPrettyPrint;
041        private static final ObjectMapper ourMapperNonPrettyPrint;
042
043        static {
044                ourMapperPrettyPrint = new ObjectMapper();
045                ourMapperPrettyPrint.setSerializationInclusion(JsonInclude.Include.NON_NULL);
046                ourMapperPrettyPrint.enable(SerializationFeature.INDENT_OUTPUT);
047
048                ourMapperNonPrettyPrint = new ObjectMapper();
049                ourMapperNonPrettyPrint.setSerializationInclusion(JsonInclude.Include.NON_NULL);
050                ourMapperNonPrettyPrint.disable(SerializationFeature.INDENT_OUTPUT);
051        }
052
053        /**
054         * Parse JSON
055         */
056        public static <T> T deserialize(@Nonnull String theInput, @Nonnull Class<T> theType) {
057                try {
058                        return ourMapperPrettyPrint.readerFor(theType).readValue(theInput);
059                } catch (IOException e) {
060                        // Should not happen
061                        throw new InternalErrorException(Msg.code(2060) + e);
062                }
063        }
064
065        /**
066         * Parse JSON
067         */
068        public static <T> List<T> deserializeList(@Nonnull String theInput, @Nonnull Class<T> theType) throws IOException {
069                return ourMapperPrettyPrint.readerForListOf(theType).readValue(theInput);
070        }
071
072        /**
073         * Encode JSON
074         */
075        public static String serialize(@Nonnull Object theInput) {
076                return serialize(theInput, true);
077        }
078
079        /**
080         * Encode JSON
081         */
082        public static String serialize(@Nonnull Object theInput, boolean thePrettyPrint) {
083                try {
084                        StringWriter sw = new StringWriter();
085                        if (thePrettyPrint) {
086                                ourMapperPrettyPrint.writeValue(sw, theInput);
087                        } else {
088                                ourMapperNonPrettyPrint.writeValue(sw, theInput);
089                        }
090                        return sw.toString();
091                } catch (IOException e) {
092                        // Should not happen
093                        throw new InternalErrorException(Msg.code(2061) + e);
094                }
095        }
096
097        /**
098         * Encode JSON
099         */
100        public static void serialize(@Nonnull Object theInput, @Nonnull Writer theWriter) throws IOException {
101                // Note: We append a string here rather than just having ourMapper write directly
102                // to the Writer because ourMapper seems to close the writer for some stupid
103                // reason.. There's probably a way of preventing that bit I'm not sure what that
104                // is and it's not a big deal here.
105                theWriter.append(serialize(theInput));
106        }
107
108        public static String serializeOrInvalidRequest(IModelJson theJson) {
109                try {
110                        return ourMapperNonPrettyPrint.writeValueAsString(theJson);
111                } catch (JsonProcessingException e) {
112                        throw new InvalidRequestException(Msg.code(1741) + "Failed to encode " + theJson.getClass(), e);
113                }
114        }
115}