001package org.hl7.fhir.utilities.json.parser; 002 003import java.io.File; 004import java.io.FileOutputStream; 005import java.io.IOException; 006import java.io.InputStream; 007import java.io.OutputStream; 008import java.nio.charset.StandardCharsets; 009import java.util.Arrays; 010import java.util.List; 011 012import org.hl7.fhir.utilities.FileUtilities; 013import org.hl7.fhir.utilities.Utilities; 014import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 015import org.hl7.fhir.utilities.http.HTTPResult; 016import org.hl7.fhir.utilities.http.ManagedWebAccess; 017import org.hl7.fhir.utilities.json.JsonException; 018import org.hl7.fhir.utilities.json.model.JsonArray; 019import org.hl7.fhir.utilities.json.model.JsonBoolean; 020import org.hl7.fhir.utilities.json.model.JsonComment; 021import org.hl7.fhir.utilities.json.model.JsonElement; 022import org.hl7.fhir.utilities.json.model.JsonElementType; 023import org.hl7.fhir.utilities.json.model.JsonLocationData; 024import org.hl7.fhir.utilities.json.model.JsonNull; 025import org.hl7.fhir.utilities.json.model.JsonNumber; 026import org.hl7.fhir.utilities.json.model.JsonObject; 027import org.hl7.fhir.utilities.json.model.JsonPrimitive; 028import org.hl7.fhir.utilities.json.model.JsonProperty; 029import org.hl7.fhir.utilities.json.model.JsonString; 030import org.hl7.fhir.utilities.json.parser.JsonLexer.State; 031import org.hl7.fhir.utilities.json.parser.JsonLexer.TokenType; 032 033/** 034 * Simple parser for JSON. This parser is not particularly quick (though it's not slow) 035 * The focus for this parser is to faithfully record the line/col number of json elements 036 * so that the FHIR validator can report issues by line number 037 * 038 * Also, for the validator, the parser will accept duplicate property names 039 * 040 * JSON5: When running in Json5 mode, the parser accepts 041 * * unquoted strings for both fields and values 042 * * missing commas in objects and arrays 043 * * comments - anything starting // will be processed as a comma to the end of the line 044 * 045 * Other JSON5 features might be added in the future 046 * 047 * The FHIR Validator uses this parser in Json5 mode, and the object model is marked up 048 * with deviations from base JSON spec so that the validator can record them as errors 049 * (this is better than blowing up parsing the JSON) 050 * 051 * @author grahamegrieve 052 * 053 */ 054public class JsonParser { 055 056 protected JsonParser() { 057 super(); 058 } 059 060 protected JsonParser(int line) { 061 super(); 062 this.line = line; 063 } 064 065 public static JsonObject parseObject(InputStream stream) throws IOException, JsonException { 066 return new JsonParser().parseJsonObject(FileUtilities.streamToString(stream), false, false); 067 } 068 069 public static JsonObject parseObject(byte[] content) throws IOException, JsonException { 070 return new JsonParser().parseJsonObject(FileUtilities.bytesToString(content), false, false); 071 } 072 073 public static JsonObject parseObject(String source) throws IOException, JsonException { 074 return new JsonParser().parseJsonObject(source, false, false); 075 } 076 077 public static JsonObject parseObject(File source) throws IOException, JsonException { 078 if (!source.exists()) { 079 throw new IOException("File "+source+" not found"); 080 } 081 return new JsonParser().setSourceName(source.getAbsolutePath()).parseJsonObject(FileUtilities.fileToString(source), false, false); 082 } 083 084 public static JsonObject parseObject(File source, boolean isJson5) throws IOException, JsonException { 085 if (!source.exists()) { 086 throw new IOException("File "+source+" not found"); 087 } 088 return new JsonParser().setSourceName(source.getAbsolutePath()).parseJsonObject(FileUtilities.fileToString(source), isJson5, false); 089 } 090 091 public static JsonObject parseObjectFromFile(String source) throws IOException, JsonException { 092 return new JsonParser().setSourceName(source).parseJsonObject(FileUtilities.fileToString(source), false, false); 093 } 094 095 public static JsonObject parseObjectFromUrl(String source) throws IOException, JsonException { 096 return new JsonParser().setSourceName(source).parseJsonObject(FileUtilities.bytesToString(fetch(source)), false, false); 097 } 098 099 public static JsonObject parseObject(InputStream stream, boolean isJson5) throws IOException, JsonException { 100 return new JsonParser().parseJsonObject(FileUtilities.streamToString(stream), isJson5, false); 101 } 102 103 public static JsonObject parseObject(byte[] content, boolean isJson5) throws IOException, JsonException { 104 return new JsonParser().parseJsonObject(FileUtilities.bytesToString(content), isJson5, false); 105 } 106 107 public static JsonObject parseObject(String source, boolean isJson5) throws IOException, JsonException { 108 return new JsonParser().parseJsonObject(source, isJson5, false); 109 } 110 111 public static JsonObject parseObjectFromUrl(String source, boolean isJson5) throws IOException, JsonException { 112 return new JsonParser().setSourceName(source).parseJsonObject(FileUtilities.bytesToString(fetch(source)), isJson5, false); 113 } 114 115 public static JsonObject parseObject(InputStream stream, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 116 return parseObject(FileUtilities.streamToString(stream), isJson5, allowDuplicates); 117 } 118 119 public static JsonObject parseObject(byte[] stream, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 120 return parseObject(FileUtilities.bytesToString(stream), isJson5, allowDuplicates); 121 } 122 123 public static JsonObject parseObject(String source, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 124 return new JsonParser().parseJsonObject(source, isJson5, allowDuplicates); 125 } 126 127 public static JsonObject parseObject(String source, boolean isJson5, boolean allowDuplicates, int line) throws IOException, JsonException { 128 return new JsonParser(line).parseJsonObject(source, isJson5, allowDuplicates); 129 } 130 131 // ================================================================ 132 133 public static JsonElement parse(InputStream stream) throws IOException, JsonException { 134 return parse(FileUtilities.streamToString(stream)); 135 } 136 137 public static JsonElement parse(byte[] stream) throws IOException, JsonException { 138 return parse(FileUtilities.bytesToString(stream)); 139 } 140 141 public static JsonElement parse(String source) throws IOException, JsonException { 142 return parse(source, false); 143 } 144 145 public static JsonElement parse(File source) throws IOException, JsonException { 146 return parse(FileUtilities.fileToString(source)); 147 } 148 149 public static JsonElement parseFromFile(String source) throws IOException, JsonException { 150 return parse(FileUtilities.fileToString(source)); 151 } 152 153 public static JsonElement parseFromUrl(String source) throws IOException, JsonException { 154 return parse(fetch(source)); 155 } 156 157 public static JsonElement parse(InputStream stream, boolean isJson5) throws IOException, JsonException { 158 return parse(FileUtilities.streamToString(stream), isJson5); 159 } 160 161 public static JsonElement parse(byte[] stream, boolean isJson5) throws IOException, JsonException { 162 return parse(FileUtilities.bytesToString(stream), isJson5); 163 } 164 165 public static JsonElement parse(String source, boolean isJson5) throws IOException, JsonException { 166 return parse(source, isJson5, false); 167 } 168 169 public static JsonElement parseFromUrl(String source, boolean isJson5) throws IOException, JsonException { 170 return parse(fetch(source), isJson5); 171 } 172 173 public static JsonElement parse(InputStream stream, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 174 return parse(FileUtilities.streamToString(stream), isJson5, allowDuplicates); 175 } 176 177 public static JsonElement parse(byte[] stream, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 178 return parse(FileUtilities.bytesToString(stream), isJson5, allowDuplicates); 179 } 180 181 public static JsonElement parse(String source, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 182 return new JsonParser().parseJsonElement(source, isJson5, allowDuplicates); 183 } 184 185 186 // ================================================================ 187 188 public static String compose(JsonElement element) { 189 return compose(element, false); 190 } 191 192 public static void compose(JsonElement element, OutputStream stream) throws IOException { 193 compose(element, stream, false); 194 } 195 196 public static void compose(JsonElement element, File file) throws IOException { 197 FileOutputStream fo = ManagedFileAccess.outStream(file); 198 compose(element, fo, false); 199 fo.close(); 200 } 201 202 public static byte[] composeBytes(JsonElement element) { 203 return composeBytes(element, false); 204 } 205 206 public static String compose(JsonElement element, boolean pretty) { 207 return new JsonParser().write(element, pretty); 208 } 209 210 public static void compose(JsonElement element, OutputStream stream, boolean pretty) throws IOException { 211 byte[] cnt = composeBytes(element, pretty); 212 stream.write(cnt); 213 } 214 public static void compose(JsonElement element, File file, boolean pretty) throws IOException { 215 byte[] cnt = composeBytes(element, pretty); 216 FileOutputStream fo = ManagedFileAccess.outStream(file); 217 fo.write(cnt); 218 fo.close(); 219 } 220 221 public static byte[] composeBytes(JsonElement element, boolean pretty) { 222 String s = compose(element, pretty); 223 return s.getBytes(StandardCharsets.UTF_8); 224 } 225 226 // ================================================================ 227 228 enum ItemType { 229 Object, String, Number, Boolean, Array, End, Eof, Null; 230 } 231 private JsonLexer lexer; 232 private ItemType itemType = ItemType.Object; 233 private String itemName; 234 private String itemValue; 235 private boolean allowDuplicates = true; 236 private boolean allowComments = false; 237 private boolean allowNoComma = false; 238 private JsonLocationData startProperty; 239 private JsonLocationData endProperty; 240 private boolean itemNoComma; 241 private boolean allowUnquotedStrings; 242 private boolean itemUnquoted; 243 private boolean valueUnquoted; 244 private String sourceName; 245 private int line = 0; 246 247 private JsonObject parseJsonObject(String source, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 248 this.allowDuplicates = allowDuplicates; 249 this.allowComments = isJson5; 250 this.allowNoComma = isJson5; 251 this.allowUnquotedStrings = isJson5; 252 return parseSource(Utilities.stripBOM(source)); 253 } 254 255 private JsonObject parseSource(String source) throws IOException, JsonException { 256 lexer = new JsonLexer(source, allowComments, allowUnquotedStrings, line); 257 lexer.setSourceName(sourceName); 258 JsonObject result = new JsonObject(); 259 lexer.takeComments(result); 260 result.setStart(lexer.getLastLocationAWS().copy()); 261 if (lexer.getType() == TokenType.Open) { 262 lexer.next(); 263 lexer.getStates().push(new State("", true)); 264 } 265 else if (lexer.getType() != null) { 266 throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString()); 267 } else { 268 throw lexer.error("Unexpected content at start of JSON"); 269 } 270 271 if (lexer.getType() != TokenType.Close) { 272 parseProperty(); 273 readObject("$", result, true); 274 result.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 275 } else { 276 result.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 277 lexer.next(); 278 } 279 if (lexer.getType() != TokenType.Eof) { 280 throw lexer.error("Unexpected content at end of JSON: "+lexer.getType().toString()); 281 } 282 return result; 283 } 284 285 private JsonElement parseJsonElement(String source, boolean isJson5, boolean allowDuplicates) throws IOException, JsonException { 286 this.allowDuplicates = allowDuplicates; 287 this.allowComments = isJson5; 288 this.allowNoComma = isJson5; 289 this.allowUnquotedStrings = isJson5; 290 return parseSourceElement(Utilities.stripBOM(source)); 291 } 292 293 private JsonElement parseSourceElement(String source) throws IOException, JsonException { 294 lexer = new JsonLexer(source, allowComments, allowUnquotedStrings, line); 295 switch (lexer.getType()) { 296 case Boolean: 297 JsonBoolean bool = new JsonBoolean(lexer.getValue().equals("true")); 298 lexer.takeComments(bool); 299 bool.setStart(lexer.getLastLocationAWS().copy()); 300 bool.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 301 return bool; 302 case Null: 303 JsonNull nll = new JsonNull(); 304 lexer.takeComments(nll); 305 nll.setStart(lexer.getLastLocationAWS().copy()); 306 nll.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 307 return nll; 308 case Number: 309 JsonNumber num = new JsonNumber(lexer.getValue()); 310 lexer.takeComments(num); 311 num.setStart(lexer.getLastLocationAWS().copy()); 312 num.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 313 return num; 314 case Open: 315 JsonObject obj = new JsonObject(); 316 lexer.takeComments(obj); 317 obj.setStart(lexer.getLastLocationAWS().copy()); 318 if (lexer.getType() == TokenType.Open) { 319 lexer.next(); 320 lexer.getStates().push(new State("", true)); 321 } 322 else 323 throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString()); 324 325 if (lexer.getType() != TokenType.Close) { 326 parseProperty(); 327 readObject("$", obj, true); 328 } 329 obj.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 330 return obj; 331 case OpenArray: 332 JsonArray arr = new JsonArray(); 333 lexer.takeComments(arr); 334 arr.setStart(lexer.getLastLocationAWS().copy()); 335 lexer.next(); 336 lexer.getStates().push(new State("", false)); 337 if (lexer.getType() != TokenType.CloseArray) { 338 parseProperty(); 339 readArray("$", arr, true); 340 } 341 arr.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 342 return arr; 343 case String: 344 JsonString str = new JsonString(lexer.getValue()); 345 lexer.takeComments(str); 346 str.setStart(lexer.getLastLocationAWS().copy()); 347 str.setEnd(endProperty != null ? endProperty.copy() : lexer.getLocation().copy()); 348 return str; 349 default: 350 } 351 throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString()); 352 } 353 354 private void readObject(String path, JsonObject obj, boolean root) throws IOException, JsonException { 355 while (!(itemType == ItemType.End) || (root && (itemType == ItemType.Eof))) { 356 obj.setExtraComma(false); 357 switch (itemType) { 358 case Object: 359 JsonObject child = new JsonObject(); //(obj.path+'.'+ItemName); 360 child.setStart(startProperty.copy()); 361 lexer.takeComments(child); 362 if (obj.has(itemName) && !allowDuplicates) 363 throw lexer.error("Duplicated property name: "+itemName+ " @ "+path); 364 obj.addForParser(itemName, child, itemNoComma, itemUnquoted, valueUnquoted); 365 next(); 366 readObject(path+"."+itemName, child, false); 367 child.setEnd(endProperty.copy()); 368 break; 369 case Boolean : 370 JsonBoolean childB = new JsonBoolean(Boolean.valueOf(itemValue)); 371 childB.setStart(startProperty.copy()); 372 lexer.takeComments(childB); 373 if (obj.has(itemName) && !allowDuplicates) 374 throw lexer.error("Duplicated property name: "+itemName+ " @ "+path); 375 obj.addForParser(itemName, childB, itemNoComma, itemUnquoted, valueUnquoted); 376 childB.setEnd(endProperty.copy()); 377 break; 378 case String: 379 JsonString childS = new JsonString(itemValue); 380 childS.setStart(startProperty.copy()); 381 lexer.takeComments(childS); 382 if (obj.has(itemName) && !allowDuplicates) 383 throw lexer.error("Duplicated property name: "+itemName+ " @ "+path); 384 obj.addForParser(itemName, childS, itemNoComma, itemUnquoted, valueUnquoted); 385 childS.setEnd(endProperty.copy()); 386 break; 387 case Number: 388 JsonNumber childN = new JsonNumber(itemValue); 389 childN.setStart(startProperty.copy()); 390 lexer.takeComments(childN); 391 if (obj.has(itemName) && !allowDuplicates) 392 throw lexer.error("Duplicated property name: "+itemName+ " @ "+path); 393 obj.addForParser(itemName, childN, itemNoComma, itemUnquoted, valueUnquoted); 394 childN.setEnd(endProperty.copy()); 395 break; 396 case Null: 397 JsonNull childn = new JsonNull(); 398 childn.setStart(startProperty.copy()); 399 lexer.takeComments(childn); 400 if (obj.has(itemName) && !allowDuplicates) 401 throw lexer.error("Duplicated property name: "+itemName+ " @ "+path); 402 obj.addForParser(itemName, childn, itemNoComma, itemUnquoted, valueUnquoted); 403 childn.setEnd(endProperty.copy()); 404 break; 405 case Array: 406 JsonArray childA = new JsonArray(); // (obj.path+'.'+ItemName); 407 childA.setStart(startProperty.copy()); 408 lexer.takeComments(childA); 409 if (obj.has(itemName) && !allowDuplicates) 410 throw lexer.error("Duplicated property name: "+itemName+ " @ "+path); 411 obj.addForParser(itemName, childA, itemNoComma, itemUnquoted, valueUnquoted); 412 next(); 413 if (!readArray(path+"."+itemName, childA, false)) 414 next(true); 415 if (childA.getEnd() == null) { 416 childA.setEnd(endProperty.copy()); 417 } 418 break; 419 case Eof : 420 throw lexer.error("Unexpected End of File"); 421 case End: 422 throw lexer.error("Unexpected End"); // Don't think we can get here 423 } 424 itemNoComma = false; 425 endProperty = lexer.getLocation().copy(); 426 obj.setExtraComma(lexer.getType() == TokenType.Comma); 427 next(); 428 } 429 } 430 431 private boolean readArray(String path, JsonArray arr, boolean root) throws IOException, JsonException { 432 boolean res = false; 433 while (!((itemType == ItemType.End) || (root && (itemType == ItemType.Eof)))) { 434 res = true; 435 arr.setExtraComma(false); 436 switch (itemType) { 437 case Object: 438 JsonObject obj = new JsonObject(); // (arr.path+'['+inttostr(i)+']'); 439 obj.setStart(startProperty.copy()); 440 lexer.takeComments(obj); 441 arr.addForParser(obj, itemNoComma, valueUnquoted); 442 next(); 443 readObject(path+"["+(arr.size()-1)+"]", obj, false); 444 obj.setEnd(endProperty.copy()); 445 break; 446 case String: 447 JsonString s = new JsonString(itemValue); 448 s.setStart(startProperty.copy()); 449 lexer.takeComments(s); 450 arr.addForParser(s, itemNoComma, valueUnquoted); 451 s.setEnd(endProperty.copy()); 452 break; 453 case Number: 454 JsonNumber n = new JsonNumber(itemValue); 455 n.setStart(startProperty.copy()); 456 lexer.takeComments(n); 457 arr.addForParser(n, itemNoComma, valueUnquoted); 458 n.setEnd(endProperty.copy()); 459 break; 460 case Boolean: 461 JsonBoolean b = new JsonBoolean("true".equals(itemValue)); 462 b.setStart(startProperty.copy()); 463 lexer.takeComments(b); 464 arr.addForParser(b, itemNoComma, valueUnquoted); 465 b.setEnd(endProperty.copy()); 466 break; 467 case Null : 468 JsonNull nn = new JsonNull(); 469 nn.setStart(startProperty.copy()); 470 lexer.takeComments(nn); 471 arr.addForParser(nn, itemNoComma, valueUnquoted); 472 nn.setEnd(endProperty.copy()); 473 break; 474 case Array: 475 JsonArray child = new JsonArray(); // (arr.path+'['+inttostr(i)+']'); 476 child.setStart(startProperty.copy()); 477 lexer.takeComments(child); 478 arr.addForParser(child, itemNoComma, valueUnquoted); 479 next(); 480 readArray(path+"["+(arr.size()-1)+"]", child, false); 481 child.setEnd(endProperty.copy()); 482 break; 483 case Eof : 484 throw lexer.error("Unexpected End of File"); 485 case End: 486 throw lexer.error("Can't get here"); 487 } 488 itemNoComma = false; 489 arr.setEnd(lexer.getLocation().copy()); 490 arr.setExtraComma(lexer.getType() == TokenType.Comma); 491 next(); 492 } 493 return res; 494 } 495 496 private void next() throws IOException { 497 next(false); 498 } 499 500 private void next(boolean noPop) throws IOException { 501 switch (itemType) { 502 case Object : 503 lexer.consume(TokenType.Open); 504 lexer.getStates().push(new State(itemName, true)); 505 if (lexer.getType() == TokenType.Close) { 506 itemType = ItemType.End; 507 lexer.next(); 508 } else 509 parseProperty(); 510 break; 511 case Null: 512 case String: 513 case Number: 514 case End: 515 case Boolean : 516 if (itemType == ItemType.End && !noPop) 517 lexer.getStates().pop(); 518 if (lexer.getType() == TokenType.Comma) { 519 lexer.next(); 520 if (allowNoComma && (lexer.getType() == TokenType.CloseArray || lexer.getType() == TokenType.Close)) { 521 itemType = ItemType.End; 522 lexer.next(); 523 } else { 524 parseProperty(); 525 } 526 } else if (lexer.getType() == TokenType.Close) { 527 itemType = ItemType.End; 528 lexer.next(); 529 } else if (lexer.getType() == TokenType.CloseArray) { 530 itemType = ItemType.End; 531 lexer.next(); 532 } else if (lexer.getType() == TokenType.Eof) { 533 itemType = ItemType.Eof; 534 } else if (allowNoComma && (lexer.getType() == TokenType.String || (!lexer.getStates().peek().isProp()) && lexer.getType().isValueType())) { 535 itemNoComma = true; 536 parseProperty(); 537 } else { 538 throw lexer.error("Unexpected JSON syntax"); 539 } 540 break; 541 case Array : 542 lexer.next(); 543 lexer.getStates().push(new State(itemName+"[]", false)); 544 parseProperty(); 545 break; 546 case Eof : 547 throw lexer.error("JSON Syntax Error - attempt to read past end of json stream"); 548 default: 549 throw lexer.error("not done yet (a): "+itemType.toString()); 550 } 551 } 552 553 private void parseProperty() throws IOException { 554 if (lexer.getStates().peek().isProp()) { 555 itemUnquoted = lexer.isUnquoted(); 556 itemName = lexer.consume(TokenType.String); 557 itemValue = null; 558 lexer.consume(TokenType.Colon); 559 } 560 startProperty = lexer.getLastLocationAWS().copy(); 561 endProperty = lexer.getLocation().copy(); 562 valueUnquoted = lexer.isUnquoted(); 563 switch (lexer.getType()) { 564 case Null : 565 itemType = ItemType.Null; 566 itemValue = lexer.getValue(); 567 lexer.next(); 568 break; 569 case String : 570 itemType = ItemType.String; 571 itemValue = lexer.getValue(); 572 lexer.next(); 573 break; 574 case Boolean : 575 itemType = ItemType.Boolean; 576 itemValue = lexer.getValue(); 577 lexer.next(); 578 break; 579 case Number : 580 itemType = ItemType.Number; 581 itemValue = lexer.getValue(); 582 lexer.next(); 583 break; 584 case Open : 585 itemType = ItemType.Object; 586 break; 587 case OpenArray : 588 itemType = ItemType.Array; 589 break; 590 case CloseArray : 591 itemType = ItemType.End; 592 break; 593 // case Close, , case Colon, case Comma, case OpenArray, ! 594 default: 595 throw lexer.error("not done yet (b): "+lexer.getType().toString()); 596 } 597 } 598 599 private String write(JsonElement element, boolean pretty) { 600 StringBuilder b = new StringBuilder(); 601 if (pretty && element.hasComments()) { 602 writeComments(b, element.getComments(), 0); 603 } 604 write(b, element, pretty, 0); 605 if (pretty) { 606 b.append("\n"); 607 } 608 return b.toString(); 609 } 610 611 private void writeComments(StringBuilder b, List<JsonComment> comments, int indent) { 612 for (JsonComment s : comments) { 613 b.append("// "); 614 b.append(s.getContent()); 615 b.append("\n"); 616 b.append(Utilities.padLeft("", ' ', indent)); 617 } 618 } 619 620 private void write(StringBuilder b, JsonElement e, boolean pretty, int indent) { 621 switch (e.type()) { 622 case ARRAY: 623 JsonArray arr = (JsonArray) e; 624 b.append("["); 625 boolean first = true; 626 boolean complex = arr.size() > 6; // arbitrary cut off 627 if (!complex) { 628 int length = 0; 629 for (JsonElement i : arr.getItems()) { 630 if (i instanceof JsonPrimitive) { 631 length = length + ((JsonPrimitive)i).toJson().length(); 632 } 633 if (i.type() == JsonElementType.ARRAY || i.type() == JsonElementType.OBJECT 634 || i.hasComments()) { // 20 is a somewhat arbitrary cut off 635 complex = true; 636 } 637 } 638 if (length > 60) { 639 complex = true; 640 } 641 } 642 for (JsonElement i : arr.getItems()) { 643 if (first) first = false; else b.append(pretty && !complex ? ", " : ","); 644 if (pretty && complex) { 645 b.append("\n"); 646 b.append(Utilities.padLeft("", ' ', indent+2)); 647 if (i.hasComments()) { 648 writeComments(b, i.getComments(), indent+2); 649 } 650 } 651 write(b, i, pretty && complex, indent+2); 652 } 653 if (pretty && complex) { 654 b.append("\n"); 655 b.append(Utilities.padLeft("", ' ', indent)); 656 } 657 b.append("]"); 658 break; 659 case BOOLEAN: 660 b.append(((JsonBoolean) e).getValue()); 661 break; 662 case NULL: 663 b.append(((JsonNull) e).getValue()); 664 break; 665 case NUMBER: 666 b.append(((JsonNumber) e).getValue()); 667 break; 668 case OBJECT: 669 b.append("{"); 670 first = true; 671 for (JsonProperty p : ((JsonObject) e).getProperties()) { 672 if (first) first = false; else b.append(","); 673 if (pretty) { 674 b.append("\n"); 675 b.append(Utilities.padLeft("", ' ', indent+2)); 676 if (p.getValue().hasComments()) { 677 writeComments(b, p.getValue().getComments(), indent+2); 678 } 679 } 680 b.append("\""); 681 b.append(p.getName()); 682 b.append(pretty ? "\" : " : "\":"); 683 write(b, p.getValue(), pretty, indent+2); 684 } 685 if (pretty) { 686 b.append("\n"); 687 b.append(Utilities.padLeft("", ' ', indent)); 688 } 689 b.append("}"); 690 break; 691 case STRING: 692 b.append("\""); 693 b.append(Utilities.escapeJson(((JsonString) e).getValue(), false)); 694 b.append("\""); 695 break; 696 default: 697 throw new Error("Can't get here"); 698 } 699 } 700 701 private static byte[] fetch(String source) throws IOException { 702 String murl = source.contains("?") ? source+"&nocache=" + System.currentTimeMillis() : source+"?nocache=" + System.currentTimeMillis(); 703 HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), murl, "application/json, application/fhir+json"); 704 res.checkThrowException(); 705 return res.getContent(); 706 } 707 708 public String getSourceName() { 709 return sourceName; 710 } 711 712 public JsonParser setSourceName(String sourceName) { 713 this.sourceName = sourceName; 714 return this; 715 } 716 717 718}