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