2010-10-26 07:36:29 +08:00
|
|
|
/*
|
2016-09-18 19:17:03 +08:00
|
|
|
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
|
2012-03-21 02:55:55 +08:00
|
|
|
* Copyright (c) 2011-2012 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.
|
|
|
|
*/
|
|
|
|
|
2011-02-23 01:08:41 +08:00
|
|
|
#include <string.h>
|
2012-04-30 03:09:29 +08:00
|
|
|
#include "jansson.h"
|
2010-10-26 07:36:29 +08:00
|
|
|
#include "jansson_private.h"
|
2011-02-28 03:34:12 +08:00
|
|
|
#include "utf.h"
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
typedef struct {
|
|
|
|
int line;
|
|
|
|
int column;
|
|
|
|
size_t pos;
|
|
|
|
char token;
|
|
|
|
} token_t;
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
typedef struct {
|
2011-02-22 16:47:02 +08:00
|
|
|
const char *start;
|
2011-01-24 03:14:19 +08:00
|
|
|
const char *fmt;
|
2013-08-15 02:54:11 +08:00
|
|
|
token_t prev_token;
|
|
|
|
token_t token;
|
|
|
|
token_t next_token;
|
2011-01-24 03:14:19 +08:00
|
|
|
json_error_t *error;
|
2011-01-25 15:36:43 +08:00
|
|
|
size_t flags;
|
2011-01-24 03:14:19 +08:00
|
|
|
int line;
|
|
|
|
int column;
|
2013-08-15 02:54:11 +08:00
|
|
|
size_t pos;
|
2017-11-07 11:48:50 +08:00
|
|
|
int has_error;
|
2011-01-24 03:14:19 +08:00
|
|
|
} scanner_t;
|
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
#define token(scanner) ((scanner)->token.token)
|
|
|
|
|
2012-12-06 08:46:14 +08:00
|
|
|
static const char * const type_names[] = {
|
2011-01-26 02:37:53 +08:00
|
|
|
"object",
|
|
|
|
"array",
|
|
|
|
"string",
|
|
|
|
"integer",
|
|
|
|
"real",
|
|
|
|
"true",
|
|
|
|
"false",
|
|
|
|
"null"
|
|
|
|
};
|
|
|
|
|
|
|
|
#define type_name(x) type_names[json_typeof(x)]
|
|
|
|
|
2012-12-06 08:50:08 +08:00
|
|
|
static const char unpack_value_starters[] = "{[siIbfFOon";
|
2011-02-23 01:08:41 +08:00
|
|
|
|
|
|
|
static void scanner_init(scanner_t *s, json_error_t *error,
|
|
|
|
size_t flags, const char *fmt)
|
|
|
|
{
|
|
|
|
s->error = error;
|
|
|
|
s->flags = flags;
|
|
|
|
s->fmt = s->start = fmt;
|
2013-08-15 02:54:11 +08:00
|
|
|
memset(&s->prev_token, 0, sizeof(token_t));
|
|
|
|
memset(&s->token, 0, sizeof(token_t));
|
|
|
|
memset(&s->next_token, 0, sizeof(token_t));
|
2011-02-23 01:08:41 +08:00
|
|
|
s->line = 1;
|
|
|
|
s->column = 0;
|
2013-08-15 02:54:11 +08:00
|
|
|
s->pos = 0;
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 0;
|
2011-02-23 01:08:41 +08:00
|
|
|
}
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static void next_token(scanner_t *s)
|
2011-01-15 01:18:42 +08:00
|
|
|
{
|
2013-08-15 02:54:11 +08:00
|
|
|
const char *t;
|
|
|
|
s->prev_token = s->token;
|
|
|
|
|
|
|
|
if(s->next_token.line) {
|
|
|
|
s->token = s->next_token;
|
|
|
|
s->next_token.line = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-10 03:26:35 +08:00
|
|
|
if (!token(s) && !*s->fmt)
|
|
|
|
return;
|
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
t = s->fmt;
|
2011-01-24 03:14:19 +08:00
|
|
|
s->column++;
|
2013-08-15 02:54:11 +08:00
|
|
|
s->pos++;
|
2011-01-24 03:14:19 +08:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
s->pos++;
|
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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
s->token.token = *t;
|
|
|
|
s->token.line = s->line;
|
|
|
|
s->token.column = s->column;
|
|
|
|
s->token.pos = s->pos;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2018-07-10 03:26:35 +08:00
|
|
|
if (*t) t++;
|
2011-01-24 03:14:19 +08:00
|
|
|
s->fmt = t;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
static void prev_token(scanner_t *s)
|
|
|
|
{
|
|
|
|
s->next_token = s->token;
|
|
|
|
s->token = s->prev_token;
|
|
|
|
}
|
|
|
|
|
2017-10-03 17:42:07 +08:00
|
|
|
static void set_error(scanner_t *s, const char *source, enum json_error_code code,
|
|
|
|
const char *fmt, ...)
|
2011-01-24 03:14:19 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2011-02-22 16:47:02 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
jsonp_error_vset(s->error, s->token.line, s->token.column, s->token.pos,
|
2017-10-03 17:42:07 +08:00
|
|
|
code, fmt, ap);
|
2011-02-22 16:47:02 +08:00
|
|
|
|
2011-02-23 01:08:41 +08:00
|
|
|
jsonp_error_set_source(s->error, source);
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
|
|
|
|
/* ours will be set to 1 if jsonp_free() must be called for the result
|
|
|
|
afterwards */
|
|
|
|
static char *read_string(scanner_t *s, va_list *ap,
|
2018-09-26 02:31:56 +08:00
|
|
|
const char *purpose, size_t *out_len, int *ours, int optional)
|
2013-08-15 02:54:11 +08:00
|
|
|
{
|
|
|
|
char t;
|
|
|
|
strbuffer_t strbuff;
|
|
|
|
const char *str;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
next_token(s);
|
|
|
|
t = token(s);
|
|
|
|
prev_token(s);
|
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
*ours = 0;
|
2012-04-04 09:00:29 +08:00
|
|
|
if(t != '#' && t != '%' && t != '+') {
|
2013-08-15 02:54:11 +08:00
|
|
|
/* Optimize the simple case */
|
|
|
|
str = va_arg(*ap, const char *);
|
|
|
|
|
|
|
|
if(!str) {
|
2018-09-26 02:31:56 +08:00
|
|
|
if (!optional) {
|
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
|
|
|
|
s->has_error = 1;
|
|
|
|
}
|
2013-08-15 02:54:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
length = strlen(str);
|
|
|
|
|
|
|
|
if(!utf8_check_string(str, length)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
|
2018-09-26 02:31:56 +08:00
|
|
|
s->has_error = 1;
|
2013-08-15 02:54:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
*out_len = length;
|
2013-08-15 02:54:11 +08:00
|
|
|
return (char *)str;
|
2018-09-26 02:31:56 +08:00
|
|
|
} else if (optional) {
|
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Cannot use '%c' on optional strings", t);
|
|
|
|
s->has_error = 1;
|
|
|
|
|
|
|
|
return NULL;
|
2013-08-15 02:54:11 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 17:35:37 +08:00
|
|
|
if(strbuffer_init(&strbuff)) {
|
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
|
|
|
|
s->has_error = 1;
|
|
|
|
}
|
2013-08-15 02:54:11 +08:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
str = va_arg(*ap, const char *);
|
|
|
|
if(!str) {
|
2018-09-26 02:31:56 +08:00
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2013-08-15 02:54:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
next_token(s);
|
|
|
|
|
|
|
|
if(token(s) == '#') {
|
|
|
|
length = va_arg(*ap, int);
|
|
|
|
}
|
2012-04-04 09:00:29 +08:00
|
|
|
else if(token(s) == '%') {
|
|
|
|
length = va_arg(*ap, size_t);
|
|
|
|
}
|
2013-08-15 02:54:11 +08:00
|
|
|
else {
|
|
|
|
prev_token(s);
|
2017-11-07 11:48:50 +08:00
|
|
|
length = s->has_error ? 0 : strlen(str);
|
2013-08-15 02:54:11 +08:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
if(!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2013-08-15 02:54:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
next_token(s);
|
|
|
|
if(token(s) != '+') {
|
|
|
|
prev_token(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
if(s->has_error) {
|
|
|
|
strbuffer_close(&strbuff);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
if(!utf8_check_string(strbuff.value, strbuff.length)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
|
2012-04-04 09:00:29 +08:00
|
|
|
strbuffer_close(&strbuff);
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2013-08-15 02:54:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
*out_len = strbuff.length;
|
2013-08-15 02:54:11 +08:00
|
|
|
*ours = 1;
|
2012-04-04 09:00:29 +08:00
|
|
|
return strbuffer_steal_value(&strbuff);
|
2013-08-15 02:54:11 +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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
while(token(s) != '}') {
|
|
|
|
char *key;
|
2012-04-04 09:00:29 +08:00
|
|
|
size_t len;
|
2013-08-15 02:54:11 +08:00
|
|
|
int ours;
|
2011-01-24 03:14:19 +08:00
|
|
|
json_t *value;
|
2018-09-26 02:31:56 +08:00
|
|
|
char valueOptional;
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(!token(s)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
|
2011-01-24 03:14:19 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(s) != 's') {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
|
2011-01-24 03:14:19 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2018-09-26 02:31:56 +08:00
|
|
|
key = read_string(s, ap, "object key", &len, &ours, 0);
|
2011-02-28 03:34:12 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(s);
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2018-09-26 02:31:56 +08:00
|
|
|
next_token(s);
|
|
|
|
valueOptional = token(s);
|
|
|
|
prev_token(s);
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
value = pack(s, ap);
|
2012-04-04 09:00:29 +08:00
|
|
|
if(!value) {
|
|
|
|
if(ours)
|
|
|
|
jsonp_free(key);
|
|
|
|
|
2018-09-26 02:31:56 +08:00
|
|
|
if(valueOptional != '*') {
|
2018-09-26 05:34:25 +08:00
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL object value");
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2017-04-18 23:05:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
next_token(s);
|
|
|
|
continue;
|
2012-04-04 09:00:29 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
if(s->has_error)
|
|
|
|
json_decref(value);
|
2013-08-15 02:54:11 +08:00
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
if(!s->has_error && json_object_set_new_nocheck(object, key, value)) {
|
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Unable to add key \"%s\"", key);
|
|
|
|
s->has_error = 1;
|
2011-01-24 03:14:19 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(ours)
|
|
|
|
jsonp_free(key);
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(s);
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
if(!s->has_error)
|
|
|
|
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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
while(token(s) != ']') {
|
2011-01-24 03:14:19 +08:00
|
|
|
json_t *value;
|
2018-09-26 02:31:56 +08:00
|
|
|
char valueOptional;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(!token(s)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
|
2017-11-07 11:48:50 +08:00
|
|
|
/* Format string errors are unrecoverable. */
|
2011-01-24 03:14:19 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2018-09-26 02:31:56 +08:00
|
|
|
next_token(s);
|
|
|
|
valueOptional = token(s);
|
|
|
|
prev_token(s);
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
value = pack(s, ap);
|
2017-04-18 23:05:38 +08:00
|
|
|
if(!value) {
|
2018-09-26 02:31:56 +08:00
|
|
|
if(valueOptional != '*') {
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2017-04-18 23:05:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
next_token(s);
|
|
|
|
continue;
|
2017-04-18 23:05:38 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2017-11-07 11:48:50 +08:00
|
|
|
if(s->has_error)
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
if(!s->has_error && json_array_append_new(array, value)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Unable to append to array");
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2011-01-24 03:14:19 +08:00
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(s);
|
|
|
|
}
|
2017-11-07 11:48:50 +08:00
|
|
|
|
|
|
|
if(!s->has_error)
|
|
|
|
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
|
|
|
|
2016-02-23 05:32:20 +08:00
|
|
|
static json_t *pack_string(scanner_t *s, va_list *ap)
|
|
|
|
{
|
|
|
|
char *str;
|
2018-09-26 02:31:56 +08:00
|
|
|
char t;
|
2016-02-23 05:32:20 +08:00
|
|
|
size_t len;
|
|
|
|
int ours;
|
2018-09-26 02:31:56 +08:00
|
|
|
int optional;
|
2016-02-23 05:32:20 +08:00
|
|
|
|
|
|
|
next_token(s);
|
2018-09-26 02:31:56 +08:00
|
|
|
t = token(s);
|
|
|
|
optional = t == '?' || t == '*';
|
|
|
|
if (!optional)
|
2016-02-23 05:32:20 +08:00
|
|
|
prev_token(s);
|
|
|
|
|
2018-09-26 02:31:56 +08:00
|
|
|
str = read_string(s, ap, "string", &len, &ours, optional);
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return t == '?' && !s->has_error ? json_null() : NULL;
|
|
|
|
|
|
|
|
if (s->has_error) {
|
2018-11-06 05:43:10 +08:00
|
|
|
/* It's impossible to reach this point if ours != 0, do not free str. */
|
2018-09-26 02:31:56 +08:00
|
|
|
return NULL;
|
2016-02-23 05:32:20 +08:00
|
|
|
}
|
2018-09-26 02:31:56 +08:00
|
|
|
|
|
|
|
if (ours)
|
|
|
|
return jsonp_stringn_nocheck_own(str, len);
|
|
|
|
|
|
|
|
return json_stringn_nocheck(str, len);
|
2016-02-23 05:32:20 +08:00
|
|
|
}
|
|
|
|
|
2018-03-12 22:39:04 +08:00
|
|
|
static json_t *pack_object_inter(scanner_t *s, va_list *ap, int need_incref)
|
|
|
|
{
|
|
|
|
json_t *json;
|
|
|
|
char ntoken;
|
|
|
|
|
|
|
|
next_token(s);
|
|
|
|
ntoken = token(s);
|
|
|
|
|
2018-09-26 02:31:56 +08:00
|
|
|
if (ntoken != '?' && ntoken != '*')
|
2018-03-12 22:39:04 +08:00
|
|
|
prev_token(s);
|
|
|
|
|
|
|
|
json = va_arg(*ap, json_t *);
|
|
|
|
|
|
|
|
if (json)
|
|
|
|
return need_incref ? json_incref(json) : json;
|
|
|
|
|
|
|
|
switch (ntoken) {
|
|
|
|
case '?':
|
|
|
|
return json_null();
|
|
|
|
case '*':
|
|
|
|
return NULL;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:34:25 +08:00
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL object");
|
2018-03-12 22:39:04 +08:00
|
|
|
s->has_error = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:34:25 +08:00
|
|
|
static json_t *pack_integer(scanner_t *s, json_int_t value)
|
|
|
|
{
|
|
|
|
json_t *json = json_integer(value);
|
|
|
|
|
|
|
|
if (!json) {
|
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
|
|
|
|
s->has_error = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_t *pack_real(scanner_t *s, double value)
|
|
|
|
{
|
|
|
|
/* Allocate without setting value so we can identify OOM error. */
|
|
|
|
json_t *json = json_real(0.0);
|
|
|
|
|
|
|
|
if (!json) {
|
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
|
|
|
|
s->has_error = 1;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_real_set(json, value)) {
|
|
|
|
json_decref(json);
|
|
|
|
|
|
|
|
set_error(s, "<args>", json_error_numeric_overflow, "Invalid floating point value");
|
|
|
|
s->has_error = 1;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
static json_t *pack(scanner_t *s, va_list *ap)
|
|
|
|
{
|
2013-08-15 02:54:11 +08:00
|
|
|
switch(token(s)) {
|
2011-01-24 03:14:19 +08:00
|
|
|
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
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
case 's': /* string */
|
2016-02-23 05:32:20 +08:00
|
|
|
return pack_string(s, ap);
|
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-26 03:41:35 +08:00
|
|
|
case 'i': /* integer from int */
|
2018-09-26 05:34:25 +08:00
|
|
|
return pack_integer(s, va_arg(*ap, int));
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-26 03:41:35 +08:00
|
|
|
case 'I': /* integer from json_int_t */
|
2018-09-26 05:34:25 +08:00
|
|
|
return pack_integer(s, va_arg(*ap, json_int_t));
|
2011-01-26 03:41:35 +08:00
|
|
|
|
2011-01-26 02:37:53 +08:00
|
|
|
case 'f': /* real */
|
2018-09-26 05:34:25 +08:00
|
|
|
return pack_real(s, 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 */
|
2018-03-12 22:39:04 +08:00
|
|
|
return pack_object_inter(s, ap, 1);
|
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 */
|
2018-03-12 22:39:04 +08:00
|
|
|
return pack_object_inter(s, ap, 0);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-26 02:37:53 +08:00
|
|
|
default:
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
|
2013-08-15 02:54:11 +08:00
|
|
|
token(s));
|
2017-11-07 11:48:50 +08:00
|
|
|
s->has_error = 1;
|
2011-01-24 03:14:19 +08:00
|
|
|
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;
|
2011-01-30 03:38:07 +08:00
|
|
|
int strict = 0;
|
2014-02-16 00:44:02 +08:00
|
|
|
int gotopt = 0;
|
2011-01-25 03:45:54 +08:00
|
|
|
|
2011-01-25 15:36:43 +08:00
|
|
|
/* Use a set (emulated by a hashtable) to check that all object
|
|
|
|
keys are accessed. Checking that the correct number of keys
|
|
|
|
were accessed is not enough, as the same key can be unpacked
|
|
|
|
multiple times.
|
|
|
|
*/
|
2011-01-30 03:38:07 +08:00
|
|
|
hashtable_t key_set;
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2012-01-25 02:36:31 +08:00
|
|
|
if(hashtable_init(&key_set)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
|
2011-01-30 03:38:07 +08:00
|
|
|
return -1;
|
2011-01-25 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_object(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected object, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-30 03:38:07 +08:00
|
|
|
goto out;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
while(token(s) != '}') {
|
2011-01-25 03:20:56 +08:00
|
|
|
const char *key;
|
|
|
|
json_t *value;
|
2012-01-27 03:13:07 +08:00
|
|
|
int opt = 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-30 03:38:07 +08:00
|
|
|
if(strict != 0) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Expected '}' after '%c', got '%c'",
|
2013-08-15 02:54:11 +08:00
|
|
|
(strict == 1 ? '!' : '*'), token(s));
|
2011-01-30 03:38:07 +08:00
|
|
|
goto out;
|
2011-01-25 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(!token(s)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
|
2011-01-30 03:38:07 +08:00
|
|
|
goto out;
|
2011-01-25 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(s) == '!' || token(s) == '*') {
|
|
|
|
strict = (token(s) == '!' ? 1 : -1);
|
2011-01-25 03:45:54 +08:00
|
|
|
next_token(s);
|
|
|
|
continue;
|
2011-01-25 03:20:56 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(s) != 's') {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
|
2011-01-30 03:38:07 +08:00
|
|
|
goto out;
|
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) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL object key");
|
2011-01-30 03:38:07 +08:00
|
|
|
goto out;
|
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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(s) == '?') {
|
2014-02-16 00:44:02 +08:00
|
|
|
opt = gotopt = 1;
|
2012-01-27 03:13:07 +08:00
|
|
|
next_token(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!root) {
|
|
|
|
/* skipping */
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = json_object_get(root, key);
|
|
|
|
if(!value && !opt) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_item_not_found, "Object item not found: %s", key);
|
2012-01-27 03:13:07 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2011-02-23 01:08:41 +08:00
|
|
|
}
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
if(unpack(s, value, ap))
|
2011-01-30 03:38:07 +08:00
|
|
|
goto out;
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
hashtable_set(&key_set, key, json_null());
|
2011-01-25 03:20:56 +08:00
|
|
|
next_token(s);
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
2011-01-30 03:38:07 +08:00
|
|
|
if(strict == 0 && (s->flags & JSON_STRICT))
|
|
|
|
strict = 1;
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2014-02-16 00:23:53 +08:00
|
|
|
if(root && strict == 1) {
|
|
|
|
/* We need to check that all non optional items have been parsed */
|
|
|
|
const char *key;
|
2018-03-06 21:27:20 +08:00
|
|
|
/* keys_res is 1 for uninitialized, 0 for success, -1 for error. */
|
|
|
|
int keys_res = 1;
|
2015-12-18 08:56:22 +08:00
|
|
|
strbuffer_t unrecognized_keys;
|
2014-02-16 00:23:53 +08:00
|
|
|
json_t *value;
|
|
|
|
long unpacked = 0;
|
2018-03-06 21:27:20 +08:00
|
|
|
|
|
|
|
if (gotopt || json_object_size(root) != key_set.size) {
|
2014-02-16 00:44:02 +08:00
|
|
|
json_object_foreach(root, key, value) {
|
|
|
|
if(!hashtable_get(&key_set, key)) {
|
|
|
|
unpacked++;
|
2015-12-18 08:56:22 +08:00
|
|
|
|
2015-12-23 13:40:02 +08:00
|
|
|
/* Save unrecognized keys for the error message */
|
2018-03-06 21:27:20 +08:00
|
|
|
if (keys_res == 1) {
|
|
|
|
keys_res = strbuffer_init(&unrecognized_keys);
|
|
|
|
} else if (!keys_res) {
|
|
|
|
keys_res = strbuffer_append_bytes(&unrecognized_keys, ", ", 2);
|
2015-12-18 08:56:22 +08:00
|
|
|
}
|
2018-03-06 21:27:20 +08:00
|
|
|
|
|
|
|
if (!keys_res)
|
|
|
|
keys_res = strbuffer_append_bytes(&unrecognized_keys, key, strlen(key));
|
2014-02-16 00:44:02 +08:00
|
|
|
}
|
2014-02-16 00:23:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (unpacked) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_end_of_input_expected,
|
2015-12-18 08:56:22 +08:00
|
|
|
"%li object item(s) left unpacked: %s",
|
2018-03-06 21:27:20 +08:00
|
|
|
unpacked,
|
|
|
|
keys_res ? "<unknown>" : strbuffer_value(&unrecognized_keys));
|
2015-12-18 08:56:22 +08:00
|
|
|
strbuffer_close(&unrecognized_keys);
|
2014-02-16 00:23:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2011-01-25 03:45:54 +08:00
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2011-01-25 03:45:54 +08:00
|
|
|
ret = 0;
|
|
|
|
|
2011-01-30 03:38:07 +08:00
|
|
|
out:
|
|
|
|
hashtable_close(&key_set);
|
2011-01-25 03:45:54 +08:00
|
|
|
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-30 03:38:07 +08:00
|
|
|
int strict = 0;
|
2011-01-25 03:20:56 +08:00
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_array(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected array, got %s", type_name(root));
|
2011-01-25 03:20:56 +08:00
|
|
|
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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
while(token(s) != ']') {
|
2011-01-25 03:20:56 +08:00
|
|
|
json_t *value;
|
|
|
|
|
2011-01-30 03:38:07 +08:00
|
|
|
if(strict != 0) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Expected ']' after '%c', got '%c'",
|
2011-01-30 03:38:07 +08:00
|
|
|
(strict == 1 ? '!' : '*'),
|
2013-08-15 02:54:11 +08:00
|
|
|
token(s));
|
2011-01-25 03:45:54 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(!token(s)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "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
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(s) == '!' || token(s) == '*') {
|
|
|
|
strict = (token(s) == '!' ? 1 : -1);
|
2011-01-25 03:45:54 +08:00
|
|
|
next_token(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-15 02:54:11 +08:00
|
|
|
if(!strchr(unpack_value_starters, token(s))) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
|
2013-08-15 02:54:11 +08:00
|
|
|
token(s));
|
2011-02-23 01:08:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!root) {
|
|
|
|
/* skipping */
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = json_array_get(root, i);
|
|
|
|
if(!value) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_index_out_of_range, "Array index %lu out of range",
|
2012-01-27 03:13:07 +08:00
|
|
|
(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-30 03:38:07 +08:00
|
|
|
if(strict == 0 && (s->flags & JSON_STRICT))
|
|
|
|
strict = 1;
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && strict == 1 && i != json_array_size(root)) {
|
2011-01-25 03:20:56 +08:00
|
|
|
long diff = (long)json_array_size(root) - (long)i;
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_end_of_input_expected, "%li array item(s) left unpacked", 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)
|
|
|
|
{
|
2013-08-15 02:54:11 +08:00
|
|
|
switch(token(s))
|
2011-01-25 03:20:56 +08:00
|
|
|
{
|
|
|
|
case '{':
|
|
|
|
return unpack_object(s, root, ap);
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
return unpack_array(s, root, ap);
|
|
|
|
|
|
|
|
case 's':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_string(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected string, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:36:43 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
2012-04-04 09:00:29 +08:00
|
|
|
const char **str_target;
|
|
|
|
size_t *len_target = NULL;
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
str_target = va_arg(*ap, const char **);
|
|
|
|
if(!str_target) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL string argument");
|
2011-01-25 15:36:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-25 03:20:56 +08:00
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
next_token(s);
|
|
|
|
|
|
|
|
if(token(s) == '%') {
|
|
|
|
len_target = va_arg(*ap, size_t *);
|
|
|
|
if(!len_target) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<args>", json_error_null_value, "NULL string length argument");
|
2012-04-04 09:00:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev_token(s);
|
|
|
|
|
|
|
|
if(root) {
|
|
|
|
*str_target = json_string_value(root);
|
|
|
|
if(len_target)
|
|
|
|
*len_target = json_string_length(root);
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
}
|
2011-01-25 03:20:56 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'i':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_integer(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
|
|
|
int *target = va_arg(*ap, int*);
|
|
|
|
if(root)
|
2012-04-30 03:09:29 +08:00
|
|
|
*target = (int)json_integer_value(root);
|
2012-01-27 03:13:07 +08:00
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
return 0;
|
|
|
|
|
2011-01-26 03:41:35 +08:00
|
|
|
case 'I':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_integer(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-26 03:41:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
|
|
|
json_int_t *target = va_arg(*ap, json_int_t*);
|
|
|
|
if(root)
|
|
|
|
*target = json_integer_value(root);
|
|
|
|
}
|
2011-01-26 03:41:35 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
case 'b':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_boolean(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected true or false, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
|
|
|
int *target = va_arg(*ap, int*);
|
|
|
|
if(root)
|
|
|
|
*target = json_is_true(root);
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'f':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_real(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected real, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
|
|
|
double *target = va_arg(*ap, double*);
|
|
|
|
if(root)
|
|
|
|
*target = json_real_value(root);
|
|
|
|
}
|
2011-01-26 02:42:41 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'F':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_number(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected real or integer, got %s",
|
2011-01-26 02:42:41 +08:00
|
|
|
type_name(root));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
|
|
|
double *target = va_arg(*ap, double*);
|
|
|
|
if(root)
|
|
|
|
*target = json_number_value(root);
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'O':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !(s->flags & JSON_VALIDATE_ONLY))
|
2011-01-25 15:36:43 +08:00
|
|
|
json_incref(root);
|
2011-01-25 03:20:56 +08:00
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case 'o':
|
2012-01-27 03:13:07 +08:00
|
|
|
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
|
|
|
json_t **target = va_arg(*ap, json_t**);
|
|
|
|
if(root)
|
|
|
|
*target = root;
|
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
2011-01-25 03:20:56 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'n':
|
2011-01-25 15:36:43 +08:00
|
|
|
/* Never assign, just validate */
|
2012-01-27 03:13:07 +08:00
|
|
|
if(root && !json_is_null(root)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<validation>", json_error_wrong_type, "Expected null, got %s",
|
2011-02-23 01:08:41 +08:00
|
|
|
type_name(root));
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
|
2013-08-15 02:54:11 +08:00
|
|
|
token(s));
|
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 15:36:43 +08:00
|
|
|
json_t *json_vpack_ex(json_error_t *error, size_t flags,
|
|
|
|
const char *fmt, va_list ap)
|
2011-01-15 01:18:42 +08:00
|
|
|
{
|
2011-01-24 03:14:19 +08:00
|
|
|
scanner_t s;
|
2011-02-03 23:51:26 +08:00
|
|
|
va_list ap_copy;
|
2011-01-24 03:14:19 +08:00
|
|
|
json_t *value;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-15 01:18:42 +08:00
|
|
|
if(!fmt || !*fmt) {
|
2011-02-23 01:08:41 +08:00
|
|
|
jsonp_error_init(error, "<format>");
|
2017-10-03 17:42:07 +08:00
|
|
|
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
|
2011-01-21 03:09:14 +08:00
|
|
|
return NULL;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2011-02-23 01:08:41 +08:00
|
|
|
jsonp_error_init(error, NULL);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-02-23 01:08:41 +08:00
|
|
|
scanner_init(&s, error, flags, fmt);
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(&s);
|
2011-02-23 01:08:41 +08:00
|
|
|
|
2011-02-03 23:51:26 +08:00
|
|
|
va_copy(ap_copy, ap);
|
|
|
|
value = pack(&s, &ap_copy);
|
|
|
|
va_end(ap_copy);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2018-11-06 05:43:10 +08:00
|
|
|
/* This will cover all situations where s.has_error is true */
|
2011-02-23 01:08:41 +08:00
|
|
|
if(!value)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-24 03:14:19 +08:00
|
|
|
next_token(&s);
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(&s)) {
|
2011-01-25 15:36:43 +08:00
|
|
|
json_decref(value);
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
|
2011-01-24 03:14:19 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-01-25 15:36:43 +08:00
|
|
|
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
|
2011-01-15 01:18:42 +08:00
|
|
|
{
|
2011-01-25 15:36:43 +08:00
|
|
|
json_t *value;
|
2011-01-15 01:18:42 +08:00
|
|
|
va_list ap;
|
2011-01-25 15:36:43 +08:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
value = json_vpack_ex(error, flags, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t *json_pack(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
json_t *value;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
value = json_vpack_ex(NULL, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
scanner_t s;
|
2011-02-03 23:51:26 +08:00
|
|
|
va_list ap_copy;
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-02-23 01:08:41 +08:00
|
|
|
if(!root) {
|
|
|
|
jsonp_error_init(error, "<root>");
|
2017-10-03 17:42:07 +08:00
|
|
|
jsonp_error_set(error, -1, -1, 0, json_error_null_value, "NULL root value");
|
2011-02-23 01:08:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-15 01:18:42 +08:00
|
|
|
|
|
|
|
if(!fmt || !*fmt) {
|
2011-02-23 01:08:41 +08:00
|
|
|
jsonp_error_init(error, "<format>");
|
2017-10-03 17:42:07 +08:00
|
|
|
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
2011-01-15 01:18:42 +08:00
|
|
|
}
|
2011-02-23 01:08:41 +08:00
|
|
|
jsonp_error_init(error, NULL);
|
2010-10-26 07:36:29 +08:00
|
|
|
|
2011-02-23 01:08:41 +08:00
|
|
|
scanner_init(&s, error, flags, fmt);
|
2011-01-25 03:20:56 +08:00
|
|
|
next_token(&s);
|
|
|
|
|
2011-02-03 23:51:26 +08:00
|
|
|
va_copy(ap_copy, ap);
|
|
|
|
if(unpack(&s, root, &ap_copy)) {
|
|
|
|
va_end(ap_copy);
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
2011-02-03 23:51:26 +08:00
|
|
|
}
|
|
|
|
va_end(ap_copy);
|
2011-01-25 03:20:56 +08:00
|
|
|
|
|
|
|
next_token(&s);
|
2013-08-15 02:54:11 +08:00
|
|
|
if(token(&s)) {
|
2017-10-03 17:42:07 +08:00
|
|
|
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
|
2011-01-25 03:20:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-10-26 07:36:29 +08:00
|
|
|
}
|
2011-01-25 15:36:43 +08:00
|
|
|
|
|
|
|
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = json_vunpack_ex(root, error, flags, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int json_unpack(json_t *root, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = json_vunpack_ex(root, NULL, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|