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