001package org.hl7.fhir.r4.formats;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.IOException;
035import java.io.Writer;
036import java.math.BigDecimal;
037
038import org.hl7.fhir.utilities.Utilities;
039
040/**
041 * A little implementation of a json write to replace Gson .... because Gson screws up decimal values, and *we care*
042 * 
043 * @author Grahame Grieve
044 *
045 */
046public class JsonCreatorDirect implements JsonCreator {
047
048  private Writer writer;
049  private boolean pretty;
050  private boolean named;
051  private boolean valued;
052  private int indent;
053  
054  public JsonCreatorDirect(Writer writer) {
055    super();
056    this.writer = writer;
057  }
058
059  @Override
060  public void setIndent(String indent) {
061    this.pretty = !Utilities.noString(indent);
062  }
063
064  @Override
065  public void beginObject() throws IOException {
066    checkState();
067    writer.write("{");
068    stepIn();
069  }
070
071  public void stepIn() 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  public void stepOut() throws IOException {
082    if (pretty) {
083      indent--;
084      writer.write("\r\n");
085      for (int i = 0; i < indent; i++) {
086        writer.write("  ");
087      }
088    }
089  }
090
091  private void checkState() throws IOException {
092    if (named) {
093      if (pretty)
094        writer.write(" : ");
095      else
096        writer.write(":");
097      named = false;
098    }
099    if (valued) {
100      writer.write(",");
101      if (pretty) {
102        writer.write("\r\n");
103        for (int i = 0; i < indent; i++) {
104          writer.write("  ");
105        }        
106      }
107      valued = false;
108    }
109  }
110
111  @Override
112  public void endObject() throws IOException {
113    stepOut();
114    writer.write("}");    
115  }
116
117  @Override
118  public void nullValue() throws IOException {
119    checkState();
120    writer.write("null");
121    valued = true;
122  }
123
124  @Override
125  public void name(String name) throws IOException {
126    checkState();
127    writer.write("\""+name+"\"");
128    named = true;
129  }
130
131  @Override
132  public void value(String value) throws IOException {
133    checkState();
134    writer.write("\""+Utilities.escapeJson(value)+"\"");    
135    valued = true;
136  }
137
138  @Override
139  public void value(Boolean value) throws IOException {
140    checkState();
141    if (value == null)
142      writer.write("null");
143    else if (value.booleanValue())
144      writer.write("true");
145    else
146      writer.write("false");
147    valued = true;
148  }
149
150  @Override
151  public void value(BigDecimal value) throws IOException {
152    checkState();
153    if (value == null)
154      writer.write("null");
155    else 
156      writer.write(value.toString());    
157    valued = true;
158  }
159
160  @Override
161  public void valueNum(String value) throws IOException {
162    checkState();
163    if (value == null)
164      writer.write("null");
165    else 
166      writer.write(value);    
167    valued = true;
168  }
169
170  @Override
171  public void value(Integer value) throws IOException {
172    checkState();
173    if (value == null)
174      writer.write("null");
175    else 
176      writer.write(value.toString());    
177    valued = true;
178  }
179
180  @Override
181  public void beginArray() throws IOException {
182    checkState();
183    writer.write("[");    
184  }
185
186  @Override
187  public void endArray() throws IOException {
188    writer.write("]");        
189  }
190
191  @Override
192  public void finish() throws IOException {
193    // nothing
194  }
195
196  @Override
197  public void link(String href) {
198    // not used
199    
200  }
201
202}