001/*- 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.util; 021 022import ca.uhn.fhir.i18n.Msg; 023import ca.uhn.fhir.model.api.IModelJson; 024import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 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; 035import java.util.List; 036 037public class JsonUtil { 038 039 private static final ObjectMapper ourMapperPrettyPrint; 040 private static final ObjectMapper ourMapperNonPrettyPrint; 041 042 static { 043 ourMapperPrettyPrint = new ObjectMapper(); 044 ourMapperPrettyPrint.setSerializationInclusion(JsonInclude.Include.NON_NULL); 045 ourMapperPrettyPrint.enable(SerializationFeature.INDENT_OUTPUT); 046 047 ourMapperNonPrettyPrint = new ObjectMapper(); 048 ourMapperNonPrettyPrint.setSerializationInclusion(JsonInclude.Include.NON_NULL); 049 ourMapperNonPrettyPrint.disable(SerializationFeature.INDENT_OUTPUT); 050 } 051 052 /** 053 * Parse JSON 054 */ 055 public static <T> T deserialize(@Nonnull String theInput, @Nonnull Class<T> theType) { 056 try { 057 return ourMapperPrettyPrint.readerFor(theType).readValue(theInput); 058 } catch (IOException e) { 059 // Should not happen 060 throw new InternalErrorException(Msg.code(2060) + e); 061 } 062 } 063 064 /** 065 * Parse JSON 066 */ 067 public static <T> List<T> deserializeList(@Nonnull String theInput, @Nonnull Class<T> theType) throws IOException { 068 return ourMapperPrettyPrint.readerForListOf(theType).readValue(theInput); 069 } 070 071 /** 072 * Encode JSON 073 */ 074 public static String serialize(@Nonnull Object theInput) { 075 return serialize(theInput, true); 076 } 077 078 /** 079 * Encode JSON 080 */ 081 public static String serialize(@Nonnull Object theInput, boolean thePrettyPrint) { 082 try { 083 StringWriter sw = new StringWriter(); 084 if (thePrettyPrint) { 085 ourMapperPrettyPrint.writeValue(sw, theInput); 086 } else { 087 ourMapperNonPrettyPrint.writeValue(sw, theInput); 088 } 089 return sw.toString(); 090 } catch (IOException e) { 091 // Should not happen 092 throw new InternalErrorException(Msg.code(2061) + e); 093 } 094 } 095 096 /** 097 * Encode JSON 098 */ 099 public static void serialize(@Nonnull Object theInput, @Nonnull Writer theWriter) throws IOException { 100 // Note: We append a string here rather than just having ourMapper write directly 101 // to the Writer because ourMapper seems to close the writer for some stupid 102 // reason.. There's probably a way of preventing that bit I'm not sure what that 103 // is and it's not a big deal here. 104 theWriter.append(serialize(theInput)); 105 } 106 107 public static String serializeOrInvalidRequest(IModelJson theJson) { 108 try { 109 return ourMapperNonPrettyPrint.writeValueAsString(theJson); 110 } catch (JsonProcessingException e) { 111 throw new InvalidRequestException(Msg.code(1741) + "Failed to encode " + theJson.getClass(), e); 112 } 113 } 114}