View Javadoc
1   package ca.uhn.fhir.parser.json;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.math.BigDecimal;
26  import java.math.BigInteger;
27  import java.util.Stack;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import com.google.gson.stream.JsonWriter;
33  
34  public class GsonWriter extends JsonLikeWriter {
35  	private static final Logger log = LoggerFactory.getLogger(GsonWriter.class);
36  
37  	private JsonWriter eventWriter;
38  	private enum BlockType {
39  		NONE, OBJECT, ARRAY
40  	}
41  	private BlockType blockType = BlockType.NONE;
42  	private Stack<BlockType> blockStack = new Stack<BlockType>(); 
43  
44  	public GsonWriter () {
45  		super();
46  	}
47  	public GsonWriter (Writer writer) {
48  		setWriter(writer);
49  	}
50  
51  	@Override
52  	public JsonLikeWriter init() throws IOException {
53  		eventWriter = new JsonWriter(getWriter());
54  		eventWriter.setSerializeNulls(true);
55  		if (isPrettyPrint()) {
56  			eventWriter.setIndent("  ");
57  		}
58  		blockType = BlockType.NONE;
59  		blockStack.clear();
60  		return this;
61  	}
62  
63  	@Override
64  	public JsonLikeWriter flush() throws IOException {
65  		if (blockType != BlockType.NONE) {
66  			log.error("JsonLikeStreamWriter.flush() called but JSON document is not finished");
67  		}
68  		eventWriter.flush();
69  		getWriter().flush();
70  		return this;
71  	}
72  
73  	@Override
74  	public void close() throws IOException {
75  		eventWriter.close();
76  		getWriter().close();
77  	}
78  
79  	@Override
80  	public JsonLikeWriter beginObject() throws IOException {
81  		blockStack.push(blockType);
82  		blockType = BlockType.OBJECT;
83  		eventWriter.beginObject();
84  		return this;
85  	}
86  
87  	@Override
88  	public JsonLikeWriter beginArray() throws IOException {
89  		blockStack.push(blockType);
90  		blockType = BlockType.ARRAY;
91  		eventWriter.beginArray();
92  		return this;
93  	}
94  
95  	@Override
96  	public JsonLikeWriter beginObject(String name) throws IOException {
97  		blockStack.push(blockType);
98  		blockType = BlockType.OBJECT;
99  		eventWriter.name(name);
100 		eventWriter.beginObject();
101 		return this;
102 	}
103 
104 	@Override
105 	public JsonLikeWriter beginArray(String name) throws IOException {
106 		blockStack.push(blockType);
107 		blockType = BlockType.ARRAY;
108 		eventWriter.name(name);
109 		eventWriter.beginArray();
110 		return this;
111 	}
112 
113 	@Override
114 	public JsonLikeWriter write(String value) throws IOException {
115 		eventWriter.value(value);
116 		return this;
117 	}
118 
119 	@Override
120 	public JsonLikeWriter write(BigInteger value) throws IOException {
121 		eventWriter.value(value);
122 		return this;
123 	}
124 	
125 	@Override
126 	public JsonLikeWriter write(BigDecimal value) throws IOException {
127 		eventWriter.value(value);
128 		return this;
129 	}
130 
131 	@Override
132 	public JsonLikeWriter write(long value) throws IOException {
133 		eventWriter.value(value);
134 		return this;
135 	}
136 
137 	@Override
138 	public JsonLikeWriter write(double value) throws IOException {
139 		eventWriter.value(value);
140 		return this;
141 	}
142 
143 	@Override
144 	public JsonLikeWriter write(Boolean value) throws IOException {
145 		eventWriter.value(value);
146 		return this;
147 	}
148 
149 	@Override
150 	public JsonLikeWriter write(boolean value) throws IOException {
151 		eventWriter.value(value);
152 		return this;
153 	}
154 
155 	@Override
156 	public JsonLikeWriter writeNull() throws IOException {
157 		eventWriter.nullValue();
158 		return this;
159 	}
160 
161 	@Override
162 	public JsonLikeWriter write(String name, String value) throws IOException {
163 		eventWriter.name(name);
164 		eventWriter.value(value);
165 		return this;
166 	}
167 
168 	@Override
169 	public JsonLikeWriter write(String name, BigInteger value) throws IOException {
170 		eventWriter.name(name);
171 		eventWriter.value(value);
172 		return this;
173 	}
174 	@Override
175 	public JsonLikeWriter write(String name, BigDecimal value) throws IOException {
176 		eventWriter.name(name);
177 		eventWriter.value(value);
178 		return this;
179 	}
180 
181 	@Override
182 	public JsonLikeWriter write(String name, long value) throws IOException {
183 		eventWriter.name(name);
184 		eventWriter.value(value);
185 		return this;
186 	}
187 
188 	@Override
189 	public JsonLikeWriter write(String name, double value) throws IOException {
190 		eventWriter.name(name);
191 		eventWriter.value(value);
192 		return this;
193 	}
194 
195 	@Override
196 	public JsonLikeWriter write(String name, Boolean value) throws IOException {
197 		eventWriter.name(name);
198 		eventWriter.value(value);
199 		return this;
200 	}
201 
202 	@Override
203 	public JsonLikeWriter write(String name, boolean value) throws IOException {
204 		eventWriter.name(name);
205 		eventWriter.value(value);
206 		return this;
207 	}
208 
209 	@Override
210 	public JsonLikeWriter writeNull(String name) throws IOException {
211 		eventWriter.name(name);
212 		eventWriter.nullValue();
213 		return this;
214 	}
215 
216 	@Override
217 	public JsonLikeWriter endObject() throws IOException {
218 		if (blockType == BlockType.NONE) {
219 			log.error("JsonLikeStreamWriter.endObject(); called with no active JSON document");
220 		} else {
221 			if (blockType != BlockType.OBJECT) {
222 				log.error("JsonLikeStreamWriter.endObject(); called outside a JSON object. (Use endArray() instead?)");
223 				eventWriter.endArray();
224 			} else {
225 				eventWriter.endObject();
226 			}
227 			blockType = blockStack.pop();
228 		}
229 		return this;
230 	}
231 
232 	@Override
233 	public JsonLikeWriter endArray() throws IOException {
234 		if (blockType == BlockType.NONE) {
235 			log.error("JsonLikeStreamWriter.endArray(); called with no active JSON document");
236 		} else {
237 			if (blockType != BlockType.ARRAY) {
238 				log.error("JsonLikeStreamWriter.endArray(); called outside a JSON array. (Use endObject() instead?)");
239 				eventWriter.endObject();
240 			} else {
241 				eventWriter.endArray();
242 			}
243 			blockType = blockStack.pop();
244 		}
245 		return this;
246 	}
247 
248 	@Override
249 	public JsonLikeWriter endBlock() throws IOException {
250 		if (blockType == BlockType.NONE) {
251 			log.error("JsonLikeStreamWriter.endBlock(); called with no active JSON document");
252 		} else {
253 			if (blockType == BlockType.ARRAY) {
254 				eventWriter.endArray();
255 			} else {
256 				eventWriter.endObject();
257 			}
258 			blockType = blockStack.pop();
259 		}
260 		return this;
261 	}
262 
263 }