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