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