2010-10-26 07:36:29 +08:00
|
|
|
/*
|
2011-01-21 03:09:14 +08:00
|
|
|
* Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
|
|
|
|
* Copyright (c) 2011 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
|
2010-10-26 07:36:29 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
#include "jansson_private.h"
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const fmt, va_list *ap)
|
|
|
|
{
|
2010-10-26 07:36:29 +08:00
|
|
|
json_t *root = NULL; /* root object */
|
|
|
|
json_t *obj = NULL;
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Scanner variables */
|
|
|
|
const char *tok = fmt;
|
|
|
|
const char *etok;
|
|
|
|
int etok_depth;
|
|
|
|
|
2010-10-26 07:36:29 +08:00
|
|
|
char *key = NULL; /* Current key in an object */
|
|
|
|
char *s;
|
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
int line = 1;
|
|
|
|
int column = 1;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Skip whitespace at the beginning of the string. */
|
|
|
|
while(size && *tok == ' ') {
|
|
|
|
tok++;
|
|
|
|
size--;
|
|
|
|
column++;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(size <= 0) {
|
|
|
|
jsonp_error_set(error, 1, 1, "Empty format string!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* tok must contain either a container type, or a length-1 string for a
|
|
|
|
* simple type. */
|
|
|
|
if(*tok == '[')
|
|
|
|
root = json_array();
|
|
|
|
else if(*tok == '{')
|
|
|
|
root = json_object();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Simple object. Permit trailing spaces, otherwise complain. */
|
|
|
|
if((ssize_t)strspn(tok+1, " ") < size-1)
|
|
|
|
{
|
|
|
|
jsonp_error_set(error, 1, 1,
|
|
|
|
"Expected a single object, got %i", size);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
switch(*tok)
|
|
|
|
{
|
|
|
|
case 's': /* string */
|
2011-01-21 03:09:14 +08:00
|
|
|
s = va_arg(*ap, char *);
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!s)
|
|
|
|
{
|
|
|
|
jsonp_error_set(error, 1, 1,
|
|
|
|
"Refusing to handle a NULL string");
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2011-01-21 03:09:14 +08:00
|
|
|
return json_string(s);
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case 'n': /* null */
|
2011-01-21 03:09:14 +08:00
|
|
|
return json_null();
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case 'b': /* boolean */
|
|
|
|
obj = va_arg(*ap, int) ?
|
|
|
|
json_true() : json_false();
|
2011-01-21 03:09:14 +08:00
|
|
|
return obj;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case 'i': /* integer */
|
2011-01-21 03:09:14 +08:00
|
|
|
return json_integer(va_arg(*ap, int));
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case 'f': /* double-precision float */
|
2011-01-21 03:09:14 +08:00
|
|
|
return json_real(va_arg(*ap, double));
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case 'O': /* a json_t object; increments refcount */
|
|
|
|
obj = va_arg(*ap, json_t *);
|
|
|
|
json_incref(obj);
|
2011-01-21 03:09:14 +08:00
|
|
|
return obj;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case 'o': /* a json_t object; doesn't increment refcount */
|
|
|
|
obj = va_arg(*ap, json_t *);
|
2011-01-21 03:09:14 +08:00
|
|
|
return obj;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
default: /* Whoops! */
|
|
|
|
jsonp_error_set(error, 1, 1,
|
|
|
|
"Didn't understand format character '%c'",
|
|
|
|
*tok);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move past container opening token */
|
|
|
|
tok++;
|
|
|
|
column++;
|
|
|
|
|
|
|
|
while(tok-fmt < size) {
|
|
|
|
switch(*tok) {
|
2010-10-26 07:36:29 +08:00
|
|
|
case '\n':
|
|
|
|
line++;
|
2011-01-21 03:09:14 +08:00
|
|
|
column = 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ': /* Whitespace */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ',': /* Element spacer */
|
|
|
|
if(key)
|
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
2010-10-27 04:36:24 +08:00
|
|
|
"Expected KEY, got COMMA!");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':': /* Key/value separator */
|
|
|
|
if(!key)
|
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
2010-10-27 04:36:24 +08:00
|
|
|
"Got key/value separator without "
|
|
|
|
"a key preceding it!");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!json_is_object(root))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
2010-10-27 04:36:24 +08:00
|
|
|
"Got a key/value separator "
|
|
|
|
"(':') outside an object!");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ']': /* Close array or object */
|
|
|
|
case '}':
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(tok-fmt + (ssize_t)strspn(tok+1, " ") != size-1)
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Unexpected close-bracket '%c'", *tok);
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if((*tok == ']' && !json_is_array(root)) ||
|
|
|
|
(*tok == '}' && !json_is_object(root)))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Stray close-array '%c' character", *tok);
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-21 03:09:14 +08:00
|
|
|
return root;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case '[':
|
|
|
|
case '{':
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Shortcut so we don't mess up the column count in error
|
|
|
|
* messages */
|
|
|
|
if(json_is_object(root) && !key)
|
|
|
|
goto common;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
if(*tok == *etok)
|
2011-01-15 01:18:42 +08:00
|
|
|
etok_depth++;
|
2011-01-21 03:09:14 +08:00
|
|
|
else if(*tok == '[' && *etok == ']') {
|
2011-01-15 01:18:42 +08:00
|
|
|
etok_depth--;
|
|
|
|
break;
|
2011-01-21 03:09:14 +08:00
|
|
|
} else if(*tok == '{' && *etok == '}') {
|
2011-01-15 01:18:42 +08:00
|
|
|
etok_depth--;
|
|
|
|
break;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
etok++;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Recurse */
|
|
|
|
obj = json_vnpack(error, etok-tok+1, tok, ap);
|
|
|
|
if(!obj) {
|
|
|
|
/* error should already be set */
|
|
|
|
error->column += column-1;
|
|
|
|
error->line += line-1;
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
|
|
|
column += etok-tok;
|
|
|
|
tok = etok;
|
|
|
|
goto common;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case 's':
|
|
|
|
/* Handle strings specially, since they're used for both keys
|
|
|
|
* and values */
|
2011-01-21 03:09:14 +08:00
|
|
|
s = va_arg(*ap, char *);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
|
|
|
if(!s)
|
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
2010-10-26 07:36:29 +08:00
|
|
|
"Refusing to handle a NULL string");
|
2011-01-15 01:18:42 +08:00
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(json_is_object(root) && !key)
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
|
|
|
/* It's a key */
|
|
|
|
key = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = json_string(s);
|
2011-01-15 01:18:42 +08:00
|
|
|
goto common;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
default:
|
|
|
|
obj = json_vnpack(error, 1, tok, ap);
|
|
|
|
if(!obj) {
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
common:
|
|
|
|
/* Add to container */
|
|
|
|
if(json_is_object(root)) {
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!key)
|
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Expected key, got identifier '%c'!", *tok);
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
json_object_set_new(root, key, obj);
|
2010-10-26 07:36:29 +08:00
|
|
|
key = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
json_array_append_new(root, obj);
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
tok++;
|
|
|
|
column++;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Whoops -- we didn't match the close bracket! */
|
|
|
|
jsonp_error_set(error, line, column, "Missing close array or object!");
|
|
|
|
json_decref(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const char *fmt, va_list *ap)
|
|
|
|
{
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
int rv = 0; /* Return value */
|
2010-10-26 07:36:29 +08:00
|
|
|
int line = 1; /* Line number */
|
2011-01-15 01:18:42 +08:00
|
|
|
int column = 1; /* Column */
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Position markers for arrays or objects */
|
2010-10-26 07:36:29 +08:00
|
|
|
int array_index = 0;
|
2011-01-15 01:18:42 +08:00
|
|
|
char *key = NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
const char **s;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Scanner variables */
|
|
|
|
const char *tok = fmt;
|
|
|
|
const char *etok;
|
|
|
|
int etok_depth;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
json_t *obj;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* If we're successful, we need to know if the number of arguments
|
|
|
|
* provided matches the number of JSON objects. We can do this by
|
|
|
|
* counting the elements in every array or object we open up, and
|
|
|
|
* decrementing the count as we visit their children. */
|
|
|
|
int unvisited = 0;
|
|
|
|
|
|
|
|
/* Skip whitespace at the beginning of the string. */
|
|
|
|
while(size && *tok == ' ') {
|
|
|
|
tok++;
|
|
|
|
size--;
|
|
|
|
column++;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(size <= 0) {
|
|
|
|
jsonp_error_set(error, 1, 1, "Empty format string!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* tok must contain either a container type, or a length-1 string for a
|
|
|
|
* simple type. */
|
|
|
|
if(*tok != '[' && *tok != '{')
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Simple object. Permit trailing spaces, otherwise complain. */
|
|
|
|
if((ssize_t)strspn(tok+1, " ") < size-1)
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, 1, 1,
|
|
|
|
"Expected a single object, got %i", size);
|
2011-01-21 03:09:14 +08:00
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
switch(*tok)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
if(!json_is_string(root))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Type mismatch! Object (%i) wasn't a string.",
|
|
|
|
json_typeof(root));
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
s = va_arg(*ap, const char **);
|
|
|
|
if(!s) {
|
|
|
|
jsonp_error_set(error, line, column, "Passed a NULL string pointer!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
*s = json_string_value(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case 'i':
|
|
|
|
if(!json_is_integer(root))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Type mismatch! Object (%i) wasn't an integer.",
|
|
|
|
json_typeof(root));
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
*va_arg(*ap, int*) = json_integer_value(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case 'b':
|
|
|
|
if(!json_is_boolean(root))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Type mismatch! Object (%i) wasn't a boolean.",
|
|
|
|
json_typeof(root));
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
*va_arg(*ap, int*) = json_is_true(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case 'f':
|
|
|
|
if(!json_is_number(root))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Type mismatch! Object (%i) wasn't a real.",
|
|
|
|
json_typeof(root));
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
*va_arg(*ap, double*) = json_number_value(root);
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case 'O':
|
|
|
|
json_incref(root);
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
*va_arg(*ap, json_t**) = root;
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case 'n':
|
|
|
|
/* Don't actually assign anything; we're just happy
|
|
|
|
* the null turned up as promised in the format
|
|
|
|
* string. */
|
2011-01-21 03:09:14 +08:00
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
default:
|
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Unknown format character '%c'", *tok);
|
2011-01-21 03:09:14 +08:00
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Move past container opening token */
|
|
|
|
tok++;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
while(tok-fmt < size) {
|
|
|
|
switch(*tok) {
|
|
|
|
case '\n':
|
|
|
|
line++;
|
2011-01-21 03:09:14 +08:00
|
|
|
column = 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
break;
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case ' ': /* Whitespace */
|
|
|
|
break;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case ',': /* Element spacer */
|
|
|
|
if(key)
|
|
|
|
{
|
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Expected KEY, got COMMA!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
|
|
|
break;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
case ':': /* Key/value separator */
|
|
|
|
if(!key)
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Got key/value separator without "
|
|
|
|
"a key preceding it!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!json_is_object(root))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Got a key/value separator "
|
|
|
|
"(':') outside an object!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ']': /* Close array or object */
|
|
|
|
case '}':
|
|
|
|
|
|
|
|
if(tok-fmt + (ssize_t)strspn(tok+1, " ") != size-1)
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Unexpected close-bracket '%c'", *tok);
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if((*tok == ']' && !json_is_array(root)) ||
|
|
|
|
(*tok == '}' && !json_is_object(root)))
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Stray close-array '%c' character", *tok);
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-21 03:09:14 +08:00
|
|
|
return unvisited;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
case '[':
|
|
|
|
case '{':
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Find corresponding close bracket */
|
|
|
|
etok = tok+1;
|
|
|
|
etok_depth = 1;
|
|
|
|
while(etok_depth) {
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!*etok || etok-fmt >= size) {
|
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Couldn't find matching close bracket for '%c'",
|
|
|
|
*tok);
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
if(*tok == *etok)
|
2011-01-15 01:18:42 +08:00
|
|
|
etok_depth++;
|
2011-01-21 03:09:14 +08:00
|
|
|
else if(*tok == '[' && *etok == ']') {
|
2011-01-15 01:18:42 +08:00
|
|
|
etok_depth--;
|
|
|
|
break;
|
2011-01-21 03:09:14 +08:00
|
|
|
} else if(*tok == '{' && *etok == '}') {
|
2011-01-15 01:18:42 +08:00
|
|
|
etok_depth--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
etok++;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(rv < 0) {
|
|
|
|
/* error should already be set */
|
|
|
|
error->column += column-1;
|
|
|
|
error->line += line-1;
|
2011-01-21 03:09:14 +08:00
|
|
|
return rv;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
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)
|
2010-10-26 07:36:29 +08:00
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
/* It's a key */
|
2011-01-21 03:09:14 +08:00
|
|
|
key = va_arg(*ap, char *);
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2010-10-26 07:36:29 +08:00
|
|
|
if(!key)
|
|
|
|
{
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Refusing to handle a NULL key");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
break;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
/* Fetch the element from the JSON container */
|
|
|
|
if(json_is_object(root))
|
|
|
|
obj = json_object_get(root, key);
|
2010-10-26 07:36:29 +08:00
|
|
|
else
|
2011-01-15 01:18:42 +08:00
|
|
|
obj = json_array_get(root, array_index++);
|
|
|
|
|
|
|
|
if(!obj) {
|
|
|
|
jsonp_error_set(error, line, column,
|
|
|
|
"Array/object entry didn't exist!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -1;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
rv = json_vnunpack(obj, error, 1, tok, ap);
|
|
|
|
if(rv != 0)
|
2011-01-21 03:09:14 +08:00
|
|
|
return rv;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tok++;
|
|
|
|
column++;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
/* Whoops -- we didn't match the close bracket! */
|
|
|
|
jsonp_error_set(error, line, column, "Missing close array or object!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
json_t *obj;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_init(error, "");
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!fmt || !*fmt) {
|
|
|
|
jsonp_error_set(error, 1, 1, "Null or empty format string!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
va_start(ap, fmt);
|
2011-01-21 03:09:14 +08:00
|
|
|
obj = json_vpack(error, fmt, &ap);
|
2011-01-15 01:18:42 +08:00
|
|
|
va_end(ap);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
return obj;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int rv;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
jsonp_error_init(error, "");
|
|
|
|
|
|
|
|
if(!fmt || !*fmt) {
|
|
|
|
jsonp_error_set(error, 1, 1, "Null or empty format string!");
|
2011-01-21 03:09:14 +08:00
|
|
|
return -2;;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
va_start(ap, fmt);
|
|
|
|
rv = json_vnunpack(root, error, strlen(fmt), fmt, &ap);
|
|
|
|
va_end(ap);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-21 03:09:14 +08:00
|
|
|
return rv;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|