- /* Move past container opening token */
- tok++;
-
- while(tok-fmt < size) {
- switch(*tok) {
- case '\n':
- line++;
- column = 0;
- break;
-
- case ' ': /* Whitespace */
- break;
-
- case ',': /* Element spacer */
- if(key)
- {
- jsonp_error_set(error, line, column,
- "Expected KEY, got COMMA!");
- return -2;
- }
- break;
-
- case ':': /* Key/value separator */
- if(!key)
- {
- jsonp_error_set(error, line, column,
- "Got key/value separator without "
- "a key preceding it!");
- return -2;
- }
-
- if(!json_is_object(root))
- {
- jsonp_error_set(error, line, column,
- "Got a key/value separator "
- "(':') outside an object!");
- return -2;
- }
-
- break;
-
- case ']': /* Close array or object */
- case '}':
-
- if(tok-fmt + (ssize_t)strspn(tok+1, " ") != size-1)
- {
- jsonp_error_set(error, line, column,
- "Unexpected close-bracket '%c'", *tok);
- return -2;
- }
-
- if((*tok == ']' && !json_is_array(root)) ||
- (*tok == '}' && !json_is_object(root)))
- {
- jsonp_error_set(error, line, column,
- "Stray close-array '%c' character", *tok);
- return -2;
- }
- return unvisited;
-
- case '[':
- case '{':
-
- /* Find corresponding close bracket */
- etok = tok+1;
- etok_depth = 1;
- while(etok_depth) {
-
- if(!*etok || etok-fmt >= size) {
- jsonp_error_set(error, line, column,
- "Couldn't find matching close bracket for '%c'",
- *tok);
- return -2;
- }
-
- if(*tok == *etok)
- etok_depth++;
- else if(*tok == '[' && *etok == ']') {
- etok_depth--;
- break;
- } else if(*tok == '{' && *etok == '}') {
- etok_depth--;
- break;
- }
-
- etok++;
- }
-
- /* Recurse */
- if(json_is_array(root)) {
- rv = json_vnunpack(json_object_get(root, key),
- error, etok-tok+1, tok, ap);
- } else {
- rv = json_vnunpack(json_array_get(root, array_index++),
- error, etok-tok+1, tok, ap);
- }
-
- if(rv < 0) {
- /* error should already be set */
- error->column += column-1;
- error->line += line-1;
- return rv;
- }
-
- unvisited += rv;
- column += etok-tok;
- tok = etok;
- break;
-
- case 's':
- /* Handle strings specially, since they're used for both keys
- * and values */
-
- if(json_is_object(root) && !key)
- {
- /* It's a key */
- key = va_arg(*ap, char *);
-
- if(!key)
- {
- jsonp_error_set(error, line, column,
- "Refusing to handle a NULL key");
- return -2;
- }
- break;
- }
-
- /* Fall through */
-
- default:
-
- /* Fetch the element from the JSON container */
- if(json_is_object(root))
- obj = json_object_get(root, key);
- else
- obj = json_array_get(root, array_index++);
-
- if(!obj) {
- jsonp_error_set(error, line, column,
- "Array/object entry didn't exist!");
- return -1;
- }
-
- rv = json_vnunpack(obj, error, 1, tok, ap);
- if(rv != 0)
- return rv;
-
- break;
- }
- tok++;
- column++;