1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /*
  26  * This is not really json in the state it is now.
  27  * Some differences:
  28  * - Double quotes around the key in an object is not enforced.
  29  *     i.e you can write: { foo : "bar" } instead of { "foo" : "bar" }.
  30  * - Comments are allowed.
  31  * - The last element in an object or array can have an ending comma.
  32  */
  33 
  34 #include "precompiled.hpp"
  35 #include "utilities/json.hpp"
  36 #include <math.h>
  37 
  38 #if defined(__APPLE__) || defined(_WINDOWS) || defined(SOLARIS)
  39 // strchrnul not available on osx
  40 const char* strchrnul(const char *s, int c) {
  41   const char* tmp = strchr(s, c);
  42   return tmp == NULL ? s + strlen(s) : tmp;
  43 }
  44 #endif
  45 
  46 JSON::JSON(const char* text, bool silent)
  47 : start(text), pos(text), mark(text),
  48   level(0), line(1), column(0), silent(silent), _valid(true)
  49 {
  50 }
  51 
  52 void JSON::parse() {
  53   assert(start != NULL, "Need something to parse");
  54   if (start == NULL) {
  55     _valid = false;
  56     error(INTERNAL_ERROR, "JSON parser was called with a string that was NULL.");
  57   } else {
  58     _valid = parse_json_value();
  59   }
  60 }
  61 
  62 bool JSON::valid() {
  63   return _valid;
  64 }
  65 
  66 bool JSON::parse_json_value() {
  67   int c;
  68 
  69   c = skip_to_token();
  70   if (c == -1) {
  71     return false;
  72   }
  73 
  74   // Must start with object or array
  75   if (level == 0) {
  76 
  77     switch (c) {
  78     case '{':
  79       if (parse_json_object() == false) {
  80         return false;
  81       }
  82       c = skip_to_token();
  83       if (c > 0) {
  84         mark_pos();
  85         error(SYNTAX_ERROR, "Only one top level object/array is allowed.");
  86         return false;
  87       } else if (c < 0) {
  88         return false;
  89       }
  90       return true;
  91 
  92     case '[':
  93       if (parse_json_array() == false) {
  94         return false;
  95       }
  96       c = skip_to_token();
  97       if (c > 0) {
  98         mark_pos();
  99         error(SYNTAX_ERROR, "Only one top level object/array is allowed.");
 100         return false;
 101       } else if (c < 0) {
 102         return false;
 103       }
 104       return true;
 105 
 106     case 0:
 107       error(SYNTAX_ERROR, "EOS was encountered before any json declarations");
 108       return false;
 109 
 110     default:
 111       error(SYNTAX_ERROR, "Json must start with an object or an array.");
 112       return false;
 113     }
 114   } else { // level > 0
 115     switch (c) {
 116     case '{':
 117       return parse_json_object();
 118 
 119     case '[':
 120       return parse_json_array();
 121 
 122     case '"':
 123       return parse_json_string();
 124 
 125     case '-': case '0':
 126     case '1': case '2': case '3':
 127     case '4': case '5': case '6':
 128     case '7': case '8': case '9':
 129       return parse_json_number();
 130 
 131     case 't':
 132       return parse_json_symbol("true", JSON_TRUE);
 133 
 134     case 'f':
 135       return parse_json_symbol("false", JSON_FALSE);
 136 
 137     case 'n':
 138       return parse_json_symbol("null", JSON_NULL);
 139 
 140     case 0:
 141       error(SYNTAX_ERROR, "EOS was encountered when expecting a json value.");
 142       return false;
 143 
 144     default:
 145       error(SYNTAX_ERROR, "Could not parse as a json value (did you forget to quote your strings?).");
 146       return false;
 147     }
 148   }
 149 }
 150 
 151 // Should only be called when we actually have the start of an object
 152 // Otherwise it is an internal error
 153 bool JSON::parse_json_object() {
 154   NOT_PRODUCT(const char* prev_pos);
 155   int c;
 156 
 157   mark_pos();
 158   // Check that we are not called in error
 159   if (expect_any("{", "object start", INTERNAL_ERROR) <= 0) {
 160     return false;
 161   }
 162 
 163   if (!callback(JSON_OBJECT_BEGIN, NULL, level++)) {
 164     return false;
 165   }
 166 
 167   for (;;) {
 168     mark_pos();
 169     c = skip_to_token();
 170     if (c == 0) {
 171       error(SYNTAX_ERROR, "EOS when expecting an object key or object end");
 172       return false;
 173     } else if (c < 0) {
 174       return false;
 175     } else if (c == '}') {
 176       // We got here from either empty object "{}" or ending comma "{a:1,}"
 177       next();
 178       break;
 179     }
 180 
 181     NOT_PRODUCT(prev_pos = pos);
 182     if (parse_json_key() == false) {
 183       return false;
 184     }
 185     assert(pos > prev_pos, "parsing stalled");
 186 
 187     skip_to_token();
 188     mark_pos();
 189     if (expect_any(":", "object key-value separator") <= 0) {
 190       return false;
 191     }
 192 
 193     skip_to_token();
 194     mark_pos();
 195     NOT_PRODUCT(prev_pos = pos);
 196     if (parse_json_value() == false) {
 197       return false;
 198     }
 199     assert(pos > prev_pos, "parsing stalled");
 200 
 201     c = skip_to_token();
 202     mark_pos();
 203     if (expect_any(",}", "value separator or object end") <= 0) {
 204       return false;
 205     }
 206     if (c == '}') {
 207       break;
 208     }
 209   }
 210 
 211   assert(c == '}', "array parsing ended without object end token ('}')");
 212   return callback(JSON_OBJECT_END, NULL, --level);
 213 }
 214 
 215 // Should only be called when we actually have the start of an array
 216 // Otherwise it is an internal error
 217 bool JSON::parse_json_array() {
 218   NOT_PRODUCT(const char* prev_pos);
 219   int c;
 220 
 221   mark_pos();
 222   // Check that we are not called in error
 223   if (expect_any("[", "array start character", INTERNAL_ERROR) <= 0) {
 224     return false;
 225   }
 226 
 227   if (!callback(JSON_ARRAY_BEGIN, NULL, level++)) {
 228     return false;
 229   }
 230 
 231   for (;;) {
 232     mark_pos();
 233     c = skip_to_token();
 234     if (c == 0) {
 235       error(SYNTAX_ERROR, "EOS when expecting a json value or array end");
 236       return false;
 237     } else if (c < 0) {
 238       return false;
 239     } else if (c == ']') {
 240       // We got here from either empty array "[]" or ending comma "[1,]"
 241       next();
 242       break;
 243     }
 244 
 245     mark_pos();
 246     NOT_PRODUCT(prev_pos = pos);
 247     if (parse_json_value() == false) {
 248       return false;
 249     }
 250     assert(pos > prev_pos, "parsing stalled");
 251 
 252     c = skip_to_token();
 253     mark_pos();
 254     if (expect_any(",]", "value separator or array end") <= 0) {
 255       return false;
 256     }
 257     if (c == ']') {
 258       break;
 259     }
 260   }
 261 
 262   assert(c == ']', "array parsing ended without array end token (']')");
 263   return callback(JSON_ARRAY_END, NULL, --level);
 264 }
 265 
 266 bool JSON::parse_json_string(bool key) {
 267   const char* end;
 268   JSON_VAL v;
 269 
 270   mark_pos();
 271   if (expect_any("\"", "string start character", INTERNAL_ERROR) <= 0) {
 272     return false;
 273   }
 274 
 275   end = strchr(pos, '"'); // TODO: escapes
 276   if (end == NULL) {
 277     error(SYNTAX_ERROR, "String started here never ended. Expected \'\"\' before EOS.");
 278     return false;
 279   }
 280 
 281   v.str.start = pos;
 282   v.str.length = end - pos;
 283   skip(end - pos);
 284 
 285   if (expect_any("\"", "string end character", INTERNAL_ERROR) <= 0) {
 286     return false;
 287   }
 288 
 289   if (key == true) {
 290     return callback(JSON_KEY, &v, level);
 291   } else {
 292     return callback(JSON_STRING, &v, level);
 293   }
 294 }
 295 
 296 // TODO: hotspot equivalents?
 297 static bool is_alpha(u_char c) {
 298   return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
 299 }
 300 static bool is_numeric(u_char c) {
 301   return (c >= '0' && c <= '9');
 302 }
 303 static bool is_alnum(u_char c) {
 304   return is_alpha(c) || is_numeric(c);
 305 }
 306 static bool is_word(u_char c) {
 307   return c == '_' || is_alnum(c);
 308 }
 309 
 310 // Allow object keys to be without quotation,
 311 // but then restrict to ([a-zA-Z0-9_])+
 312 bool JSON::parse_json_key() {
 313   const char* begin;
 314   JSON_VAL v;
 315   u_char c;
 316 
 317   mark_pos();
 318   c = peek();
 319   if (c == '"') {
 320     return parse_json_string(true);
 321   }
 322 
 323   begin = pos;
 324   c = peek();
 325   if (c == 0) {
 326     error(SYNTAX_ERROR, "Got EOS when expecting an object key.");
 327     return false;
 328   } else if (is_word(c) == false) {
 329     error(SYNTAX_ERROR, "Expected an object key, which can be a double-quoted (\") string or a simple string (only alphanumeric characters and underscore, separated by whitespace) that doesn't need to be quoted.");
 330     return false;
 331   }
 332 
 333   for (;;) {
 334     c = peek();
 335     // Allow the key to be delimited by control characters and the object key-value separator ':'
 336     if (c <= ' ' || c == ':') {
 337       break;
 338     } else if (is_word(c) == false) {
 339       error(SYNTAX_ERROR, "Object key need to be quoted, or consist entirely of alphanumeric characters and underscores.");
 340       return false;
 341     }
 342     next();
 343   }
 344 
 345   v.str.start = begin;
 346   v.str.length = pos - begin;
 347   return callback(JSON_KEY, &v, level);
 348 }
 349 
 350 bool JSON::parse_json_number() {
 351   double double_value;
 352   int tokens, read;
 353   JSON_VAL v;
 354 
 355   mark_pos();
 356 
 357   // Parsing number - for simplicity ints are limited to 2**53
 358   // sscanf as a double and check if part is 0.
 359   tokens = sscanf(pos, "%lf%n", &double_value, &read);
 360   assert(tokens <= 1, "scanf implementation that counts %n as a token, parsing json numbers will always fail");
 361   if (tokens == 1) {
 362     assert(read > 0, "sanity");
 363 
 364     if (floor(double_value) == double_value) {
 365       // No exponent - treat as an int
 366       v.int_value = (int)double_value;
 367       if (!callback(JSON_NUMBER_INT, &v, level)) {
 368         return false;
 369       }
 370     } else {
 371       v.double_value = double_value;
 372       if (!callback(JSON_NUMBER_FLOAT, &v, level)) {
 373         return false;
 374       }
 375     }
 376     skip(read);
 377     return true;
 378   }
 379 
 380   error(SYNTAX_ERROR, "Couldn't parse json number (note that exponents are not supported).");
 381   return false;
 382 }
 383 
 384 bool JSON::parse_json_symbol(const char* name, JSON_TYPE symbol) {
 385   if (expect_string(name, "maybe you forgot to quote your strings?") == false) {
 386     mark_pos();
 387     return false;
 388   }
 389   return callback(symbol, NULL, level);
 390 }
 391 
 392 void JSON::mark_pos() {
 393   assert((mark == start || *(mark - 1)) != 0, "buffer overrun");
 394   assert(mark <= pos, "mark runahead");
 395 
 396   u_char c;
 397 
 398   while (mark < pos) {
 399     c = *mark;
 400     assert(c != 0, "pos buffer overrun?");
 401     if (c != 0) {
 402       mark++;
 403       column++;
 404     }
 405     if (c == '\n') {
 406       line++;
 407       column = 0;
 408     }
 409   }
 410 
 411   assert(mark <= pos, "mark runahead");
 412 }
 413 
 414 u_char JSON::next() {
 415   assert((pos == start || *(pos - 1)) != 0, "buffer overrun");
 416 
 417   u_char c = *pos;
 418   if (c != 0) {
 419     pos++;
 420   }
 421   return c;
 422 }
 423 
 424 u_char JSON::peek() {
 425   return *pos;
 426 }
 427 
 428 // Peek ahead i chars (0 is same as peek())
 429 u_char JSON::peek(size_t i) {
 430   u_char c;
 431   const char* p;
 432 
 433   p = pos;
 434   c = *p;
 435   while (i > 0 && c != 0) {
 436     i--;
 437     p++;
 438     c = *p;
 439   }
 440   return c;
 441 }
 442 
 443 /*
 444  * Check that one of the expected characters is next in the stream.
 445  * If not, it is an error.
 446  * Returns 0 if EOS is encountered.
 447  * Returns -1 if the next character was not one of the expected.
 448  * Otherwise consumes and returns the expected character that was encountered.
 449  */
 450 int JSON::expect_any(const char* valid_chars, const char* error_msg, JSON_ERROR e) {
 451   size_t len;
 452   u_char c;
 453 
 454   len = strlen(valid_chars);
 455   assert(len > 0, "need non-empty string");
 456 
 457   c = peek();
 458   if (c == 0) {
 459     error(e, "Got EOS when expecting %s (%s\'%s\').", error_msg, len > 1 ? "one of " : "", valid_chars);
 460     return 0;
 461   }
 462   for (size_t i = 0; i < len; i++) {
 463     if (c == valid_chars[i]) {
 464       return next();
 465     }
 466   }
 467   error(e, "Expected %s (%s\'%s\').", error_msg, len > 1 ? "one of " : "", valid_chars);
 468   return -1;
 469 }
 470 
 471 /*
 472  * Check that the expected string is next in the stream.
 473  * If not, it is an error.
 474  * Consumes the expected characters if they are present.
 475  * Returns true if the expected characters were present, otherwise false.
 476  */
 477 bool JSON::expect_string(const char* expected_string, const char* error_msg, JSON_ERROR e) {
 478   u_char c, expected_char;
 479   size_t len;
 480 
 481   assert(expected_string != NULL, "need non-null string");
 482   len = strlen(expected_string);
 483   assert(len > 0, "need non-empty string");
 484 
 485   for (size_t i = 0; i < len; i++) {
 486     expected_char = expected_string[i];
 487     assert(expected_char > ' ', "not sane for control characters");
 488     if (expected_char <= ' ') {
 489       error(INTERNAL_ERROR, "expect got a control char");
 490     }
 491     c = pos[i];
 492     if (c == 0) {
 493       error(e, "EOS encountered when expecting %s (\"%s\")", error_msg, expected_string);
 494       return false;
 495     } else if (c != expected_char) {
 496       error(e, "Expected \"%s\" (%s)", expected_string, error_msg);
 497       return false;
 498     }
 499   }
 500   skip(len);
 501   return true;
 502 }
 503 
 504 /*
 505  * Skip i characters.
 506  * Returns number of characters skipped.
 507  */
 508 size_t JSON::skip(size_t i) {
 509   u_char c;
 510   size_t j;
 511 
 512   c = peek();
 513   for (j = i; c != 0 && j > 0; j--) {
 514     c = next();
 515   }
 516   return i - j;
 517 }
 518 
 519 /*
 520  * Skip whitespace and comments.
 521  * Returns the first token after whitespace/comments without consuming it
 522  * Returns 0 if EOS is encountered.
 523  * Returns -1 if there is an error
 524  */
 525 int JSON::skip_to_token() {
 526   for (;;) {
 527     int c = peek(0);
 528     if (c == '/') {
 529       u_char c2 = peek(1);
 530       if (c2 == '/') {
 531         c = skip_line_comment();
 532       } else if (c2 == '*') {
 533         c = skip_block_comment();
 534         if (c < 0) {
 535           return -1;
 536         }
 537       }
 538       // Fall through to keep checking if there
 539       // are more whitespace / comments to skip
 540     }
 541     if (c == 0 || c > ' ') {
 542       return c;
 543     }
 544     next();
 545   }
 546   return 0;
 547 }
 548 
 549 /*
 550  * Skip to, and return the wanted char without consuming it
 551  * Returns 0 if EOS is encountered.
 552  */
 553 u_char JSON::skip_to(u_char want) {
 554   // We want the bookkeeping done in next().
 555   // Otherwise strchr could have been used.
 556   u_char c;
 557   for(;;) {
 558     c = peek();
 559     if (c == 0 || c == want) {
 560       return c;
 561     }
 562     next();
 563   }
 564 }
 565 
 566 /*
 567  * Should only be called when we actually have a line comment to skip.
 568  * Otherwise it is an internal error.
 569  *
 570  * Will return the first token after the line comment without consuming it.
 571  * Returns 0 if EOS is encoutered.
 572  */
 573 u_char JSON::skip_line_comment() {
 574   u_char c;
 575 
 576   // Check that we are not called in error
 577   expect_any("/", "line comment start", INTERNAL_ERROR);
 578   expect_any("/", "line comment start", INTERNAL_ERROR);
 579 
 580   c = skip_to('\n');
 581   if (c == 0) {
 582     return 0;
 583   }
 584   next();
 585   return next();
 586 }
 587 
 588 /*
 589  * Should only be called when we actually have a block comment to skip.
 590  * Otherwise it is an internal error.
 591  *
 592  * Returns the first token after the block comment without consuming it.
 593  * Returns -1 if EOS is encountered in the middle of a comment.
 594  */
 595 int JSON::skip_block_comment() {
 596   const char* current;
 597 
 598   // Check that we are not called in error.
 599   if (peek() != '/' || peek(1) != '*') {
 600     // Let expect handle EOS.
 601     expect_string("/*", "block comment start", INTERNAL_ERROR);
 602     return 0;
 603   }
 604 
 605   current = pos;
 606   for (;;) {
 607     current = strchrnul(current, '*');
 608 
 609     if (current[0] == 0 || current[1] == 0) {
 610       // Advance error marker to start of block comment
 611       mark_pos();
 612       error(SYNTAX_ERROR, "Block comment started here never ended. Expected \"*/\" before EOS.");
 613       return -1;
 614     }
 615 
 616     if (current[1] == '/') {
 617       pos = current;
 618       if (expect_string("*/", "block comment end", INTERNAL_ERROR) == false) {
 619         return -1;
 620       }
 621       // Found block comment end
 622       return peek();
 623     }
 624     current++;
 625   }
 626 }
 627 
 628 const char* JSON::strerror(JSON_ERROR e) {
 629   switch (e) {
 630   case SYNTAX_ERROR:
 631     return "Syntax error";
 632   case INTERNAL_ERROR:
 633     return "Internal error";
 634   case KEY_ERROR:
 635     return "Key error";
 636   case VALUE_ERROR:
 637     return "Value error";
 638   default:
 639     ShouldNotReachHere();
 640     return "Unknown error";
 641   }
 642 }
 643 
 644 void JSON::error(JSON_ERROR e, const char* format, ...) {
 645   _valid = false;
 646 
 647   if (silent == false) {
 648     const char* line_start;
 649     const char* tmp;
 650     size_t line_length;
 651     va_list args;
 652     u_char c;
 653 
 654     printf("%s on line %u byte %u: ", JSON::strerror(e), line, column + 1);
 655     va_start(args, format);
 656     vprintf(format, args);
 657     printf("\n");
 658     va_end(args);
 659 
 660     line_start = mark - column;
 661     assert(line_start >= start, "out of bounds");
 662     assert(line_start <= mark, "out of bounds");
 663     assert(line_start == start || line_start[-1] == '\n', "line counting error");
 664 
 665     c = *pos;
 666     if (c == 0) {
 667       printf("  Got ");
 668       printf("EOS.\n");
 669     }
 670     tmp = mark;
 671     c = *tmp;
 672     if (c > ' ') {
 673       printf("  At ");
 674       printf("'");
 675       while (c > ' ') {
 676         printf("%c", c);
 677         tmp++;
 678         c = *tmp;
 679       }
 680       printf("'.\n");
 681     }
 682 
 683     // Skip to newline or EOS
 684     tmp = strchrnul(mark, '\n');
 685     line_length = tmp - line_start;
 686 
 687     //char buf[200];
 688     //buf[0] = 0;
 689     //snprintf(buf, line_length + 1 > 200 ? 200 : line_length + 1, "%s", line_start);
 690     //buf[199] = 0;
 691     printf("%s\n", line_start);
 692   }
 693 }
 694 
 695 #ifndef PRODUCT
 696 void JSONTest::test(const char* text, bool should_pass) {
 697   JSONTest json(text);
 698   if (should_pass) {
 699     assert(json.valid() == true, "failed on a valid json string");
 700     if (VerboseInternalVMTests) {
 701       printf("-- json test passed as expected --\n");
 702     }
 703   } else {
 704     assert(json.valid() == false, "succeeded on an invalid json string");
 705     if (VerboseInternalVMTests) {
 706       printf("-- json test failed as expected --\n");
 707     }
 708   }
 709 }
 710 
 711 JSONTest::JSONTest(const char* text) : JSON(text, VerboseInternalVMTests == false) {
 712   prev = JSON_NONE;
 713   parse();
 714 }
 715 
 716 bool JSONTest::test() {
 717   JSONTest::test("{}", true);
 718   JSONTest::test("[]", true);
 719   JSONTest::test("  {  }  ", true);
 720   JSONTest::test("  [  ]  ", true);
 721 
 722   JSONTest::test("\"error\"", false);
 723   JSONTest::test("error", false);
 724   JSONTest::test("1", false);
 725   JSONTest::test("1.2", false);
 726   JSONTest::test("true", false);
 727   JSONTest::test("false", false);
 728   JSONTest::test("null", false);
 729 
 730   JSONTest::test("[ 1 ]", true);
 731   JSONTest::test("[ 1, ]", true);
 732   JSONTest::test("[ true ]", true);
 733   JSONTest::test("[ true, ]", true);
 734   JSONTest::test("[ false ]", true);
 735   JSONTest::test("[ false, ]", true);
 736   JSONTest::test("[ null ]", true);
 737   JSONTest::test("[ null, ]", true);
 738   JSONTest::test("[ \"\" ]", true);
 739   JSONTest::test("[ \"\", ]", true);
 740   JSONTest::test("[ \"elem1\" ]", true);
 741   JSONTest::test("[ \"elem1\", ]", true);
 742   JSONTest::test("[ \"elem1\", ]", true);
 743   JSONTest::test("[ \"elem1\" ]", true);
 744   JSONTest::test("[ \"elem1\", \"elem2\" ]", true);
 745   JSONTest::test("[ \"elem1\", \"elem2\", ]", true);
 746 
 747 
 748   JSONTest::test("[ \"elem1\" ] { }", false);
 749   JSONTest::test("[ elem1, \"elem2\" ]", false);
 750   JSONTest::test("[ \"elem1\"", false);
 751   JSONTest::test("[ \"elem1 ]", false);
 752   JSONTest::test("[ \"elem1\", \"elem2\"", false);
 753   JSONTest::test("[ truefoo ]", false);
 754   JSONTest::test("[ falsefoo ]", false);
 755   JSONTest::test("[ nullfoo ]", false);
 756 
 757   JSONTest::test("{ key : 1 }", true);
 758   JSONTest::test("{ key : 1, }", true);
 759   JSONTest::test("{ key : true }", true);
 760   JSONTest::test("{ key : true, }", true);
 761   JSONTest::test("{ key : false }", true);
 762   JSONTest::test("{ key : false, }", true);
 763   JSONTest::test("{ key : null }", true);
 764   JSONTest::test("{ key : null, }", true);
 765   JSONTest::test("{ \"\" : \"\" }", true);
 766   JSONTest::test("{ \"\" : \"\", }", true);
 767   JSONTest::test("{ \"key1\" : \"val1\" }", true);
 768   JSONTest::test("{ \"key1\" : \"val1\", }", true);
 769   JSONTest::test("{ \"key1\" : \"val1\", \"key2\" : \"val2\" }", true);
 770   JSONTest::test("{ \"key1\" : \"val1\", \"key2\" : \"val2\", }", true);
 771 
 772   JSONTest::test("{ \"key\" : \"val\" } [ \"error\" ]", false);
 773   JSONTest::test("{ \"key\" : \"val\" ", false);
 774 
 775   JSONTest::test("/**/ { }", true);
 776   JSONTest::test("/* */ { }", true);
 777   JSONTest::test("/*foo*/ { }", true);
 778   JSONTest::test("/* *foo */ { }", true);
 779   JSONTest::test("/* *foo* */ { }", true);
 780   JSONTest::test("/* /*foo */ { }", true);
 781   JSONTest::test("{ } /* foo */", true);
 782   JSONTest::test("{ } /* foo */ ", true);
 783   JSONTest::test("{ } //", true);
 784   JSONTest::test("{ } // ", true);
 785   JSONTest::test("{ } // foo", true);
 786 
 787   JSONTest::test("/* * / { }", false);
 788   JSONTest::test("/ * */ { }", false);
 789   JSONTest::test("// { }", false);
 790   JSONTest::test("/* { } */", false);
 791   JSONTest::test("/* { } */ ", false);
 792   JSONTest::test("/* { } ", false);
 793   JSONTest::test("{ } /* ", false);
 794   JSONTest::test("/* { } *", false);
 795   JSONTest::test("{ /* } */", false);
 796   JSONTest::test("[ /* ] */", false);
 797   JSONTest::test("{ key : \"val\", /* } */", false);
 798   JSONTest::test("[ \"val\", /* ] */", false);
 799 
 800   JSONTest::test("/* comment */{ key1 : { \"key2\" : { \"key3\" : [ \"elem1\", \"elem2\", { \"key4\" : null }, 3, 2, 1, 0, -1, -2, -3, true, false, null, ] }, \"key5\" : true }, \"key6\" : [ \"☃\" ], key7 : \"val\",}", true);
 801   JSONTest::test("/* comment */ { \"key1\" : { \"key2\" : { \"key3\" : [ \"elem1\", \"elem2\", { \"key4\" : null }, 3, 2, 1, 0, -1, -2, -3, true, false, null, ] }, \"key5\" : true }, \"key6\" : [ \"☃\" ], key7 : \"val\",}", true);
 802   JSONTest::test("/*comment*/{\"ff1 fsd\":{\"☃\":{\"☃\":[\"☃\",\"☃\"]},\"☃\":true},\"☃\":[\"☃\"],\"foo\":\"☃\",}", true);
 803   JSONTest::test("/* comment */ { key1 error : { \"☃\" : { \"☃\" : [ \"☃\", \"☃\" ] }, \"☃\" : true }, \"baz\" : [ \"☃\" ], foo : \"☃\",}", false); // first key needs to be quoted since it contains a space
 804   JSONTest::test("/* comment */ { key : 1.2 }", false); // Float not supported
 805 
 806   JSONTest::test("[\n]", true);
 807 
 808   JSONTest::test(
 809       "[" "\n"
 810       "   {"
 811       "         // pattern to match against class+method+signature" "\n"
 812       "         // leading and trailing wildcard (*) allowed" "\n"
 813       "         match: \"foo.bar.*\"," "\n"
 814       " " "\n"
 815       "         // override defaults for specified compiler" "\n"
 816       "         // we may differentiate between levels too. TBD." "\n"
 817       "         c1:  {" "\n"
 818       "           //override c1 presets " "\n"
 819       "           array_bounds_check_removal: false" "\n"
 820       "         }," "\n"
 821       "" "\n"
 822       "         c2: {" "\n"
 823       "           // control inlining of method" "\n"
 824       "           // + force inline, - dont inline" "\n"
 825       "           inline : [ \"+java.util.*\", \"-com.sun.*\"]," "\n"
 826       "         }," "\n"
 827       "" "\n"
 828       "         // directives outside a specific preset applies to all compilers" "\n"
 829       "         inline : [ \"+java.util.*\", \"-com.sun.*\"]," "\n"
 830       "         print_assembly: true," "\n"
 831       "         verify_oopmaps: true," "\n"
 832       "         max_loop_unrolling: 5" "\n"
 833       "   }," "\n"
 834       "   {" "\n"
 835       "         // matching several patterns require an array" "\n"
 836       "         match: [\"baz.*\",\"frob*\"]," "\n"
 837       "" "\n"
 838       "         // only enable c1 for this directive" "\n"
 839       "         // all enabled by default. Command disables all not listed" "\n"
 840       "         enable: \"c1\"," "\n"
 841       "" "\n"
 842       "         // applies to all compilers" "\n"
 843       "         // + force inline, - dont inline" "\n"
 844       "         inline : [ \"+java.util.*\", \"-com.sun.*\"]," "\n"
 845       "         print_inlining: true," "\n"
 846       "" "\n"
 847       "         // force matching compiles to be blocking/syncronous" "\n"
 848       "         blocking_compile: true" "\n"
 849       "   }," "\n"
 850       "]" "\n", true);
 851 
 852   return true;
 853 }
 854 
 855 void JSONTest::log(uint indent, const char* format, ...) {
 856   if (VerboseInternalVMTests) {
 857     if (prev != JSON_KEY) {
 858       for (uint i = 0; i < indent; i++) {
 859         printf("  ");
 860       }
 861     }
 862     va_list args;
 863     va_start(args, format);
 864     vprintf(format, args);
 865     va_end(args);
 866   }
 867 }
 868 
 869 bool JSONTest::callback(JSON_TYPE t, JSON_VAL* v, uint rlevel) {
 870   switch (t) {
 871   case JSON_OBJECT_BEGIN:
 872     log(rlevel, "{\n");
 873     prev = JSON_NONE; // Only care about JSON_KEY, to indent correctly
 874     return true;
 875 
 876   case JSON_OBJECT_END:
 877     log(rlevel, "},\n");
 878     prev = JSON_NONE;
 879     return true;
 880 
 881   case JSON_ARRAY_BEGIN:
 882     log(rlevel, "[\n");
 883     prev = JSON_NONE;
 884     return true;
 885 
 886   case JSON_ARRAY_END:
 887     log(rlevel, "],\n");
 888     prev = JSON_NONE;
 889     return true;
 890 
 891   case JSON_KEY:
 892     if (VerboseInternalVMTests) {
 893       for (uint i = 0; i < rlevel; i++) {
 894         printf("  ");
 895       }
 896       printf("<key>");
 897       for (size_t i = 0; i < v->str.length; i++) {
 898         u_char c = v->str.start[i];
 899         assert(c != 0, "string overrun");
 900         if (c == 0) {
 901           return false;
 902         }
 903         printf("%c", c);
 904       }
 905       printf(" : ");
 906     }
 907     prev = JSON_KEY;
 908     return true;
 909 
 910   case JSON_STRING:
 911     if (VerboseInternalVMTests) {
 912       if (prev != JSON_KEY) {
 913         for (uint i = 0; i < rlevel; i++) {
 914           printf("  ");
 915         }
 916       }
 917       printf("<str>");
 918       for (size_t i = 0; i < v->str.length; i++) {
 919         u_char c = v->str.start[i];
 920         assert(c != 0, "string overrun");
 921         if (c == 0) {
 922           return false;
 923         }
 924         printf("%c", c);
 925       }
 926       printf(",\n");
 927     }
 928     prev = JSON_NONE;
 929     return true;
 930 
 931   case JSON_NUMBER_INT:
 932     log(rlevel, "<int>%" PRId64 ",\n", v->int_value);
 933     prev = JSON_NONE;
 934     return true;
 935 
 936   case JSON_NUMBER_FLOAT:
 937     log(rlevel, "<double>%lf,\n", v->double_value);
 938     prev = JSON_NONE;
 939     return true;
 940 
 941   case JSON_TRUE:
 942     log(rlevel, "<true>,\n");
 943     prev = JSON_NONE;
 944     return true;
 945 
 946   case JSON_FALSE:
 947     log(rlevel, "<false>,\n");
 948     prev = JSON_NONE;
 949     return true;
 950 
 951   case JSON_NULL:
 952     log(rlevel, "<null>,\n");
 953     prev = JSON_NONE;
 954     return true;
 955 
 956   default:
 957     error(INTERNAL_ERROR, "unknown JSON type");
 958     return false;
 959   }
 960 }
 961 #endif