001package ca.uhn.fhir.parser.json.jackson; 002 003/*- 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 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.parser.json.BaseJsonLikeWriter; 024import com.fasterxml.jackson.core.JsonFactory; 025import com.fasterxml.jackson.core.JsonGenerator; 026import com.fasterxml.jackson.core.util.DefaultIndenter; 027import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; 028import com.fasterxml.jackson.core.util.Separators; 029 030import java.io.IOException; 031import java.io.Writer; 032import java.math.BigDecimal; 033import java.math.BigInteger; 034 035public class JacksonWriter extends BaseJsonLikeWriter { 036 037 private JsonGenerator myJsonGenerator; 038 039 public JacksonWriter(JsonFactory theJsonFactory, Writer theWriter) throws IOException { 040 myJsonGenerator = theJsonFactory.createGenerator(theWriter); 041 setWriter(theWriter); 042 } 043 044 public JacksonWriter() { 045 } 046 047 @Override 048 public BaseJsonLikeWriter init() { 049 if (isPrettyPrint()) { 050 DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter() { 051 052 /** 053 * Objects should serialize as 054 * <pre> 055 * { 056 * "key": "value" 057 * } 058 * </pre> 059 * in order to be consistent with Gson behaviour, instead of the jackson default 060 * <pre> 061 * { 062 * "key" : "value" 063 * } 064 * </pre> 065 */ 066 @Override 067 public DefaultPrettyPrinter withSeparators(Separators separators) { 068 _separators = separators; 069 _objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " "; 070 return this; 071 } 072 073 }; 074 prettyPrinter = prettyPrinter.withObjectIndenter(new DefaultIndenter(" ", "\n")); 075 076 myJsonGenerator.setPrettyPrinter(prettyPrinter); 077 } 078 return this; 079 } 080 081 @Override 082 public BaseJsonLikeWriter flush() { 083 return this; 084 } 085 086 @Override 087 public void close() throws IOException { 088 myJsonGenerator.close(); 089 } 090 091 @Override 092 public BaseJsonLikeWriter beginObject() throws IOException { 093 myJsonGenerator.writeStartObject(); 094 return this; 095 } 096 097 @Override 098 public BaseJsonLikeWriter beginObject(String name) throws IOException { 099 myJsonGenerator.writeObjectFieldStart(name); 100 return this; 101 } 102 103 @Override 104 public BaseJsonLikeWriter beginArray(String name) throws IOException { 105 myJsonGenerator.writeArrayFieldStart(name); 106 return this; 107 } 108 109 @Override 110 public BaseJsonLikeWriter write(String value) throws IOException { 111 myJsonGenerator.writeObject(value); 112 return this; 113 } 114 115 @Override 116 public BaseJsonLikeWriter write(BigInteger value) throws IOException { 117 myJsonGenerator.writeObject(value); 118 return this; 119 } 120 121 @Override 122 public BaseJsonLikeWriter write(BigDecimal value) throws IOException { 123 myJsonGenerator.writeObject(value); 124 return this; 125 } 126 127 @Override 128 public BaseJsonLikeWriter write(long value) throws IOException { 129 myJsonGenerator.writeObject(value); 130 return this; 131 } 132 133 @Override 134 public BaseJsonLikeWriter write(double value) throws IOException { 135 myJsonGenerator.writeObject(value); 136 return this; 137 } 138 139 @Override 140 public BaseJsonLikeWriter write(Boolean value) throws IOException { 141 myJsonGenerator.writeObject(value); 142 return this; 143 } 144 145 @Override 146 public BaseJsonLikeWriter write(boolean value) throws IOException { 147 myJsonGenerator.writeObject(value); 148 return this; 149 } 150 151 @Override 152 public BaseJsonLikeWriter writeNull() throws IOException { 153 myJsonGenerator.writeNull(); 154 return this; 155 } 156 157 @Override 158 public BaseJsonLikeWriter write(String name, String value) throws IOException { 159 myJsonGenerator.writeObjectField(name, value); 160 return this; 161 } 162 163 @Override 164 public BaseJsonLikeWriter write(String name, BigInteger value) throws IOException { 165 myJsonGenerator.writeObjectField(name, value); 166 return this; 167 } 168 169 @Override 170 public BaseJsonLikeWriter write(String name, BigDecimal value) throws IOException { 171 myJsonGenerator.writeObjectField(name, value); 172 return this; 173 } 174 175 @Override 176 public BaseJsonLikeWriter write(String name, long value) throws IOException { 177 myJsonGenerator.writeObjectField(name, value); 178 return this; 179 } 180 181 @Override 182 public BaseJsonLikeWriter write(String name, double value) throws IOException { 183 myJsonGenerator.writeObjectField(name, value); 184 return this; 185 } 186 187 @Override 188 public BaseJsonLikeWriter write(String name, Boolean value) throws IOException { 189 myJsonGenerator.writeObjectField(name, value); 190 return this; 191 } 192 193 @Override 194 public BaseJsonLikeWriter write(String name, boolean value) throws IOException { 195 myJsonGenerator.writeObjectField(name, value); 196 return this; 197 } 198 199 @Override 200 public BaseJsonLikeWriter endObject() throws IOException { 201 myJsonGenerator.writeEndObject(); 202 return this; 203 } 204 205 @Override 206 public BaseJsonLikeWriter endArray() throws IOException { 207 myJsonGenerator.writeEndArray(); 208 return this; 209 } 210 211 @Override 212 public BaseJsonLikeWriter endBlock() throws IOException { 213 myJsonGenerator.writeEndObject(); 214 return this; 215 } 216}