001package org.hl7.fhir.utilities.json;
002
003import java.io.File;
004
005/*
006  Copyright (c) 2011+, HL7, Inc.
007  All rights reserved.
008  
009  Redistribution and use in source and binary forms, with or without modification, 
010  are permitted provided that the following conditions are met:
011    
012   * Redistributions of source code must retain the above copyright notice, this 
013     list of conditions and the following disclaimer.
014   * Redistributions in binary form must reproduce the above copyright notice, 
015     this list of conditions and the following disclaimer in the documentation 
016     and/or other materials provided with the distribution.
017   * Neither the name of HL7 nor the names of its contributors may be used to 
018     endorse or promote products derived from this software without specific 
019     prior written permission.
020  
021  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
022  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
023  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
024  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
025  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
026  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
027  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
028  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
029  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
030  POSSIBILITY OF SUCH DAMAGE.
031  
032 */
033
034
035
036import java.io.IOException;
037import java.io.InputStream;
038import java.math.BigDecimal;
039import java.nio.charset.StandardCharsets;
040import java.util.Map;
041import java.util.Stack;
042
043import org.hl7.fhir.utilities.TextFile;
044import org.hl7.fhir.utilities.Utilities;
045import org.hl7.fhir.utilities.http.HTTPResult;
046import org.hl7.fhir.utilities.http.ManagedWebAccess;
047
048import com.google.gson.Gson;
049import com.google.gson.GsonBuilder;
050import com.google.gson.JsonArray;
051import com.google.gson.JsonElement;
052import com.google.gson.JsonNull;
053import com.google.gson.JsonObject;
054import com.google.gson.JsonPrimitive;
055
056
057/**
058 * This is created to get a json parser that can track line numbers... grr...
059 * 
060 * @author Grahame Grieve
061 *
062 */
063public class JsonTrackingParser {
064
065        public class PresentedBigDecimal extends BigDecimal {
066
067          public String presentation;
068          
069    public PresentedBigDecimal(String value) {
070      super(value);
071      presentation = value;
072    }
073
074    public String getPresentation() {
075      return presentation;
076    }
077
078  }
079
080  public enum TokenType {
081                Open, Close, String, Number, Colon, Comma, OpenArray, CloseArray, Eof, Null, Boolean;
082        }
083        
084        public class LocationData {
085                private int line;
086                private int col;
087                
088                protected LocationData(int line, int col) {
089                        super();
090                        this.line = line;
091                        this.col = col;
092                }
093                
094                public int getLine() {
095                        return line;
096                }
097                
098                public int getCol() {
099                        return col;
100                }
101                
102                public void newLine() {
103                        line++;
104                        col = 1;                
105                }
106
107                public LocationData copy() {
108                        return new LocationData(line, col);
109                }
110        }
111        
112        private class State {
113                private String name;
114                private boolean isProp;
115                protected State(String name, boolean isProp) {
116                        super();
117                        this.name = name;
118                        this.isProp = isProp;
119                }
120                public String getName() {
121                        return name;
122                }
123                public boolean isProp() {
124                        return isProp;
125                }
126        }
127        
128        private class Lexer {
129                private String source;
130                private int cursor;
131                private String peek;
132                private String value;
133                private TokenType type;
134                private Stack<State> states = new Stack<State>();
135                private LocationData lastLocationBWS;
136                private LocationData lastLocationAWS;
137                private LocationData location;
138                private StringBuilder b = new StringBuilder();
139                
140    public Lexer(String source) throws IOException {
141        this.source = source;
142        cursor = -1;
143        location = new LocationData(1, 1);  
144        start();
145    }
146    
147    private boolean more() {
148        return peek != null || cursor < source.length(); 
149    }
150    
151    private String getNext(int length) throws IOException {
152        String result = "";
153      if (peek != null) {
154        if (peek.length() > length) {
155                result = peek.substring(0, length);
156                peek = peek.substring(length);
157        } else {
158                result = peek;
159                peek = null;
160        }
161      }
162      if (result.length() < length) {
163        int len = length - result.length(); 
164        if (cursor > source.length() - len) 
165                throw error("Attempt to read past end of source");
166        result = result + source.substring(cursor+1, cursor+len+1);
167        cursor = cursor + len;
168      }
169       for (char ch : result.toCharArray())
170        if (ch == '\n')
171          location.newLine();
172        else
173          location.col++;
174      return result;
175    }
176    
177    private char getNextChar() throws IOException {
178      if (peek != null) {
179        char ch = peek.charAt(0);
180        peek = peek.length() == 1 ? null : peek.substring(1);
181        return ch;
182      } else {
183        cursor++;
184        if (cursor >= source.length())
185          return (char) 0;
186        char ch = source.charAt(cursor);
187        if (ch == '\n') {
188          location.newLine();
189        } else {
190          location.col++;
191        }
192        return ch;
193      }
194    }
195    
196    private void push(char ch){
197        peek = peek == null ? String.valueOf(ch) : String.valueOf(ch)+peek;
198    }
199    
200    private void parseWord(String word, char ch, TokenType type) throws IOException {
201      this.type = type;
202      value = ""+ch+getNext(word.length()-1);
203      if (!value.equals(word))
204        throw error("Syntax error in json reading special word "+word);
205    }
206    
207    private IOException error(String msg) {
208      return new IOException("Error parsing JSON source: "+msg+" at Line "+Integer.toString(location.line)+" (path=["+path()+"])");
209    }
210    
211    private String path() {
212      if (states.empty())
213        return value;
214      else {
215        String result = "";
216        for (State s : states) 
217          result = result + '/'+ s.getName();
218        result = result + value;
219        return result;
220      }
221    }
222
223    public void start() throws IOException {
224//      char ch = getNextChar();
225//      if (ch = '\.uEF')
226//      begin
227//        // skip BOM
228//        getNextChar();
229//        getNextChar();
230//      end
231//      else
232//        push(ch);
233      next();
234    }
235    
236    public TokenType getType() {
237        return type;
238    }
239    
240    public String getValue() {
241        return value;
242    }
243
244
245    public LocationData getLastLocationBWS() {
246        return lastLocationBWS;
247    }
248
249    public LocationData getLastLocationAWS() {
250        return lastLocationAWS;
251    }
252
253    public void next() throws IOException {
254        lastLocationBWS = location.copy();
255        char ch;
256        do {
257                ch = getNextChar();
258                if (allowComments && ch == '/') {
259                  char ch1 = getNextChar();
260                  if (ch1 == '/') {
261                    while (more() && !Utilities.charInSet(ch, '\r', '\n')) {
262                      ch = getNextChar();
263                    }
264                  } else {
265                    push(ch1);
266                  }               
267                }
268        } while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
269        lastLocationAWS = location.copy();
270
271        if (!more()) {
272                type = TokenType.Eof;
273        } else {
274                switch (ch) {
275                case '{' : 
276                        type = TokenType.Open;
277                        break;
278                case '}' : 
279                        type = TokenType.Close;
280                        break;
281                case '"' :
282                        type = TokenType.String;
283                        b.setLength(0);
284                        do {
285                                ch = getNextChar();
286                                if (ch == '\\') {
287                                        ch = getNextChar();
288                                        switch (ch) {
289              case '"': b.append('"'); break;
290              case '\'': b.append('\''); break;
291                                        case '\\': b.append('\\'); break;
292                                        case '/': b.append('/'); break;
293                                        case 'n': b.append('\n'); break;
294                                        case 'r': b.append('\r'); break;
295                                        case 't': b.append('\t'); break;
296                                        case 'u': b.append((char) Integer.parseInt(getNext(4), 16)); break;
297                                        default :
298                                                throw error("unknown escape sequence: \\"+ch);
299                                        }
300                                        ch = ' ';
301                                } else if (ch != '"')
302                                        b.append(ch);
303                        } while (more() && (ch != '"'));
304                        if (!more())
305                                throw error("premature termination of json stream during a string");
306                        value = b.toString();
307                        break;
308                case ':' : 
309                        type = TokenType.Colon;
310                        break;
311                case ',' : 
312                        type = TokenType.Comma;
313                        break;
314                case '[' : 
315                        type = TokenType.OpenArray;
316                        break;
317                case ']' : 
318                        type = TokenType.CloseArray;
319                        break;
320                case 't' : 
321                        parseWord("true", ch, TokenType.Boolean);
322                        break;
323                case 'f' : 
324                        parseWord("false", ch, TokenType.Boolean);
325                        break;
326                case 'n' : 
327                        parseWord("null", ch, TokenType.Null);
328                        break;
329                default:
330                        if ((ch >= '0' && ch <= '9') || ch == '-') {
331                                type = TokenType.Number;
332                                b.setLength(0);
333                                while (more() && ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.') || ch == '+' || ch == 'e' || ch == 'E') {
334                                        b.append(ch);
335                                        ch = getNextChar();
336                                }
337                                value = b.toString();
338                                push(ch);
339                        } else
340                                throw error("Unexpected char '"+ch+"' in json stream");
341                }
342        }
343    }
344
345    public String consume(TokenType type) throws IOException {
346      if (this.type != type)
347        throw error("JSON syntax error - found "+this.type.toString()+" expecting "+type.toString());
348      String result = value;
349      next();
350      return result;
351    }
352
353        }
354
355        enum ItemType {
356          Object, String, Number, Boolean, Array, End, Eof, Null;
357        }
358        private Map<JsonElement, LocationData> map;
359  private Lexer lexer;
360  private ItemType itemType = ItemType.Object;
361  private String itemName;
362  private String itemValue;
363  private boolean errorOnDuplicates = true;
364  private boolean allowComments = false;
365
366  public static JsonObject parseJson(String source) throws IOException {
367    return parse(source, null);
368  }
369  
370  public static JsonObject parseJson(InputStream stream) throws IOException {
371    return parse(TextFile.streamToString(stream), null);
372  }
373  
374  public static JsonObject parseJson(byte[] stream) throws IOException {
375    return parse(TextFile.bytesToString(stream), null);
376  }
377  
378  public static JsonArray parseJsonArray(byte[] stream) throws IOException {
379    return parseArray(TextFile.bytesToString(stream), null);
380  }
381  
382  public static JsonObject parseJson(byte[] stream, boolean allowDuplicates) throws IOException {
383    return parse(TextFile.bytesToString(stream), null, allowDuplicates);
384  }
385  
386  public static JsonObject parseJson(File source) throws IOException {
387    return parse(TextFile.fileToString(source), null);
388  }
389  
390  public static JsonObject parseJsonFile(String source) throws IOException {
391    return parse(TextFile.fileToString(source), null);
392  }
393  
394  public static JsonObject parse(String source, Map<JsonElement, LocationData> map) throws IOException {
395    return parse(source, map, false);
396  }
397    
398  public static JsonArray parseArray(String source, Map<JsonElement, LocationData> map) throws IOException {
399    return parseArray(source, map, false);
400  }
401    
402    
403  public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
404    return parse(source, map, allowDuplicates, false);
405  }
406  
407  public static JsonArray parseArray(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
408    return parseArray(source, map, allowDuplicates, false);
409  }
410  
411  public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates, boolean allowComments) throws IOException {
412    JsonTrackingParser self = new JsonTrackingParser();
413    self.map = map;
414    self.setErrorOnDuplicates(!allowDuplicates);
415    self.setAllowComments(allowComments);
416    return self.parse(Utilities.stripBOM(source));
417  }
418
419  public static JsonArray parseArray(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates, boolean allowComments) throws IOException {
420    JsonTrackingParser self = new JsonTrackingParser();
421    self.map = map;
422    self.setErrorOnDuplicates(!allowDuplicates);
423    self.setAllowComments(allowComments);
424    return self.parseArray(Utilities.stripBOM(source));
425  }
426
427        private JsonObject parse(String source) throws IOException {
428                lexer = new Lexer(source);
429                JsonObject result = new JsonObject();
430                LocationData loc = lexer.location.copy();
431    if (lexer.getType() == TokenType.Open) {
432      lexer.next();
433      lexer.states.push(new State("", false));
434    } 
435    else
436      throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString());
437
438    if (lexer.getType() != TokenType.Close) {
439      parseProperty();
440      readObject(result, true);
441    }
442    if (map != null)
443                  map.put(result, loc);
444    return result;
445        }
446
447  private JsonArray parseArray(String source) throws IOException {
448    return new Gson().fromJson(source, JsonArray.class);
449  }
450
451        private void readObject(JsonObject obj, boolean root) throws IOException {
452          if (map != null)
453      map.put(obj, lexer.location.copy());
454
455                while (!(itemType == ItemType.End) || (root && (itemType == ItemType.Eof))) {
456                        switch (itemType) {
457                        case Object:
458                                JsonObject child = new JsonObject(); //(obj.path+'.'+ItemName);
459                                LocationData loc = lexer.location.copy();
460              if (!obj.has(itemName))
461                obj.add(itemName, child);
462              else if (errorOnDuplicates)
463                throw lexer.error("Duplicated property name: "+itemName);
464                                next();
465                                readObject(child, false);
466                                if (map != null)
467                      map.put(obj, loc);
468                                break;
469                        case Boolean :
470                                JsonPrimitive v = new JsonPrimitive(Boolean.valueOf(itemValue));
471        if (!obj.has(itemName))
472                                obj.add(itemName, v);
473        else if (errorOnDuplicates)
474          throw lexer.error("Duplicated property name: "+itemName);
475                                if (map != null)
476                      map.put(v, lexer.location.copy());
477                                break;
478                        case String:
479                                v = new JsonPrimitive(itemValue);
480        if (!obj.has(itemName))
481                                obj.add(itemName, v);
482        else if (errorOnDuplicates)
483          throw lexer.error("Duplicated property name: "+itemName);
484                                if (map != null)
485                      map.put(v, lexer.location.copy());
486                                break;
487                        case Number:
488                                v = new JsonPrimitive(new PresentedBigDecimal(itemValue));
489        if (!obj.has(itemName))
490                                obj.add(itemName, v);
491        else if (errorOnDuplicates)
492          throw lexer.error("Duplicated property name: "+itemName);
493                                if (map != null)
494                      map.put(v, lexer.location.copy());
495                                break;
496                        case Null:
497                                JsonNull n = new JsonNull();
498        if (!obj.has(itemName))
499                                obj.add(itemName, n);
500        else if (errorOnDuplicates)
501          throw lexer.error("Duplicated property name: "+itemName);
502                                if (map != null)
503                      map.put(n, lexer.location.copy());
504                                break;
505                        case Array:
506                                JsonArray arr = new JsonArray(); // (obj.path+'.'+ItemName);
507                                loc = lexer.location.copy();
508        if (!obj.has(itemName))
509                                obj.add(itemName, arr);
510        else if (errorOnDuplicates)
511          throw lexer.error("Duplicated property name: "+itemName);
512                                next();
513                                if (!readArray(arr, false))
514                                  next(true);
515                                if (map != null)
516                      map.put(arr, loc);
517                                break;
518                        case Eof : 
519                                throw lexer.error("Unexpected End of File");
520                        case End:
521                                // TODO GG: This isn't handled. Should it be?
522                                break;
523                        }
524                        next();
525                }
526        }
527
528        private boolean readArray(JsonArray arr, boolean root) throws IOException {
529          boolean res = false;
530          while (!((itemType == ItemType.End) || (root && (itemType == ItemType.Eof)))) {
531            res  = true;
532            switch (itemType) {
533            case Object:
534                JsonObject obj  = new JsonObject(); // (arr.path+'['+inttostr(i)+']');
535                                LocationData loc = lexer.location.copy();
536                arr.add(obj);
537              next();
538              readObject(obj, false);
539              if (map != null)
540                map.put(obj, loc);
541              break;
542            case String:
543                JsonPrimitive v = new JsonPrimitive(itemValue);
544                                arr.add(v);
545                                if (map != null)
546                      map.put(v, lexer.location.copy());
547                                break;
548            case Number:
549                v = new JsonPrimitive(new BigDecimal(itemValue));
550                                arr.add(v);
551                                if (map != null)
552                      map.put(v, lexer.location.copy());
553                                break;
554            case Null :
555                JsonNull n = new JsonNull();
556                                arr.add(n);
557                                if (map != null)
558                      map.put(n, lexer.location.copy());
559                                break;
560            case Array:
561        JsonArray child = new JsonArray(); // (arr.path+'['+inttostr(i)+']');
562                                loc = lexer.location.copy();
563                                arr.add(child);
564        next();
565              readArray(child, false);
566              if (map != null)
567                map.put(arr, loc);
568        break;
569            case Eof : 
570                throw lexer.error("Unexpected End of File");
571                 case End:
572                 case Boolean:
573                        // TODO GG: These aren't handled. SHould they be?
574                        break;
575            }
576            next();
577          }
578          return res;
579        }
580
581  private void next() throws IOException {
582    next(false);
583  }
584  
585        private void next(boolean noPop) throws IOException {
586                switch (itemType) {
587                case Object :
588                        lexer.consume(TokenType.Open);
589                        lexer.states.push(new State(itemName, false));
590                        if (lexer.getType() == TokenType.Close) {
591                                itemType = ItemType.End;
592                                lexer.next();
593                        } else
594                                parseProperty();
595                        break;
596                case Null:
597                case String:
598                case Number: 
599                case End: 
600                case Boolean :
601                        if (itemType == ItemType.End && !noPop)
602                                lexer.states.pop();
603                        if (lexer.getType() == TokenType.Comma) {
604                                lexer.next();
605                                parseProperty();
606                        } else if (lexer.getType() == TokenType.Close) {
607                                itemType = ItemType.End;
608                                lexer.next();
609                        } else if (lexer.getType() == TokenType.CloseArray) {
610                                itemType = ItemType.End;
611                                lexer.next();
612                        } else if (lexer.getType() == TokenType.Eof) {
613                                itemType = ItemType.Eof;
614                        } else
615                                throw lexer.error("Unexpected JSON syntax");
616                        break;
617                case Array :
618                        lexer.next();
619                        lexer.states.push(new State(itemName+"[]", true));
620                        parseProperty();
621                        break;
622                case Eof :
623                        throw lexer.error("JSON Syntax Error - attempt to read past end of json stream");
624                default:
625                        throw lexer.error("not done yet (a): "+itemType.toString());
626                }
627        }
628
629        private void parseProperty() throws IOException {
630                if (!lexer.states.peek().isProp) {
631                        itemName = lexer.consume(TokenType.String);
632                        itemValue = null;
633                        lexer.consume(TokenType.Colon);
634                }
635                switch (lexer.getType()) {
636                case Null :
637                        itemType = ItemType.Null;
638                        itemValue = lexer.value;
639                        lexer.next();
640                        break;
641                case String :
642                        itemType = ItemType.String;
643                        itemValue = lexer.value;
644                        lexer.next();
645                        break;
646                case Boolean :
647                        itemType = ItemType.Boolean;
648                        itemValue = lexer.value;
649                        lexer.next();
650                        break;
651                case Number :
652                        itemType = ItemType.Number;
653                        itemValue = lexer.value;
654                        lexer.next();
655                        break;
656                case Open :
657                        itemType = ItemType.Object;
658                        break;
659                case OpenArray :
660                        itemType = ItemType.Array;
661                        break;
662                case CloseArray :
663                        itemType = ItemType.End;
664                        break;
665                        // case Close, , case Colon, case Comma, case OpenArray,       !
666                default:
667                        throw lexer.error("not done yet (b): "+lexer.getType().toString());
668                }
669        }
670
671  public boolean isErrorOnDuplicates() {
672    return errorOnDuplicates;
673  }
674
675  public void setErrorOnDuplicates(boolean errorOnDuplicates) {
676    this.errorOnDuplicates = errorOnDuplicates;
677  }
678
679  
680  public boolean isAllowComments() {
681    return allowComments;
682  }
683
684  public void setAllowComments(boolean allowComments) {
685    this.allowComments = allowComments;
686  }
687
688  public static void write(JsonObject json, File file) throws IOException {
689    Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
690    String jcnt = gson.toJson(json);
691    TextFile.stringToFile(jcnt, file);    
692  }
693    
694  public static void write(JsonObject json, File file, boolean pretty) throws IOException {
695    Gson gson = pretty ? new GsonBuilder().setPrettyPrinting().create() : new GsonBuilder().create();
696    String jcnt = gson.toJson(json);
697    TextFile.stringToFile(jcnt, file);    
698  }
699    
700  public static void write(JsonObject json, String fileName) throws IOException {
701    Gson gson = new GsonBuilder().setPrettyPrinting().create();
702    String jcnt = gson.toJson(json);
703    TextFile.stringToFile(jcnt, fileName);    
704  }
705  
706  public static String write(JsonObject json) {
707    Gson gson = new GsonBuilder().setPrettyPrinting().create();
708    return gson.toJson(json);    
709  }
710
711  public static String writeDense(JsonObject json) {
712    Gson gson = new GsonBuilder().create();
713    return gson.toJson(json);    
714  }
715
716  public static byte[] writeBytes(JsonObject json, boolean pretty) {
717    if (pretty) {
718      Gson gson = new GsonBuilder().setPrettyPrinting().create();
719      return gson.toJson(json).getBytes(StandardCharsets.UTF_8);    
720    } else {
721      Gson gson = new GsonBuilder().create();
722      return gson.toJson(json).getBytes(StandardCharsets.UTF_8);    
723    }    
724  }
725  
726  public static JsonObject fetchJson(String source) throws IOException {
727    HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
728    res.checkThrowException();
729    return parseJson(res.getContent());
730  }
731  
732  public static JsonArray fetchJsonArray(String source) throws IOException {
733    HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
734    res.checkThrowException();
735    return parseJsonArray(res.getContent());
736  }
737        
738}