001package org.hl7.fhir.r5.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.OutputStreamWriter;
036import java.math.BigDecimal;
037import java.util.ArrayList;
038import java.util.Collections;
039import java.util.List;
040import java.util.Stack;
041
042public class JsonCreatorCanonical implements JsonCreator {
043
044  public class JsonCanValue {
045    String name;
046    private JsonCanValue(String name) {
047      this.name = name;  
048   }
049  }
050
051  private class JsonCanNumberValue extends JsonCanValue {
052    private BigDecimal value;
053    private JsonCanNumberValue(String name, BigDecimal value) {
054      super(name);
055      this.value = value;  
056    }
057  }
058
059  private class JsonCanPresentedNumberValue extends JsonCanValue {
060    private String value;
061    private JsonCanPresentedNumberValue(String name, String value) {
062      super(name);
063      this.value = value;  
064    }
065  }
066
067  private class JsonCanIntegerValue extends JsonCanValue {
068    private Integer value;
069    private JsonCanIntegerValue(String name, Integer value) {
070      super(name);
071      this.value = value;  
072    }
073  }
074
075  private class JsonCanBooleanValue extends JsonCanValue  {
076    private Boolean value;
077    private JsonCanBooleanValue(String name, Boolean value) {
078      super(name);
079      this.value = value;  
080    }
081  }
082
083  private class JsonCanStringValue extends JsonCanValue {
084    private String value;
085    private JsonCanStringValue(String name, String value) {
086      super(name);
087      this.value = value;  
088    }
089  }
090
091  private class JsonCanNullValue extends JsonCanValue  {
092    private JsonCanNullValue(String name) {
093      super(name);
094    }
095  }
096
097  public class JsonCanObject extends JsonCanValue {
098
099    boolean array;
100    List<JsonCanValue> children = new ArrayList<JsonCanValue>();
101    
102    public JsonCanObject(String name, boolean array) {
103      super(name);
104      this.array = array;
105    }
106
107    public void addProp(JsonCanValue obj) {
108      children.add(obj);
109    }
110  }
111
112  Stack<JsonCanObject> stack;
113  JsonCanObject root; 
114  JsonCreatorDirect jj;
115  String name;
116  
117  public JsonCreatorCanonical(OutputStreamWriter osw) {
118    stack = new Stack<JsonCreatorCanonical.JsonCanObject>();
119    jj = new JsonCreatorDirect(osw, false, false);
120    name = null;
121  }
122
123  private String takeName() {
124    String res = name;
125    name = null;
126    return res;
127  }
128
129  @Override
130  public void beginObject() throws IOException {
131    JsonCanObject obj = new JsonCanObject(takeName(), false);
132    if (stack.isEmpty())
133      root = obj;
134    else
135      stack.peek().addProp(obj);
136    stack.push(obj);
137  }
138
139  @Override
140  public void endObject() throws IOException {
141    stack.pop();
142  }
143
144  @Override
145  public void nullValue() throws IOException {
146    stack.peek().addProp(new JsonCanNullValue(takeName()));
147  }
148
149  @Override
150  public void name(String name) throws IOException {
151    this.name = name;
152  }
153
154  @Override
155  public void value(String value) throws IOException {
156    stack.peek().addProp(new JsonCanStringValue(takeName(), value));    
157  }
158
159  @Override
160  public void value(Boolean value) throws IOException {
161    stack.peek().addProp(new JsonCanBooleanValue(takeName(), value));    
162  }
163
164  @Override
165  public void value(BigDecimal value) throws IOException {
166    stack.peek().addProp(new JsonCanNumberValue(takeName(), value));    
167  }
168  @Override
169  public void valueNum(String value) throws IOException {
170    stack.peek().addProp(new JsonCanPresentedNumberValue(takeName(), value));    
171  }
172
173
174  @Override
175  public void value(Integer value) throws IOException {
176    stack.peek().addProp(new JsonCanIntegerValue(takeName(), value));    
177  }
178
179  @Override
180  public void beginArray() throws IOException {
181    JsonCanObject obj = new JsonCanObject(takeName(), true);
182    if (!stack.isEmpty())
183      stack.peek().addProp(obj);
184    stack.push(obj);
185    
186  }
187
188  @Override
189  public void endArray() throws IOException {
190    stack.pop();    
191  }
192
193  @Override
194  public void finish() throws IOException {
195    writeObject(root);
196  }
197
198  private void writeObject(JsonCanObject obj) throws IOException {
199    jj.beginObject();
200    List<String> names = new ArrayList<String>();
201    for (JsonCanValue v : obj.children) 
202      names.add(v.name);
203    Collections.sort(names);
204    for (String n : names) {
205      jj.name(n);
206      JsonCanValue v = getPropForName(n, obj.children);
207      if (v instanceof JsonCanNumberValue)
208        jj.value(((JsonCanNumberValue) v).value);
209      else if (v instanceof JsonCanPresentedNumberValue)
210        jj.valueNum(((JsonCanPresentedNumberValue) v).value);
211      else if (v instanceof JsonCanIntegerValue)
212        jj.value(((JsonCanIntegerValue) v).value);
213      else if (v instanceof JsonCanBooleanValue)
214        jj.value(((JsonCanBooleanValue) v).value);
215      else if (v instanceof JsonCanStringValue)
216        jj.value(((JsonCanStringValue) v).value);
217      else if (v instanceof JsonCanNullValue)
218        jj.nullValue();
219      else if (v instanceof JsonCanObject) {
220        JsonCanObject o = (JsonCanObject) v;
221        if (o.array) 
222          writeArray(o);
223        else
224          writeObject(o);
225      } else
226        throw new Error("not possible");
227    }
228    jj.endObject();
229  }
230
231  private JsonCanValue getPropForName(String name, List<JsonCanValue> children) {
232    for (JsonCanValue child : children)
233      if (child.name.equals(name))
234        return child;
235    return null;
236  }
237
238  private void writeArray(JsonCanObject arr) throws IOException {
239    jj.beginArray();
240    for (JsonCanValue v : arr.children) { 
241      if (v instanceof JsonCanNumberValue)
242        jj.value(((JsonCanNumberValue) v).value);
243      else if (v instanceof JsonCanIntegerValue)
244          jj.value(((JsonCanIntegerValue) v).value);
245      else if (v instanceof JsonCanBooleanValue)
246        jj.value(((JsonCanBooleanValue) v).value);
247      else if (v instanceof JsonCanStringValue)
248        jj.value(((JsonCanStringValue) v).value);
249      else if (v instanceof JsonCanNullValue)
250        jj.nullValue();
251      else if (v instanceof JsonCanObject) {
252        JsonCanObject o = (JsonCanObject) v;
253        if (o.array) 
254          writeArray(o);
255        else
256          writeObject(o);
257      } else
258        throw new Error("not possible");
259    }
260    jj.endArray();    
261  }
262
263  @Override
264  public void comment(String content) {
265    // canonical JSON ignores comments    
266  }
267
268  @Override
269  public void link(String href) {
270    // not used
271  }
272       
273  @Override
274  public void anchor(String name) {
275    // not used
276  }
277
278  @Override
279  public void externalLink(String string) {
280    // not used
281  }
282       
283    
284}