Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 2 | * $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the MIT license. See COPYING for details. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 10 | * |
| 11 | * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved. |
| 12 | * The copyrights to the contents of this file are licensed under the MIT License |
| 13 | * (http://www.opensource.org/licenses/mit-license.php) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Michael Clark | c8f4a6e | 2007-12-07 02:44:24 +0000 | [diff] [blame] | 20 | #include <stddef.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #include <string.h> |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 23 | #include <limits.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 24 | |
| 25 | #include "bits.h" |
| 26 | #include "debug.h" |
| 27 | #include "printbuf.h" |
| 28 | #include "arraylist.h" |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 29 | #include "json_inttypes.h" |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 30 | #include "json_object.h" |
| 31 | #include "json_tokener.h" |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 32 | #include "json_util.h" |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 33 | |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 34 | #if !HAVE_STRNCASECMP && defined(_MSC_VER) |
| 35 | /* MSC has the version as _strnicmp */ |
| 36 | # define strncasecmp _strnicmp |
| 37 | #elif !HAVE_STRNCASECMP |
| 38 | # error You do not have strncasecmp on your system. |
| 39 | #endif /* HAVE_STRNCASECMP */ |
| 40 | |
| 41 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 42 | static const char* json_null_str = "null"; |
| 43 | static const char* json_true_str = "true"; |
| 44 | static const char* json_false_str = "false"; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 45 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 46 | const char* json_tokener_errors[] = { |
| 47 | "success", |
| 48 | "continue", |
| 49 | "nesting to deep", |
| 50 | "unexpected end of data", |
| 51 | "unexpected character", |
| 52 | "null expected", |
| 53 | "boolean expected", |
| 54 | "number expected", |
| 55 | "array value separator ',' expected", |
| 56 | "quoted object property name expected", |
| 57 | "object property name separator ':' expected", |
| 58 | "object value separator ',' expected", |
| 59 | "invalid string sequence", |
| 60 | "expected comment", |
| 61 | }; |
| 62 | |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 63 | /* Stuff for decoding unicode sequences */ |
| 64 | #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800) |
| 65 | #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00) |
| 66 | #define DECODE_SURROGATE_PAIR(hi,lo) ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000) |
| 67 | static unsigned char utf8_replacement_char[3] = { 0xEF, 0xBF, 0xBD }; |
| 68 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 69 | |
Michael Clark | e8de078 | 2009-02-25 01:45:00 +0000 | [diff] [blame] | 70 | struct json_tokener* json_tokener_new(void) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 71 | { |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 72 | struct json_tokener *tok; |
| 73 | |
| 74 | tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener)); |
Christopher Watford | 543bb14 | 2009-07-08 03:46:10 +0000 | [diff] [blame] | 75 | if (!tok) return NULL; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 76 | tok->pb = printbuf_new(); |
| 77 | json_tokener_reset(tok); |
| 78 | return tok; |
| 79 | } |
| 80 | |
| 81 | void json_tokener_free(struct json_tokener *tok) |
| 82 | { |
| 83 | json_tokener_reset(tok); |
| 84 | if(tok) printbuf_free(tok->pb); |
| 85 | free(tok); |
| 86 | } |
| 87 | |
| 88 | static void json_tokener_reset_level(struct json_tokener *tok, int depth) |
| 89 | { |
| 90 | tok->stack[depth].state = json_tokener_state_eatws; |
| 91 | tok->stack[depth].saved_state = json_tokener_state_start; |
| 92 | json_object_put(tok->stack[depth].current); |
| 93 | tok->stack[depth].current = NULL; |
| 94 | free(tok->stack[depth].obj_field_name); |
| 95 | tok->stack[depth].obj_field_name = NULL; |
| 96 | } |
| 97 | |
| 98 | void json_tokener_reset(struct json_tokener *tok) |
| 99 | { |
| 100 | int i; |
Michael Clark | 22dee7c | 2009-02-25 01:51:40 +0000 | [diff] [blame] | 101 | if (!tok) |
| 102 | return; |
| 103 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 104 | for(i = tok->depth; i >= 0; i--) |
| 105 | json_tokener_reset_level(tok, i); |
| 106 | tok->depth = 0; |
| 107 | tok->err = json_tokener_success; |
| 108 | } |
| 109 | |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 110 | struct json_object* json_tokener_parse(const char *str) |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 111 | { |
| 112 | struct json_tokener* tok; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 113 | struct json_object* obj; |
| 114 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 115 | tok = json_tokener_new(); |
| 116 | obj = json_tokener_parse_ex(tok, str, -1); |
| 117 | if(tok->err != json_tokener_success) |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 118 | obj = (struct json_object*)error_ptr(-tok->err); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 119 | json_tokener_free(tok); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 120 | return obj; |
| 121 | } |
| 122 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 123 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 124 | #if !HAVE_STRNDUP |
| 125 | /* CAW: compliant version of strndup() */ |
| 126 | char* strndup(const char* str, size_t n) |
| 127 | { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 128 | if(str) { |
| 129 | size_t len = strlen(str); |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 130 | size_t nn = json_min(len,n); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 131 | char* s = (char*)malloc(sizeof(char) * (nn + 1)); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 132 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 133 | if(s) { |
| 134 | memcpy(s, str, nn); |
| 135 | s[nn] = '\0'; |
| 136 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 137 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 138 | return s; |
| 139 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 140 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 141 | return NULL; |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 142 | } |
| 143 | #endif |
| 144 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 145 | |
| 146 | #define state tok->stack[tok->depth].state |
| 147 | #define saved_state tok->stack[tok->depth].saved_state |
| 148 | #define current tok->stack[tok->depth].current |
| 149 | #define obj_field_name tok->stack[tok->depth].obj_field_name |
| 150 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 151 | /* Optimization: |
| 152 | * json_tokener_parse_ex() consumed a lot of CPU in its main loop, |
| 153 | * iterating character-by character. A large performance boost is |
| 154 | * achieved by using tighter loops to locally handle units such as |
| 155 | * comments and strings. Loops that handle an entire token within |
| 156 | * their scope also gather entire strings and pass them to |
| 157 | * printbuf_memappend() in a single call, rather than calling |
| 158 | * printbuf_memappend() one char at a time. |
| 159 | * |
| 160 | * POP_CHAR() and ADVANCE_CHAR() macros are used for code that is |
| 161 | * common to both the main loop and the tighter loops. |
| 162 | */ |
| 163 | |
| 164 | /* POP_CHAR(dest, tok) macro: |
| 165 | * Not really a pop()...peeks at the current char and stores it in dest. |
| 166 | * Returns 1 on success, sets tok->err and returns 0 if no more chars. |
| 167 | * Implicit inputs: str, len vars |
| 168 | */ |
| 169 | #define POP_CHAR(dest, tok) \ |
| 170 | (((tok)->char_offset == len) ? \ |
| 171 | (((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_finish) ? \ |
| 172 | (((tok)->err = json_tokener_success), 0) \ |
| 173 | : \ |
| 174 | (((tok)->err = json_tokener_continue), 0) \ |
| 175 | ) : \ |
| 176 | (((dest) = *str), 1) \ |
| 177 | ) |
| 178 | |
| 179 | /* ADVANCE_CHAR() macro: |
| 180 | * Incrementes str & tok->char_offset. |
| 181 | * For convenience of existing conditionals, returns the old value of c (0 on eof) |
| 182 | * Implicit inputs: c var |
| 183 | */ |
| 184 | #define ADVANCE_CHAR(str, tok) \ |
| 185 | ( ++(str), ((tok)->char_offset)++, c) |
| 186 | |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 187 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 188 | /* End optimization macro defs */ |
| 189 | |
| 190 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 191 | struct json_object* json_tokener_parse_ex(struct json_tokener *tok, |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 192 | const char *str, int len) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 193 | { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 194 | struct json_object *obj = NULL; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 195 | char c = '\1'; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 196 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 197 | tok->char_offset = 0; |
| 198 | tok->err = json_tokener_success; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 199 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 200 | while (POP_CHAR(c, tok)) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 201 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 202 | redo_char: |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 203 | switch(state) { |
| 204 | |
| 205 | case json_tokener_state_eatws: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 206 | /* Advance until we change state */ |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame^] | 207 | while (isspace((int)c)) { |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 208 | if ((!ADVANCE_CHAR(str, tok)) || (!POP_CHAR(c, tok))) |
| 209 | goto out; |
| 210 | } |
| 211 | if(c == '/') { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 212 | printbuf_reset(tok->pb); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 213 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 214 | state = json_tokener_state_comment_start; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 215 | } else { |
| 216 | state = saved_state; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 217 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | |
| 221 | case json_tokener_state_start: |
| 222 | switch(c) { |
| 223 | case '{': |
| 224 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 225 | saved_state = json_tokener_state_object_field_start; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 226 | current = json_object_new_object(); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 227 | break; |
| 228 | case '[': |
| 229 | state = json_tokener_state_eatws; |
| 230 | saved_state = json_tokener_state_array; |
| 231 | current = json_object_new_array(); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 232 | break; |
| 233 | case 'N': |
| 234 | case 'n': |
| 235 | state = json_tokener_state_null; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 236 | printbuf_reset(tok->pb); |
| 237 | tok->st_pos = 0; |
| 238 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 239 | case '"': |
| 240 | case '\'': |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 241 | state = json_tokener_state_string; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 242 | printbuf_reset(tok->pb); |
| 243 | tok->quote_char = c; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 244 | break; |
| 245 | case 'T': |
| 246 | case 't': |
| 247 | case 'F': |
| 248 | case 'f': |
| 249 | state = json_tokener_state_boolean; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 250 | printbuf_reset(tok->pb); |
| 251 | tok->st_pos = 0; |
| 252 | goto redo_char; |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 253 | #if defined(__GNUC__) |
| 254 | case '0' ... '9': |
| 255 | #else |
| 256 | case '0': |
| 257 | case '1': |
| 258 | case '2': |
| 259 | case '3': |
| 260 | case '4': |
| 261 | case '5': |
| 262 | case '6': |
| 263 | case '7': |
| 264 | case '8': |
| 265 | case '9': |
| 266 | #endif |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 267 | case '-': |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 268 | state = json_tokener_state_number; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 269 | printbuf_reset(tok->pb); |
| 270 | tok->is_double = 0; |
| 271 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 272 | default: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 273 | tok->err = json_tokener_error_parse_unexpected; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 274 | goto out; |
| 275 | } |
| 276 | break; |
| 277 | |
| 278 | case json_tokener_state_finish: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 279 | if(tok->depth == 0) goto out; |
| 280 | obj = json_object_get(current); |
| 281 | json_tokener_reset_level(tok, tok->depth); |
| 282 | tok->depth--; |
| 283 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 284 | |
| 285 | case json_tokener_state_null: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 286 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 287 | if(strncasecmp(json_null_str, tok->pb->buf, |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 288 | json_min(tok->st_pos+1, strlen(json_null_str))) == 0) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 289 | if(tok->st_pos == strlen(json_null_str)) { |
| 290 | current = NULL; |
| 291 | saved_state = json_tokener_state_finish; |
| 292 | state = json_tokener_state_eatws; |
| 293 | goto redo_char; |
| 294 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 295 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 296 | tok->err = json_tokener_error_parse_null; |
| 297 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 298 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 299 | tok->st_pos++; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | case json_tokener_state_comment_start: |
| 303 | if(c == '*') { |
| 304 | state = json_tokener_state_comment; |
| 305 | } else if(c == '/') { |
| 306 | state = json_tokener_state_comment_eol; |
| 307 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 308 | tok->err = json_tokener_error_parse_comment; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 309 | goto out; |
| 310 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 311 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 312 | break; |
| 313 | |
| 314 | case json_tokener_state_comment: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 315 | { |
| 316 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 317 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 318 | while(c != '*') { |
| 319 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 320 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 321 | goto out; |
| 322 | } |
| 323 | } |
| 324 | printbuf_memappend_fast(tok->pb, case_start, 1+str-case_start); |
| 325 | state = json_tokener_state_comment_end; |
| 326 | } |
| 327 | break; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 328 | |
| 329 | case json_tokener_state_comment_eol: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 330 | { |
| 331 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 332 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 333 | while(c != '\n') { |
| 334 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 335 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 336 | goto out; |
| 337 | } |
| 338 | } |
| 339 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 340 | MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 341 | state = json_tokener_state_eatws; |
| 342 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 343 | break; |
| 344 | |
| 345 | case json_tokener_state_comment_end: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 346 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 347 | if(c == '/') { |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 348 | MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 349 | state = json_tokener_state_eatws; |
| 350 | } else { |
| 351 | state = json_tokener_state_comment; |
| 352 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | case json_tokener_state_string: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 356 | { |
| 357 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 358 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 359 | while(1) { |
| 360 | if(c == tok->quote_char) { |
| 361 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 362 | current = json_object_new_string(tok->pb->buf); |
| 363 | saved_state = json_tokener_state_finish; |
| 364 | state = json_tokener_state_eatws; |
| 365 | break; |
| 366 | } else if(c == '\\') { |
| 367 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 368 | saved_state = json_tokener_state_string; |
| 369 | state = json_tokener_state_string_escape; |
| 370 | break; |
| 371 | } |
| 372 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 373 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 374 | goto out; |
| 375 | } |
| 376 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 377 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case json_tokener_state_string_escape: |
| 381 | switch(c) { |
| 382 | case '"': |
| 383 | case '\\': |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 384 | case '/': |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 385 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 386 | state = saved_state; |
| 387 | break; |
| 388 | case 'b': |
| 389 | case 'n': |
| 390 | case 'r': |
| 391 | case 't': |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 392 | if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1); |
| 393 | else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1); |
| 394 | else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1); |
| 395 | else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 396 | state = saved_state; |
| 397 | break; |
| 398 | case 'u': |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 399 | tok->ucs_char = 0; |
| 400 | tok->st_pos = 0; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 401 | state = json_tokener_state_escape_unicode; |
| 402 | break; |
| 403 | default: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 404 | tok->err = json_tokener_error_parse_string; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 405 | goto out; |
| 406 | } |
| 407 | break; |
| 408 | |
| 409 | case json_tokener_state_escape_unicode: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 410 | { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 411 | unsigned int got_hi_surrogate = 0; |
| 412 | |
| 413 | /* Handle a 4-byte sequence, or two sequences if a surrogate pair */ |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 414 | while(1) { |
| 415 | if(strchr(json_hex_chars, c)) { |
| 416 | tok->ucs_char += ((unsigned int)hexdigit(c) << ((3-tok->st_pos++)*4)); |
| 417 | if(tok->st_pos == 4) { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 418 | unsigned char unescaped_utf[4]; |
| 419 | |
| 420 | if (got_hi_surrogate) { |
| 421 | if (IS_LOW_SURROGATE(tok->ucs_char)) { |
| 422 | /* Recalculate the ucs_char, then fall thru to process normally */ |
| 423 | tok->ucs_char = DECODE_SURROGATE_PAIR(got_hi_surrogate, tok->ucs_char); |
| 424 | } else { |
| 425 | /* Hi surrogate was not followed by a low surrogate */ |
| 426 | /* Replace the hi and process the rest normally */ |
| 427 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 428 | } |
| 429 | got_hi_surrogate = 0; |
| 430 | } |
| 431 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 432 | if (tok->ucs_char < 0x80) { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 433 | unescaped_utf[0] = tok->ucs_char; |
| 434 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 1); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 435 | } else if (tok->ucs_char < 0x800) { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 436 | unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6); |
| 437 | unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f); |
| 438 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 2); |
| 439 | } else if (IS_HIGH_SURROGATE(tok->ucs_char)) { |
| 440 | /* Got a high surrogate. Remember it and look for the |
| 441 | * the beginning of another sequence, which should be the |
| 442 | * low surrogate. |
| 443 | */ |
| 444 | got_hi_surrogate = tok->ucs_char; |
| 445 | /* Not at end, and the next two chars should be "\u" */ |
| 446 | if ((tok->char_offset+1 != len) && |
| 447 | (tok->char_offset+2 != len) && |
| 448 | (str[1] == '\\') && |
| 449 | (str[2] == 'u')) |
| 450 | { |
| 451 | ADVANCE_CHAR(str, tok); |
| 452 | ADVANCE_CHAR(str, tok); |
| 453 | |
| 454 | /* Advance to the first char of the next sequence and |
| 455 | * continue processing with the next sequence. |
| 456 | */ |
| 457 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 458 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 459 | goto out; |
| 460 | } |
| 461 | tok->ucs_char = 0; |
| 462 | tok->st_pos = 0; |
| 463 | continue; /* other json_tokener_state_escape_unicode */ |
| 464 | } else { |
| 465 | /* Got a high surrogate without another sequence following |
| 466 | * it. Put a replacement char in for the hi surrogate |
| 467 | * and pretend we finished. |
| 468 | */ |
| 469 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 470 | } |
| 471 | } else if (IS_LOW_SURROGATE(tok->ucs_char)) { |
| 472 | /* Got a low surrogate not preceded by a high */ |
| 473 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 474 | } else if (tok->ucs_char < 0x10000) { |
| 475 | unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12); |
| 476 | unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f); |
| 477 | unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f); |
| 478 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 3); |
| 479 | } else if (tok->ucs_char < 0x110000) { |
| 480 | unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07); |
| 481 | unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f); |
| 482 | unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f); |
| 483 | unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f); |
| 484 | printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 4); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 485 | } else { |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 486 | /* Don't know what we got--insert the replacement char */ |
| 487 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
| 488 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 489 | state = saved_state; |
| 490 | break; |
| 491 | } |
| 492 | } else { |
| 493 | tok->err = json_tokener_error_parse_string; |
| 494 | goto out; |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 495 | } |
| 496 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 497 | if (got_hi_surrogate) /* Clean up any pending chars */ |
| 498 | printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 499 | goto out; |
Brent Miller | 126ad95 | 2009-08-20 06:50:22 +0000 | [diff] [blame] | 500 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 501 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 502 | } |
| 503 | break; |
| 504 | |
| 505 | case json_tokener_state_boolean: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 506 | printbuf_memappend_fast(tok->pb, &c, 1); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 507 | if(strncasecmp(json_true_str, tok->pb->buf, |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 508 | json_min(tok->st_pos+1, strlen(json_true_str))) == 0) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 509 | if(tok->st_pos == strlen(json_true_str)) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 510 | current = json_object_new_boolean(1); |
| 511 | saved_state = json_tokener_state_finish; |
| 512 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 513 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 514 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 515 | } else if(strncasecmp(json_false_str, tok->pb->buf, |
Michael Clark | 7fb9b03 | 2009-07-25 00:13:44 +0000 | [diff] [blame] | 516 | json_min(tok->st_pos+1, strlen(json_false_str))) == 0) { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 517 | if(tok->st_pos == strlen(json_false_str)) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 518 | current = json_object_new_boolean(0); |
| 519 | saved_state = json_tokener_state_finish; |
| 520 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 521 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 522 | } |
| 523 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 524 | tok->err = json_tokener_error_parse_boolean; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 525 | goto out; |
| 526 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 527 | tok->st_pos++; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 528 | break; |
| 529 | |
| 530 | case json_tokener_state_number: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 531 | { |
| 532 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 533 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 534 | int case_len=0; |
| 535 | while(c && strchr(json_number_chars, c)) { |
| 536 | ++case_len; |
| 537 | if(c == '.' || c == 'e') tok->is_double = 1; |
| 538 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 539 | printbuf_memappend_fast(tok->pb, case_start, case_len); |
| 540 | goto out; |
| 541 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 542 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 543 | if (case_len>0) |
| 544 | printbuf_memappend_fast(tok->pb, case_start, case_len); |
| 545 | } |
| 546 | { |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 547 | int64_t num64; |
| 548 | double numd; |
| 549 | if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) { |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame^] | 550 | current = json_object_new_int64(num64); |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 551 | } else if(tok->is_double && sscanf(tok->pb->buf, "%lf", &numd) == 1) { |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 552 | current = json_object_new_double(numd); |
| 553 | } else { |
| 554 | tok->err = json_tokener_error_parse_number; |
| 555 | goto out; |
| 556 | } |
| 557 | saved_state = json_tokener_state_finish; |
| 558 | state = json_tokener_state_eatws; |
| 559 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 560 | } |
| 561 | break; |
| 562 | |
| 563 | case json_tokener_state_array: |
| 564 | if(c == ']') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 565 | saved_state = json_tokener_state_finish; |
| 566 | state = json_tokener_state_eatws; |
| 567 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 568 | if(tok->depth >= JSON_TOKENER_MAX_DEPTH-1) { |
| 569 | tok->err = json_tokener_error_depth; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 570 | goto out; |
| 571 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 572 | state = json_tokener_state_array_add; |
| 573 | tok->depth++; |
| 574 | json_tokener_reset_level(tok, tok->depth); |
| 575 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 576 | } |
| 577 | break; |
| 578 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 579 | case json_tokener_state_array_add: |
| 580 | json_object_array_add(current, obj); |
| 581 | saved_state = json_tokener_state_array_sep; |
| 582 | state = json_tokener_state_eatws; |
| 583 | goto redo_char; |
| 584 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 585 | case json_tokener_state_array_sep: |
| 586 | if(c == ']') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 587 | saved_state = json_tokener_state_finish; |
| 588 | state = json_tokener_state_eatws; |
| 589 | } else if(c == ',') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 590 | saved_state = json_tokener_state_array; |
| 591 | state = json_tokener_state_eatws; |
| 592 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 593 | tok->err = json_tokener_error_parse_array; |
| 594 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 595 | } |
| 596 | break; |
| 597 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 598 | case json_tokener_state_object_field_start: |
| 599 | if(c == '}') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 600 | saved_state = json_tokener_state_finish; |
| 601 | state = json_tokener_state_eatws; |
| 602 | } else if (c == '"' || c == '\'') { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 603 | tok->quote_char = c; |
| 604 | printbuf_reset(tok->pb); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 605 | state = json_tokener_state_object_field; |
Michael Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 606 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 607 | tok->err = json_tokener_error_parse_object_key_name; |
Michael Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 608 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 609 | } |
| 610 | break; |
| 611 | |
| 612 | case json_tokener_state_object_field: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 613 | { |
| 614 | /* Advance until we change state */ |
Christopher Watford | b1a22ac | 2009-07-08 04:02:05 +0000 | [diff] [blame] | 615 | const char *case_start = str; |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 616 | while(1) { |
| 617 | if(c == tok->quote_char) { |
| 618 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 619 | obj_field_name = strdup(tok->pb->buf); |
| 620 | saved_state = json_tokener_state_object_field_end; |
| 621 | state = json_tokener_state_eatws; |
| 622 | break; |
| 623 | } else if(c == '\\') { |
| 624 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 625 | saved_state = json_tokener_state_object_field; |
| 626 | state = json_tokener_state_string_escape; |
| 627 | break; |
| 628 | } |
| 629 | if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) { |
| 630 | printbuf_memappend_fast(tok->pb, case_start, str-case_start); |
| 631 | goto out; |
| 632 | } |
| 633 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 634 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 635 | break; |
| 636 | |
| 637 | case json_tokener_state_object_field_end: |
| 638 | if(c == ':') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 639 | saved_state = json_tokener_state_object_value; |
| 640 | state = json_tokener_state_eatws; |
| 641 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 642 | tok->err = json_tokener_error_parse_object_key_sep; |
| 643 | goto out; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 644 | } |
| 645 | break; |
| 646 | |
| 647 | case json_tokener_state_object_value: |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 648 | if(tok->depth >= JSON_TOKENER_MAX_DEPTH-1) { |
| 649 | tok->err = json_tokener_error_depth; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 650 | goto out; |
| 651 | } |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 652 | state = json_tokener_state_object_value_add; |
| 653 | tok->depth++; |
| 654 | json_tokener_reset_level(tok, tok->depth); |
| 655 | goto redo_char; |
| 656 | |
| 657 | case json_tokener_state_object_value_add: |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 658 | json_object_object_add(current, obj_field_name, obj); |
| 659 | free(obj_field_name); |
| 660 | obj_field_name = NULL; |
| 661 | saved_state = json_tokener_state_object_sep; |
| 662 | state = json_tokener_state_eatws; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 663 | goto redo_char; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 664 | |
| 665 | case json_tokener_state_object_sep: |
| 666 | if(c == '}') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 667 | saved_state = json_tokener_state_finish; |
| 668 | state = json_tokener_state_eatws; |
| 669 | } else if(c == ',') { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 670 | saved_state = json_tokener_state_object_field_start; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 671 | state = json_tokener_state_eatws; |
| 672 | } else { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 673 | tok->err = json_tokener_error_parse_object_value_sep; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 674 | goto out; |
| 675 | } |
| 676 | break; |
| 677 | |
| 678 | } |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 679 | if (!ADVANCE_CHAR(str, tok)) |
| 680 | goto out; |
| 681 | } /* while(POP_CHAR) */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 682 | |
| 683 | out: |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 684 | if (!c) { /* We hit an eof char (0) */ |
| 685 | if(state != json_tokener_state_finish && |
| 686 | saved_state != json_tokener_state_finish) |
| 687 | tok->err = json_tokener_error_parse_eof; |
| 688 | } |
| 689 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 690 | if(tok->err == json_tokener_success) return json_object_get(current); |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 691 | MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n", |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 692 | json_tokener_errors[tok->err], tok->char_offset); |
| 693 | return NULL; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 694 | } |