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-24 03:14:19 +08:00
|
|
|
typedef struct {
|
|
|
|
const char *fmt;
|
|
|
|
char token;
|
|
|
|
json_error_t *error;
|
|
|
|
int line;
|
|
|
|
int column;
|
|
|
|
} scanner_t;
|
|
|
|
|
|
|
|
static void next_token(scanner_t *s)
|
2011-01-15 01:18:42 +08:00
|
|
|
{
|
2011-01-24 03:14:19 +08:00
|
|
|
const char *t = s->fmt;
|
|
|
|
s->column++;
|
|
|
|
|
|
|
|
/* skip space and ignored chars */
|
|
|
|
while(*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
|
|
|
|
if(*t == '\n') {
|
|
|
|
s->line++;
|
|
|
|
s->column = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s->column++;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
t++;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
s->token = *t;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
t++;
|
|
|
|
s->fmt = t;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static void set_error(scanner_t *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
jsonp_error_vset(s->error, s->line, s->column, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static json_t *pack(scanner_t *s, va_list *ap);
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static json_t *pack_object(scanner_t *s, va_list *ap)
|
|
|
|
{
|
|
|
|
json_t *object = json_object();
|
|
|
|
next_token(s);
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
while(s->token != '}') {
|
|
|
|
const char *key;
|
|
|
|
json_t *value;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
if(!s->token) {
|
|
|
|
set_error(s, "Unexpected end of format string");
|
|
|
|
goto error;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
if(s->token != 's') {
|
|
|
|
set_error(s, "Expected format 's', got '%c'\n", *s->fmt);
|
|
|
|
goto error;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
key = va_arg(*ap, const char *);
|
|
|
|
if(!key) {
|
|
|
|
set_error(s, "NULL object key");
|
|
|
|
goto error;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(s);
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
value = pack(s, ap);
|
|
|
|
if(!value)
|
|
|
|
goto error;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
if(json_object_set_new(object, key, value)) {
|
|
|
|
set_error(s, "Unable to add key \"%s\"", key);
|
|
|
|
goto error;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(s);
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
return object;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
error:
|
|
|
|
json_decref(object);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static json_t *pack_array(scanner_t *s, va_list *ap)
|
|
|
|
{
|
|
|
|
json_t *array = json_array();
|
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
while(s->token != ']') {
|
|
|
|
json_t *value;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
if(!s->token) {
|
|
|
|
set_error(s, "Unexpected end of format string");
|
|
|
|
goto error;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
value = pack(s, ap);
|
|
|
|
if(!value)
|
|
|
|
goto error;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
if(json_array_append_new(array, value)) {
|
|
|
|
set_error(s, "Unable to append to array");
|
|
|
|
goto error;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(s);
|
|
|
|
}
|
|
|
|
return array;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
error:
|
|
|
|
json_decref(array);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static json_t *pack(scanner_t *s, va_list *ap)
|
|
|
|
{
|
|
|
|
switch(s->token) {
|
|
|
|
case '{':
|
|
|
|
return pack_object(s, ap);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case '[':
|
|
|
|
return pack_array(s, ap);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 's': /* string */
|
|
|
|
{
|
|
|
|
const char *str = va_arg(*ap, const char *);
|
|
|
|
if(!str)
|
|
|
|
{
|
|
|
|
set_error(s, "NULL string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return json_string(str);
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 'n': /* null */
|
|
|
|
return json_null();
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 'b': /* boolean */
|
|
|
|
return va_arg(*ap, int) ? json_true() : json_false();
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 'i': /* integer */
|
|
|
|
return json_integer(va_arg(*ap, int));
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 'f': /* double-precision float */
|
|
|
|
return json_real(va_arg(*ap, double));
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 'O': /* a json_t object; increments refcount */
|
|
|
|
return json_incref(va_arg(*ap, json_t *));
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
case 'o': /* a json_t object; doesn't increment refcount */
|
|
|
|
return va_arg(*ap, json_t *);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
default: /* Whoops! */
|
|
|
|
set_error(s, "Unrecognized format character '%c'", s->token);
|
|
|
|
return NULL;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
static int unpack(scanner_t *s, json_t *root, va_list *ap);
|
|
|
|
|
|
|
|
static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
|
2011-01-15 01:18:42 +08:00
|
|
|
{
|
2011-01-25 03:45:54 +08:00
|
|
|
int ret = -1;
|
|
|
|
int wildcard = 0;
|
|
|
|
hashtable_t key_set;
|
|
|
|
|
|
|
|
if(hashtable_init(&key_set, jsonp_hash_key, jsonp_key_equal, NULL, NULL)) {
|
|
|
|
set_error(s, "Out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
if(!json_is_object(root)) {
|
|
|
|
set_error(s, "Expected object, got %i", json_typeof(root));
|
2011-01-25 03:45:54 +08:00
|
|
|
goto error;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
while(s->token != '}') {
|
|
|
|
const char *key;
|
|
|
|
json_t *value;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:45:54 +08:00
|
|
|
if(wildcard) {
|
|
|
|
set_error(s, "Expected '}', got '%c'", s->token);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
if(!s->token) {
|
|
|
|
set_error(s, "Unexpected end of format string");
|
2011-01-25 03:45:54 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->token == '*') {
|
|
|
|
wildcard = 1;
|
|
|
|
next_token(s);
|
|
|
|
continue;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
if(s->token != 's') {
|
|
|
|
set_error(s, "Expected format 's', got '%c'\n", *s->fmt);
|
2011-01-25 03:45:54 +08:00
|
|
|
goto error;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
key = va_arg(*ap, const char *);
|
|
|
|
if(!key) {
|
|
|
|
set_error(s, "NULL object key");
|
2011-01-25 03:45:54 +08:00
|
|
|
goto error;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
value = json_object_get(root, key);
|
|
|
|
if(unpack(s, value, ap))
|
2011-01-25 03:45:54 +08:00
|
|
|
goto error;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-25 03:45:54 +08:00
|
|
|
hashtable_set(&key_set, (void *)key, NULL);
|
2011-01-25 03:20:56 +08:00
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-25 03:45:54 +08:00
|
|
|
if(!wildcard && key_set.size != json_object_size(root)) {
|
|
|
|
long diff = (long)json_object_size(root) - (long)key_set.size;
|
|
|
|
set_error(s, "%li object items left unpacked", diff);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
hashtable_close(&key_set);
|
|
|
|
return ret;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
2011-01-25 03:45:54 +08:00
|
|
|
int wildcard = 0;
|
2011-01-25 03:20:56 +08:00
|
|
|
|
|
|
|
if(!json_is_array(root)) {
|
|
|
|
set_error(s, "Expected array, got %d", json_typeof(root));
|
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2011-01-25 03:20:56 +08:00
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
while(s->token != ']') {
|
|
|
|
json_t *value;
|
|
|
|
|
2011-01-25 03:45:54 +08:00
|
|
|
if(wildcard) {
|
|
|
|
set_error(s, "Expected ']', got '%c'", s->token);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
if(!s->token) {
|
|
|
|
set_error(s, "Unexpected end of format string");
|
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-25 03:45:54 +08:00
|
|
|
if(s->token == '*') {
|
|
|
|
wildcard = 1;
|
|
|
|
next_token(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
value = json_array_get(root, i);
|
|
|
|
if(!value) {
|
|
|
|
set_error(s, "Array index %lu out of range", (unsigned long)i);
|
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2011-01-25 03:20:56 +08:00
|
|
|
|
|
|
|
if(unpack(s, value, ap))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
next_token(s);
|
|
|
|
i++;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:45:54 +08:00
|
|
|
if(!wildcard && i != json_array_size(root)) {
|
2011-01-25 03:20:56 +08:00
|
|
|
long diff = (long)json_array_size(root) - (long)i;
|
2011-01-25 03:45:54 +08:00
|
|
|
set_error(s, "%li array items left upacked", diff);
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
|
|
|
{
|
|
|
|
switch(s->token)
|
|
|
|
{
|
|
|
|
case '{':
|
|
|
|
return unpack_object(s, root, ap);
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
return unpack_array(s, root, ap);
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
const char **str;
|
|
|
|
|
|
|
|
if(!json_is_string(root))
|
|
|
|
{
|
|
|
|
set_error(s, "Type mismatch! Object (%i) wasn't a string.",
|
|
|
|
json_typeof(root));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = va_arg(*ap, const char **);
|
|
|
|
if(!str) {
|
|
|
|
set_error(s, "Passed a NULL string pointer!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*str = json_string_value(root);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
if(!json_is_integer(root))
|
|
|
|
{
|
|
|
|
set_error(s, "Type mismatch! Object (%i) wasn't an integer.",
|
|
|
|
json_typeof(root));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*va_arg(*ap, int*) = json_integer_value(root);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
if(!json_is_boolean(root))
|
|
|
|
{
|
|
|
|
set_error(s, "Type mismatch! Object (%i) wasn't a boolean.",
|
|
|
|
json_typeof(root));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*va_arg(*ap, int*) = json_is_true(root);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
if(!json_is_number(root))
|
|
|
|
{
|
|
|
|
set_error(s, "Type mismatch! Object (%i) wasn't a real.",
|
|
|
|
json_typeof(root));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*va_arg(*ap, double*) = json_number_value(root);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
json_incref(root);
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
*va_arg(*ap, json_t**) = root;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
/* Don't assign, just validate */
|
|
|
|
if(!json_is_null(root))
|
|
|
|
{
|
|
|
|
set_error(s, "Type mismatch! Object (%i) wasn't null.",
|
|
|
|
json_typeof(root));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_error(s, "Unknown format character '%c'", s->token);
|
|
|
|
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
|
|
|
json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
|
|
|
{
|
2011-01-24 03:14:19 +08:00
|
|
|
scanner_t s;
|
|
|
|
json_t *value;
|
2011-01-15 01:18:42 +08:00
|
|
|
va_list ap;
|
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-24 03:14:19 +08:00
|
|
|
s.error = error;
|
|
|
|
s.fmt = fmt;
|
|
|
|
s.line = 1;
|
|
|
|
s.column = 0;
|
|
|
|
|
|
|
|
next_token(&s);
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
va_start(ap, fmt);
|
2011-01-24 03:14:19 +08:00
|
|
|
value = pack(&s, &ap);
|
2011-01-15 01:18:42 +08:00
|
|
|
va_end(ap);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(&s);
|
|
|
|
if(s.token) {
|
|
|
|
set_error(&s, "Garbage after format string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
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, ...)
|
|
|
|
{
|
2011-01-25 03:20:56 +08:00
|
|
|
scanner_t s;
|
2011-01-15 01:18:42 +08:00
|
|
|
va_list ap;
|
2011-01-25 03:20:56 +08:00
|
|
|
int result;
|
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-25 03:20:56 +08:00
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
s.error = error;
|
|
|
|
s.fmt = fmt;
|
|
|
|
s.line = 1;
|
|
|
|
s.column = 0;
|
|
|
|
|
|
|
|
next_token(&s);
|
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
va_start(ap, fmt);
|
2011-01-25 03:20:56 +08:00
|
|
|
result = unpack(&s, root, &ap);
|
2011-01-15 01:18:42 +08:00
|
|
|
va_end(ap);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
if(result)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
next_token(&s);
|
|
|
|
if(s.token) {
|
|
|
|
set_error(&s, "Garbage after format string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|