Differences From Artifact [06182df9e0]:
- File src/OFString+JSONValue.m — part of check-in [670a739c02] at 2012-06-05 16:05:32 on branch trunk — Disallow newlines in JSON strings. (user: js, size: 11683) [annotate] [blame] [check-ins using] [more...]
To Artifact [291a080964]:
- File
src/OFString+JSONValue.m
— part of check-in
[72c71c4eb2]
at
2012-06-10 16:07:33
on branch trunk
— JSON: Make it possible to get the invalid line.
This introduces OFInvalidJSONException, which has a property line to get
the line in which the parsing error occurred. (user: js, size: 12343) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
23 24 25 26 27 28 29 | #import "OFString+JSONValue.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFNumber.h" #import "OFNull.h" | | | > | > | > > > > | > | 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #import "OFString+JSONValue.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFNumber.h" #import "OFNull.h" #import "OFInvalidJSONException.h" #import "macros.h" int _OFString_JSONValue_reference; static id nextObject(const char *restrict *, const char*, size_t *restrict line); static void skipWhitespaces(const char *restrict *pointer, const char *stop, size_t *restrict line) { while (*pointer < stop && (**pointer == ' ' || **pointer == '\t' || **pointer == '\r' || **pointer == '\n')) { if (**pointer == '\n') (*line)++; (*pointer)++; } } static void skipComment(const char *restrict *pointer, const char *stop, size_t *restrict line) { if (**pointer != '/') return; if (*pointer + 1 >= stop) return; |
︙ | ︙ | |||
62 63 64 65 66 67 68 69 70 71 | while (*pointer < stop) { if (lastIsAsterisk && **pointer == '/') { (*pointer)++; return; } lastIsAsterisk = (**pointer == '*'); (*pointer)++; } | > > > | > | > | > | | | 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | while (*pointer < stop) { if (lastIsAsterisk && **pointer == '/') { (*pointer)++; return; } lastIsAsterisk = (**pointer == '*'); if (**pointer == '\n') (*line)++; (*pointer)++; } } else if (**pointer == '/') { (*pointer)++; while (*pointer < stop) { if (**pointer == '\r' || **pointer == '\n') { (*pointer)++; (*line)++; return; } (*pointer)++; } } else (*pointer)--; } static void skipWhitespacesAndComments(const char *restrict *pointer, const char *stop, size_t *restrict line) { const char *old = NULL; while (old != *pointer) { old = *pointer; skipWhitespaces(pointer, stop, line); skipComment(pointer, stop, line); } } static inline uint16_t parseUnicodeEscape(const char *pointer, const char *stop) { uint16_t ret = 0; |
︙ | ︙ | |||
122 123 124 125 126 127 128 | return 0xFFFF; } return ret; } static inline OFString* | | > | 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | return 0xFFFF; } return ret; } static inline OFString* parseString(const char *restrict *pointer, const char *stop, size_t *restrict line) { char *buffer; size_t i = 0; char delimiter = **pointer; if (++(*pointer) + 1 >= stop) return nil; |
︙ | ︙ | |||
229 230 231 232 233 234 235 | i += l; *pointer += 11; break; case '\r': (*pointer)++; | | > > > | 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | i += l; *pointer += 11; break; case '\r': (*pointer)++; if (*pointer < stop && **pointer == '\n') { (*pointer)++; (*line)++; } break; case '\n': (*pointer)++; (*line)++; break; default: free(buffer); return nil; } /* End of string found */ } else if (**pointer == delimiter) { |
︙ | ︙ | |||
256 257 258 259 260 261 262 263 264 265 266 267 268 269 | } (*pointer)++; return ret; /* Newlines in strings are disallowed */ } else if (**pointer == '\n' || **pointer == '\r') { free(buffer); return nil; } else { buffer[i++] = **pointer; (*pointer)++; } } | > | 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | } (*pointer)++; return ret; /* Newlines in strings are disallowed */ } else if (**pointer == '\n' || **pointer == '\r') { (*line)++; free(buffer); return nil; } else { buffer[i++] = **pointer; (*pointer)++; } } |
︙ | ︙ | |||
369 370 371 372 373 374 375 | * It is never possible to end with an identifier, thus we should never * reach stop. */ return nil; } static inline OFMutableArray* | | > | | | | | | > | | | | | | | | | > | > > > > | 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | * It is never possible to end with an identifier, thus we should never * reach stop. */ return nil; } static inline OFMutableArray* parseArray(const char *restrict *pointer, const char *stop, size_t *restrict line) { OFMutableArray *array = [OFMutableArray array]; if (++(*pointer) >= stop) return nil; while (**pointer != ']') { id object; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == ']') break; if (**pointer == ',') { (*pointer)++; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop || **pointer != ']') return nil; break; } if ((object = nextObject(pointer, stop, line)) == nil) return nil; [array addObject: object]; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == ',') { (*pointer)++; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; } else if (**pointer != ']') return nil; } (*pointer)++; return array; } static inline OFMutableDictionary* parseDictionary(const char *restrict *pointer, const char *stop, size_t *restrict line) { OFMutableDictionary *dictionary = [OFMutableDictionary dictionary]; if (++(*pointer) >= stop) return nil; while (**pointer != '}') { id key, object; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == '}') break; if (**pointer == ',') { (*pointer)++; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop || **pointer != '}') return nil; break; } skipWhitespacesAndComments(pointer, stop, line); if (*pointer + 1 >= stop) return nil; if ((**pointer >= 'a' && **pointer <= 'z') || (**pointer >= 'A' && **pointer <= 'Z') || **pointer == '_' || **pointer == '$' || **pointer == '\\') key = parseIdentifier(pointer, stop); else key = nextObject(pointer, stop, line); if (key == nil) return nil; skipWhitespacesAndComments(pointer, stop, line); if (*pointer + 1 >= stop || **pointer != ':') return nil; (*pointer)++; if ((object = nextObject(pointer, stop, line)) == nil) return nil; [dictionary setObject: object forKey: key]; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; if (**pointer == ',') { (*pointer)++; skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; } else if (**pointer != '}') return nil; } (*pointer)++; return dictionary; } static inline OFNumber* parseNumber(const char *restrict *pointer, const char *stop, size_t *restrict line) { BOOL isHex = (*pointer + 1 < stop && (*pointer)[1] == 'x'); BOOL hasDecimal = NO; size_t i; OFString *string; OFNumber *number; for (i = 0; *pointer + i < stop; i++) { if ((*pointer)[i] == '.') hasDecimal = YES; if ((*pointer)[i] == ' ' || (*pointer)[i] == '\t' || (*pointer)[i] == '\r' || (*pointer)[i] == '\n' || (*pointer)[i] == ',' || (*pointer)[i] == ']' || (*pointer)[i] == '}') { if ((*pointer)[i] == '\n') (*line)++; break; } } string = [[OFString alloc] initWithUTF8String: *pointer length: i]; *pointer += i; @try { |
︙ | ︙ | |||
535 536 537 538 539 540 541 | [string release]; } return number; } static id | | > | | | | | 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | [string release]; } return number; } static id nextObject(const char *restrict *pointer, const char *stop, size_t *restrict line) { skipWhitespacesAndComments(pointer, stop, line); if (*pointer >= stop) return nil; switch (**pointer) { case '"': case '\'': return parseString(pointer, stop, line); case '[': return parseArray(pointer, stop, line); case '{': return parseDictionary(pointer, stop, line); case 't': if (*pointer + 3 >= stop) return nil; if (memcmp(*pointer, "true", 4)) return nil; |
︙ | ︙ | |||
592 593 594 595 596 597 598 | case '5': case '6': case '7': case '8': case '9': case '-': case '.': | | > | | | > | 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | case '5': case '6': case '7': case '8': case '9': case '-': case '.': return parseNumber(pointer, stop, line); default: return nil; } } @implementation OFString (JSONValue) - (id)JSONValue { const char *pointer = [self UTF8String]; const char *stop = pointer + [self UTF8StringLength]; id object; size_t line = 1; object = nextObject(&pointer, stop, &line); skipWhitespacesAndComments(&pointer, stop, &line); if (pointer < stop || object == nil) @throw [OFInvalidJSONException exceptionWithClass: isa line: line]; return object; } @end |