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