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