001package org.hl7.fhir.r4.formats;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
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
023
024import java.io.IOException;
025import java.io.Writer;
026import java.math.BigDecimal;
027
028import org.hl7.fhir.utilities.Utilities;
029
030/**
031 * A little implementation of a json write to replace Gson .... because Gson screws up decimal values, and *we care*
032 * 
033 * @author Grahame Grieve
034 *
035 */
036public class JsonCreatorDirect implements JsonCreator {
037
038  private Writer writer;
039  private boolean pretty;
040  private boolean named;
041  private boolean valued;
042  private int indent;
043  
044  public JsonCreatorDirect(Writer writer) {
045    super();
046    this.writer = writer;
047  }
048
049  @Override
050  public void setIndent(String indent) {
051    this.pretty = !Utilities.noString(indent);
052  }
053
054  @Override
055  public void beginObject() throws IOException {
056    checkState();
057    writer.write("{");
058    stepIn();
059  }
060
061  public void stepIn() throws IOException {
062    if (pretty) {
063      indent++;
064      writer.write("\r\n");
065      for (int i = 0; i < indent; i++) {
066        writer.write("  ");
067      }
068    }
069  }
070
071  public void stepOut() throws IOException {
072    if (pretty) {
073      indent--;
074      writer.write("\r\n");
075      for (int i = 0; i < indent; i++) {
076        writer.write("  ");
077      }
078    }
079  }
080
081  private void checkState() throws IOException {
082    if (named) {
083      if (pretty)
084        writer.write(" : ");
085      else
086        writer.write(":");
087      named = false;
088    }
089    if (valued) {
090      writer.write(",");
091      if (pretty) {
092        writer.write("\r\n");
093        for (int i = 0; i < indent; i++) {
094          writer.write("  ");
095        }        
096      }
097      valued = false;
098    }
099  }
100
101  @Override
102  public void endObject() throws IOException {
103    stepOut();
104    writer.write("}");    
105  }
106
107  @Override
108  public void nullValue() throws IOException {
109    checkState();
110    writer.write("null");
111    valued = true;
112  }
113
114  @Override
115  public void name(String name) throws IOException {
116    checkState();
117    writer.write("\""+name+"\"");
118    named = true;
119  }
120
121  @Override
122  public void value(String value) throws IOException {
123    checkState();
124    writer.write("\""+Utilities.escapeJson(value)+"\"");    
125    valued = true;
126  }
127
128  @Override
129  public void value(Boolean value) throws IOException {
130    checkState();
131    if (value == null)
132      writer.write("null");
133    else if (value.booleanValue())
134      writer.write("true");
135    else
136      writer.write("false");
137    valued = true;
138  }
139
140  @Override
141  public void value(BigDecimal value) throws IOException {
142    checkState();
143    if (value == null)
144      writer.write("null");
145    else 
146      writer.write(value.toString());    
147    valued = true;
148  }
149
150  @Override
151  public void valueNum(String value) throws IOException {
152    checkState();
153    if (value == null)
154      writer.write("null");
155    else 
156      writer.write(value);    
157    valued = true;
158  }
159
160  @Override
161  public void value(Integer value) throws IOException {
162    checkState();
163    if (value == null)
164      writer.write("null");
165    else 
166      writer.write(value.toString());    
167    valued = true;
168  }
169
170  @Override
171  public void beginArray() throws IOException {
172    checkState();
173    writer.write("[");    
174  }
175
176  @Override
177  public void endArray() throws IOException {
178    writer.write("]");        
179  }
180
181  @Override
182  public void finish() throws IOException {
183    // nothing
184  }
185
186  @Override
187  public void link(String href) {
188    // not used
189    
190  }
191
192}