2009-07-28 16:01:52 +08:00
|
|
|
/*
|
2011-01-21 03:28:54 +08:00
|
|
|
* Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
|
2009-07-28 16:01:52 +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.
|
|
|
|
*/
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
#define _GNU_SOURCE
|
2009-06-14 03:28:26 +08:00
|
|
|
#include <errno.h>
|
2009-09-13 18:15:34 +08:00
|
|
|
#include <limits.h>
|
2009-02-07 02:26:27 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <jansson.h>
|
2009-07-14 02:03:09 +08:00
|
|
|
#include "jansson_private.h"
|
2009-06-14 03:28:26 +08:00
|
|
|
#include "strbuffer.h"
|
2009-07-14 02:03:09 +08:00
|
|
|
#include "utf.h"
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
#define STREAM_STATE_OK 0
|
|
|
|
#define STREAM_STATE_EOF -1
|
|
|
|
#define STREAM_STATE_ERROR -2
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
#define TOKEN_INVALID -1
|
|
|
|
#define TOKEN_EOF 0
|
|
|
|
#define TOKEN_STRING 256
|
|
|
|
#define TOKEN_INTEGER 257
|
|
|
|
#define TOKEN_REAL 258
|
|
|
|
#define TOKEN_TRUE 259
|
|
|
|
#define TOKEN_FALSE 260
|
|
|
|
#define TOKEN_NULL 261
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-10-04 02:35:51 +08:00
|
|
|
/* Locale independent versions of isxxx() functions */
|
|
|
|
#define l_isupper(c) ('A' <= c && c <= 'Z')
|
|
|
|
#define l_islower(c) ('a' <= c && c <= 'z')
|
|
|
|
#define l_isalpha(c) (l_isupper(c) || l_islower(c))
|
|
|
|
#define l_isdigit(c) ('0' <= c && c <= '9')
|
|
|
|
#define l_isxdigit(c) \
|
|
|
|
(l_isdigit(c) || 'A' <= c || c <= 'F' || 'a' <= c || c <= 'f')
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
/* Read one byte from stream, convert to unsigned char, then int, and
|
|
|
|
return. return EOF on end of file. This corresponds to the
|
|
|
|
behaviour of fgetc(). */
|
2009-07-13 16:01:20 +08:00
|
|
|
typedef int (*get_func)(void *data);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
get_func get;
|
|
|
|
void *data;
|
|
|
|
char buffer[5];
|
|
|
|
int buffer_pos;
|
2011-02-22 16:47:02 +08:00
|
|
|
int state;
|
|
|
|
int line;
|
|
|
|
int column, last_column;
|
|
|
|
size_t position;
|
2009-07-13 16:01:20 +08:00
|
|
|
} stream_t;
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
typedef struct {
|
2009-07-13 16:01:20 +08:00
|
|
|
stream_t stream;
|
|
|
|
strbuffer_t saved_text;
|
2009-02-07 02:26:27 +08:00
|
|
|
int token;
|
|
|
|
union {
|
|
|
|
char *string;
|
2010-08-14 03:06:01 +08:00
|
|
|
json_int_t integer;
|
2009-06-23 05:14:28 +08:00
|
|
|
double real;
|
2009-02-07 02:26:27 +08:00
|
|
|
} value;
|
2009-07-11 15:52:57 +08:00
|
|
|
} lex_t;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
#define stream_to_lex(stream) container_of(stream, lex_t, stream)
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
/*** error reporting ***/
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
static void error_set(json_error_t *error, const lex_t *lex,
|
2009-07-11 15:52:57 +08:00
|
|
|
const char *msg, ...)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2009-06-14 03:28:26 +08:00
|
|
|
va_list ap;
|
2010-10-27 04:36:24 +08:00
|
|
|
char msg_text[JSON_ERROR_TEXT_LENGTH];
|
2011-09-20 19:10:32 +08:00
|
|
|
char msg_with_context[JSON_ERROR_TEXT_LENGTH];
|
2009-06-14 03:28:26 +08:00
|
|
|
|
2010-10-27 04:36:24 +08:00
|
|
|
int line = -1, col = -1;
|
2011-02-22 16:47:02 +08:00
|
|
|
size_t pos = 0;
|
2010-10-27 04:36:24 +08:00
|
|
|
const char *result = msg_text;
|
|
|
|
|
|
|
|
if(!error)
|
2009-05-14 03:25:34 +08:00
|
|
|
return;
|
|
|
|
|
2009-06-14 03:28:26 +08:00
|
|
|
va_start(ap, msg);
|
2010-10-27 04:36:24 +08:00
|
|
|
vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
|
2009-06-14 03:28:26 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if(lex)
|
2009-05-14 03:25:34 +08:00
|
|
|
{
|
2009-07-13 16:01:20 +08:00
|
|
|
const char *saved_text = strbuffer_value(&lex->saved_text);
|
2010-10-27 04:36:24 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
line = lex->stream.line;
|
|
|
|
col = lex->stream.column;
|
|
|
|
pos = lex->stream.position;
|
2010-10-27 04:36:24 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(saved_text && saved_text[0])
|
2009-06-14 03:28:26 +08:00
|
|
|
{
|
2009-07-15 01:47:57 +08:00
|
|
|
if(lex->saved_text.length <= 20) {
|
2010-10-27 04:36:24 +08:00
|
|
|
snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
|
|
|
|
"%s near '%s'", msg_text, saved_text);
|
|
|
|
result = msg_with_context;
|
2009-07-15 01:47:57 +08:00
|
|
|
}
|
2009-06-14 03:28:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
if(lex->stream.state == STREAM_STATE_ERROR) {
|
|
|
|
/* No context for UTF-8 decoding errors */
|
|
|
|
result = msg_text;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
|
|
|
|
"%s near end of file", msg_text);
|
|
|
|
result = msg_with_context;
|
|
|
|
}
|
2009-06-14 03:28:26 +08:00
|
|
|
}
|
2009-05-14 03:25:34 +08:00
|
|
|
}
|
2010-10-27 04:36:24 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
jsonp_error_set(error, line, col, pos, "%s", result);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** lexical analyzer ***/
|
|
|
|
|
2010-05-07 12:35:11 +08:00
|
|
|
static void
|
2011-02-22 16:47:02 +08:00
|
|
|
stream_init(stream_t *stream, get_func get, void *data)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
|
|
|
stream->get = get;
|
|
|
|
stream->data = data;
|
|
|
|
stream->buffer[0] = '\0';
|
|
|
|
stream->buffer_pos = 0;
|
2011-02-22 16:47:02 +08:00
|
|
|
|
|
|
|
stream->state = STREAM_STATE_OK;
|
|
|
|
stream->line = 1;
|
|
|
|
stream->column = 0;
|
|
|
|
stream->position = 0;
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
static int stream_get(stream_t *stream, json_error_t *error)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if(stream->state != STREAM_STATE_OK)
|
|
|
|
return stream->state;
|
2009-07-14 04:34:31 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(!stream->buffer[stream->buffer_pos])
|
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
c = stream->get(stream->data);
|
|
|
|
if(c == EOF) {
|
|
|
|
stream->state = STREAM_STATE_EOF;
|
|
|
|
return STREAM_STATE_EOF;
|
|
|
|
}
|
2009-07-14 02:03:09 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
stream->buffer[0] = c;
|
|
|
|
stream->buffer_pos = 0;
|
2009-07-14 02:03:09 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
if(0x80 <= c && c <= 0xFF)
|
2009-07-14 02:03:09 +08:00
|
|
|
{
|
|
|
|
/* multi-byte UTF-8 sequence */
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
count = utf8_check_first(c);
|
|
|
|
if(!count)
|
2009-07-14 04:34:31 +08:00
|
|
|
goto out;
|
2009-07-14 02:03:09 +08:00
|
|
|
|
|
|
|
assert(count >= 2);
|
|
|
|
|
|
|
|
for(i = 1; i < count; i++)
|
|
|
|
stream->buffer[i] = stream->get(stream->data);
|
|
|
|
|
2009-12-06 04:55:30 +08:00
|
|
|
if(!utf8_check_full(stream->buffer, count, NULL))
|
2009-07-14 04:34:31 +08:00
|
|
|
goto out;
|
2009-07-14 02:03:09 +08:00
|
|
|
|
|
|
|
stream->buffer[count] = '\0';
|
|
|
|
}
|
2011-02-22 16:47:02 +08:00
|
|
|
else
|
2009-07-14 02:03:09 +08:00
|
|
|
stream->buffer[1] = '\0';
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
c = stream->buffer[stream->buffer_pos++];
|
2009-07-14 04:34:31 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
stream->position++;
|
|
|
|
if(c == '\n') {
|
|
|
|
stream->line++;
|
|
|
|
stream->last_column = stream->column;
|
|
|
|
stream->column = 0;
|
|
|
|
}
|
|
|
|
else if(utf8_check_first(c)) {
|
|
|
|
/* track the Unicode character column, so increment only if
|
|
|
|
this is the first character of a UTF-8 sequence */
|
|
|
|
stream->column++;
|
|
|
|
}
|
2009-07-28 15:37:52 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
return c;
|
2009-07-28 15:37:52 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
out:
|
|
|
|
stream->state = STREAM_STATE_ERROR;
|
|
|
|
error_set(error, stream_to_lex(stream), "unable to decode byte 0x%x", c);
|
|
|
|
return STREAM_STATE_ERROR;
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
static void stream_unget(stream_t *stream, int c)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
if(c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stream->position--;
|
|
|
|
if(c == '\n') {
|
|
|
|
stream->line--;
|
|
|
|
stream->column = stream->last_column;
|
|
|
|
}
|
|
|
|
else if(utf8_check_first(c))
|
|
|
|
stream->column--;
|
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
assert(stream->buffer_pos > 0);
|
|
|
|
stream->buffer_pos--;
|
2009-07-28 15:37:52 +08:00
|
|
|
assert(stream->buffer[stream->buffer_pos] == c);
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
static int lex_get(lex_t *lex, json_error_t *error)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
2009-07-14 04:34:31 +08:00
|
|
|
return stream_get(&lex->stream, error);
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
static void lex_save(lex_t *lex, int c)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
|
|
|
strbuffer_append_byte(&lex->saved_text, c);
|
|
|
|
}
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
static int lex_get_save(lex_t *lex, json_error_t *error)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
int c = stream_get(&lex->stream, error);
|
|
|
|
if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR)
|
|
|
|
lex_save(lex, c);
|
2009-07-13 16:01:20 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
static void lex_unget(lex_t *lex, int c)
|
2009-07-13 16:01:20 +08:00
|
|
|
{
|
2009-07-28 15:37:52 +08:00
|
|
|
stream_unget(&lex->stream, c);
|
2011-02-22 16:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lex_unget_unsave(lex_t *lex, int c)
|
|
|
|
{
|
|
|
|
if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) {
|
|
|
|
char d;
|
|
|
|
stream_unget(&lex->stream, c);
|
|
|
|
d = strbuffer_pop(&lex->saved_text);
|
|
|
|
assert(c == d);
|
|
|
|
}
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 01:45:28 +08:00
|
|
|
static void lex_save_cached(lex_t *lex)
|
|
|
|
{
|
|
|
|
while(lex->stream.buffer[lex->stream.buffer_pos] != '\0')
|
|
|
|
{
|
|
|
|
lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]);
|
|
|
|
lex->stream.buffer_pos++;
|
2011-02-22 16:47:02 +08:00
|
|
|
lex->stream.position++;
|
2009-07-15 01:45:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 01:47:57 +08:00
|
|
|
/* assumes that str points to 'u' plus at least 4 valid hex digits */
|
2009-12-03 05:48:50 +08:00
|
|
|
static int32_t decode_unicode_escape(const char *str)
|
2009-07-15 01:47:57 +08:00
|
|
|
{
|
|
|
|
int i;
|
2009-12-03 05:48:50 +08:00
|
|
|
int32_t value = 0;
|
2009-07-15 01:47:57 +08:00
|
|
|
|
|
|
|
assert(str[0] == 'u');
|
|
|
|
|
|
|
|
for(i = 1; i <= 4; i++) {
|
|
|
|
char c = str[i];
|
|
|
|
value <<= 4;
|
2011-10-04 02:35:51 +08:00
|
|
|
if(l_isdigit(c))
|
2009-07-15 01:47:57 +08:00
|
|
|
value += c - '0';
|
2011-10-04 02:35:51 +08:00
|
|
|
else if(l_islower(c))
|
2009-07-15 01:47:57 +08:00
|
|
|
value += c - 'a' + 10;
|
2011-10-04 02:35:51 +08:00
|
|
|
else if(l_isupper(c))
|
2009-07-15 01:47:57 +08:00
|
|
|
value += c - 'A' + 10;
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
static void lex_scan_string(lex_t *lex, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
int c;
|
2009-07-13 16:01:20 +08:00
|
|
|
const char *p;
|
2009-02-07 02:26:27 +08:00
|
|
|
char *t;
|
2009-07-15 01:47:57 +08:00
|
|
|
int i;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-28 15:37:52 +08:00
|
|
|
lex->value.string = NULL;
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_INVALID;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-07-13 16:01:20 +08:00
|
|
|
|
|
|
|
while(c != '"') {
|
2011-02-22 16:47:02 +08:00
|
|
|
if(c == STREAM_STATE_ERROR)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
else if(c == STREAM_STATE_EOF) {
|
|
|
|
error_set(error, lex, "premature end of input");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto out;
|
2009-07-14 04:34:31 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
else if(0 <= c && c <= 0x1F) {
|
2009-02-07 02:26:27 +08:00
|
|
|
/* control character */
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
2009-07-14 04:34:31 +08:00
|
|
|
if(c == '\n')
|
|
|
|
error_set(error, lex, "unexpected newline", c);
|
|
|
|
else
|
|
|
|
error_set(error, lex, "control character 0x%x", c);
|
2009-02-07 02:26:27 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2009-07-13 16:01:20 +08:00
|
|
|
|
|
|
|
else if(c == '\\') {
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == 'u') {
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-07-15 01:47:57 +08:00
|
|
|
for(i = 0; i < 4; i++) {
|
2011-10-04 02:35:51 +08:00
|
|
|
if(!l_isxdigit(c)) {
|
2009-07-14 04:34:31 +08:00
|
|
|
error_set(error, lex, "invalid escape");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto out;
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
}
|
2009-07-13 16:01:20 +08:00
|
|
|
else if(c == '"' || c == '\\' || c == '/' || c == 'b' ||
|
|
|
|
c == 'f' || c == 'n' || c == 'r' || c == 't')
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-07-13 16:01:20 +08:00
|
|
|
else {
|
2009-07-14 04:34:31 +08:00
|
|
|
error_set(error, lex, "invalid escape");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto out;
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
else
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the actual value is at most of the same length as the source
|
2009-07-13 16:01:20 +08:00
|
|
|
string, because:
|
|
|
|
- shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte
|
|
|
|
- a single \uXXXX escape (length 6) is converted to at most 3 bytes
|
|
|
|
- two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
|
|
|
|
are converted to 4 bytes
|
|
|
|
*/
|
2011-02-17 16:10:53 +08:00
|
|
|
lex->value.string = jsonp_malloc(lex->saved_text.length + 1);
|
2009-02-07 02:26:27 +08:00
|
|
|
if(!lex->value.string) {
|
|
|
|
/* this is not very nice, since TOKEN_INVALID is returned */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the target */
|
|
|
|
t = lex->value.string;
|
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
/* + 1 to skip the " */
|
|
|
|
p = strbuffer_value(&lex->saved_text) + 1;
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
while(*p != '"') {
|
|
|
|
if(*p == '\\') {
|
|
|
|
p++;
|
|
|
|
if(*p == 'u') {
|
2009-07-15 01:47:57 +08:00
|
|
|
char buffer[4];
|
|
|
|
int length;
|
2009-12-03 05:48:50 +08:00
|
|
|
int32_t value;
|
2009-07-15 01:47:57 +08:00
|
|
|
|
|
|
|
value = decode_unicode_escape(p);
|
|
|
|
p += 5;
|
|
|
|
|
|
|
|
if(0xD800 <= value && value <= 0xDBFF) {
|
|
|
|
/* surrogate pair */
|
|
|
|
if(*p == '\\' && *(p + 1) == 'u') {
|
2009-12-03 05:48:50 +08:00
|
|
|
int32_t value2 = decode_unicode_escape(++p);
|
2009-07-15 01:47:57 +08:00
|
|
|
p += 5;
|
|
|
|
|
|
|
|
if(0xDC00 <= value2 && value2 <= 0xDFFF) {
|
|
|
|
/* valid second surrogate */
|
2009-10-17 02:20:38 +08:00
|
|
|
value =
|
|
|
|
((value - 0xD800) << 10) +
|
|
|
|
(value2 - 0xDC00) +
|
|
|
|
0x10000;
|
2009-07-15 01:47:57 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* invalid second surrogate */
|
|
|
|
error_set(error, lex,
|
|
|
|
"invalid Unicode '\\u%04X\\u%04X'",
|
|
|
|
value, value2);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no second surrogate */
|
|
|
|
error_set(error, lex, "invalid Unicode '\\u%04X'",
|
|
|
|
value);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(0xDC00 <= value && value <= 0xDFFF) {
|
|
|
|
error_set(error, lex, "invalid Unicode '\\u%04X'", value);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if(value == 0)
|
|
|
|
{
|
|
|
|
error_set(error, lex, "\\u0000 is not allowed");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(utf8_encode(value, buffer, &length))
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
memcpy(t, buffer, length);
|
|
|
|
t += length;
|
|
|
|
}
|
|
|
|
else {
|
2009-02-07 02:26:27 +08:00
|
|
|
switch(*p) {
|
|
|
|
case '"': case '\\': case '/':
|
|
|
|
*t = *p; break;
|
|
|
|
case 'b': *t = '\b'; break;
|
|
|
|
case 'f': *t = '\f'; break;
|
|
|
|
case 'n': *t = '\n'; break;
|
|
|
|
case 'r': *t = '\r'; break;
|
|
|
|
case 't': *t = '\t'; break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
2009-07-15 01:47:57 +08:00
|
|
|
t++;
|
|
|
|
p++;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-07-15 01:47:57 +08:00
|
|
|
*(t++) = *(p++);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
*t = '\0';
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_STRING;
|
2009-07-28 15:37:52 +08:00
|
|
|
return;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
|
|
|
out:
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(lex->value.string);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2010-08-15 01:37:50 +08:00
|
|
|
#if JSON_INTEGER_IS_LONG_LONG
|
2010-08-14 03:06:01 +08:00
|
|
|
#define json_strtoint strtoll
|
|
|
|
#else
|
|
|
|
#define json_strtoint strtol
|
|
|
|
#endif
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2009-07-13 16:01:20 +08:00
|
|
|
const char *saved_text;
|
2009-02-07 02:26:27 +08:00
|
|
|
char *end;
|
2009-09-13 18:15:34 +08:00
|
|
|
double value;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_INVALID;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == '-')
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == '0') {
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2011-10-04 02:35:51 +08:00
|
|
|
if(l_isdigit(c)) {
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-07-04 18:18:49 +08:00
|
|
|
}
|
2011-10-04 02:35:51 +08:00
|
|
|
else if(l_isdigit(c)) {
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2011-10-04 02:35:51 +08:00
|
|
|
while(l_isdigit(c))
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
2009-10-29 21:42:05 +08:00
|
|
|
else {
|
2011-02-22 16:47:02 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
|
|
|
goto out;
|
2009-10-29 21:42:05 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c != '.' && c != 'E' && c != 'e') {
|
2010-08-14 03:06:01 +08:00
|
|
|
json_int_t value;
|
2009-09-13 18:15:34 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
2009-06-23 05:14:28 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
saved_text = strbuffer_value(&lex->saved_text);
|
2009-06-23 05:14:28 +08:00
|
|
|
|
2010-08-14 03:06:01 +08:00
|
|
|
errno = 0;
|
|
|
|
value = json_strtoint(saved_text, &end, 10);
|
|
|
|
if(errno == ERANGE) {
|
|
|
|
if(value < 0)
|
|
|
|
error_set(error, lex, "too big negative integer");
|
|
|
|
else
|
|
|
|
error_set(error, lex, "too big integer");
|
2009-09-13 18:15:34 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-08-14 03:06:01 +08:00
|
|
|
assert(end == saved_text + lex->saved_text.length);
|
|
|
|
|
2009-09-13 18:15:34 +08:00
|
|
|
lex->token = TOKEN_INTEGER;
|
2010-08-11 02:45:18 +08:00
|
|
|
lex->value.integer = value;
|
2009-09-13 18:15:34 +08:00
|
|
|
return 0;
|
2009-06-23 05:14:28 +08:00
|
|
|
}
|
2009-07-04 18:18:49 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == '.') {
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get(lex, error);
|
2011-10-04 02:35:51 +08:00
|
|
|
if(!l_isdigit(c)) {
|
2011-02-22 16:47:02 +08:00
|
|
|
lex_unget(lex, c);
|
2009-02-07 02:26:27 +08:00
|
|
|
goto out;
|
2011-02-22 16:47:02 +08:00
|
|
|
}
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_save(lex, c);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2011-10-04 02:35:51 +08:00
|
|
|
while(l_isdigit(c))
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == 'E' || c == 'e') {
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == '+' || c == '-')
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-10-04 02:35:51 +08:00
|
|
|
if(!l_isdigit(c)) {
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
2009-02-07 02:26:27 +08:00
|
|
|
goto out;
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2011-10-04 02:35:51 +08:00
|
|
|
while(l_isdigit(c))
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-10-03 02:27:53 +08:00
|
|
|
if(jsonp_strtod(&lex->saved_text, &value)) {
|
2009-09-13 18:15:34 +08:00
|
|
|
error_set(error, lex, "real number overflow");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
lex->token = TOKEN_REAL;
|
|
|
|
lex->value.real = value;
|
|
|
|
return 0;
|
|
|
|
|
2009-02-07 02:26:27 +08:00
|
|
|
out:
|
2009-09-13 18:15:34 +08:00
|
|
|
return -1;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
static int lex_scan(lex_t *lex, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
int c;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
strbuffer_clear(&lex->saved_text);
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
if(lex->token == TOKEN_STRING) {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(lex->value.string);
|
2009-10-17 02:20:38 +08:00
|
|
|
lex->value.string = NULL;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get(lex, error);
|
2009-07-10 02:48:04 +08:00
|
|
|
while(c == ' ' || c == '\t' || c == '\n' || c == '\r')
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get(lex, error);
|
2011-02-22 16:47:02 +08:00
|
|
|
|
|
|
|
if(c == STREAM_STATE_EOF) {
|
|
|
|
lex->token = TOKEN_EOF;
|
|
|
|
goto out;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
if(c == STREAM_STATE_ERROR) {
|
|
|
|
lex->token = TOKEN_INVALID;
|
2009-07-13 16:01:20 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_save(lex, c);
|
|
|
|
|
|
|
|
if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
|
2009-02-07 02:26:27 +08:00
|
|
|
lex->token = c;
|
|
|
|
|
|
|
|
else if(c == '"')
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan_string(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-10-04 02:35:51 +08:00
|
|
|
else if(l_isdigit(c) || c == '-') {
|
2009-09-13 18:15:34 +08:00
|
|
|
if(lex_scan_number(lex, c, error))
|
|
|
|
goto out;
|
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-10-04 02:35:51 +08:00
|
|
|
else if(l_isalpha(c)) {
|
2009-02-07 02:26:27 +08:00
|
|
|
/* eat up the whole identifier for clearer error messages */
|
2009-07-13 16:01:20 +08:00
|
|
|
const char *saved_text;
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2011-10-04 02:35:51 +08:00
|
|
|
while(l_isalpha(c))
|
2009-07-14 04:34:31 +08:00
|
|
|
c = lex_get_save(lex, error);
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_unget_unsave(lex, c);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
saved_text = strbuffer_value(&lex->saved_text);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
if(strcmp(saved_text, "true") == 0)
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_TRUE;
|
2009-07-13 16:01:20 +08:00
|
|
|
else if(strcmp(saved_text, "false") == 0)
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_FALSE;
|
2009-07-13 16:01:20 +08:00
|
|
|
else if(strcmp(saved_text, "null") == 0)
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_NULL;
|
2009-02-07 02:26:27 +08:00
|
|
|
else
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_INVALID;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 01:45:28 +08:00
|
|
|
else {
|
|
|
|
/* save the rest of the input UTF-8 sequence to get an error
|
|
|
|
message of valid UTF-8 */
|
|
|
|
lex_save_cached(lex);
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_INVALID;
|
2009-07-15 01:45:28 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
out:
|
2009-02-07 02:26:27 +08:00
|
|
|
return lex->token;
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:30:36 +08:00
|
|
|
static char *lex_steal_string(lex_t *lex)
|
|
|
|
{
|
|
|
|
char *result = NULL;
|
|
|
|
if(lex->token == TOKEN_STRING)
|
|
|
|
{
|
|
|
|
result = lex->value.string;
|
|
|
|
lex->value.string = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
static int lex_init(lex_t *lex, get_func get, void *data)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2011-02-22 16:47:02 +08:00
|
|
|
stream_init(&lex->stream, get, data);
|
2009-07-13 16:01:20 +08:00
|
|
|
if(strbuffer_init(&lex->saved_text))
|
|
|
|
return -1;
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
lex->token = TOKEN_INVALID;
|
2009-02-07 02:26:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
static void lex_close(lex_t *lex)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2009-07-11 15:52:57 +08:00
|
|
|
if(lex->token == TOKEN_STRING)
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(lex->value.string);
|
2009-07-28 15:37:52 +08:00
|
|
|
strbuffer_close(&lex->saved_text);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** parser ***/
|
|
|
|
|
2011-05-15 18:57:48 +08:00
|
|
|
static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2011-05-15 18:57:48 +08:00
|
|
|
static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
json_t *object = json_object();
|
|
|
|
if(!object)
|
|
|
|
return NULL;
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-05-07 02:01:00 +08:00
|
|
|
if(lex->token == '}')
|
|
|
|
return object;
|
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
while(1) {
|
2009-02-07 02:26:27 +08:00
|
|
|
char *key;
|
|
|
|
json_t *value;
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
if(lex->token != TOKEN_STRING) {
|
2009-07-13 16:01:20 +08:00
|
|
|
error_set(error, lex, "string or '}' expected");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:30:36 +08:00
|
|
|
key = lex_steal_string(lex);
|
2009-02-07 02:26:27 +08:00
|
|
|
if(!key)
|
|
|
|
return NULL;
|
|
|
|
|
2011-05-15 18:57:48 +08:00
|
|
|
if(flags & JSON_REJECT_DUPLICATES) {
|
|
|
|
if(json_object_get(object, key)) {
|
|
|
|
jsonp_free(key);
|
|
|
|
error_set(error, lex, "duplicate object key");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
if(lex->token != ':') {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(key);
|
2009-07-11 15:52:57 +08:00
|
|
|
error_set(error, lex, "':' expected");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2011-05-15 18:57:48 +08:00
|
|
|
value = parse_value(lex, flags, error);
|
2009-06-14 03:54:57 +08:00
|
|
|
if(!value) {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(key);
|
2009-02-07 02:26:27 +08:00
|
|
|
goto error;
|
2009-06-14 03:54:57 +08:00
|
|
|
}
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-14 02:03:09 +08:00
|
|
|
if(json_object_set_nocheck(object, key, value)) {
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(key);
|
2009-02-07 02:26:27 +08:00
|
|
|
json_decref(value);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(value);
|
2011-02-17 16:10:53 +08:00
|
|
|
jsonp_free(key);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
if(lex->token != ',')
|
|
|
|
break;
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(lex->token != '}') {
|
2009-07-11 15:52:57 +08:00
|
|
|
error_set(error, lex, "'}' expected");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
|
|
|
|
error:
|
|
|
|
json_decref(object);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-15 18:57:48 +08:00
|
|
|
static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
json_t *array = json_array();
|
|
|
|
if(!array)
|
|
|
|
return NULL;
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-06-21 21:04:43 +08:00
|
|
|
if(lex->token == ']')
|
|
|
|
return array;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-06-21 21:04:43 +08:00
|
|
|
while(lex->token) {
|
2011-05-15 18:57:48 +08:00
|
|
|
json_t *elem = parse_value(lex, flags, error);
|
2009-06-21 21:04:43 +08:00
|
|
|
if(!elem)
|
|
|
|
goto error;
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-06-21 21:04:43 +08:00
|
|
|
if(json_array_append(array, elem)) {
|
|
|
|
json_decref(elem);
|
|
|
|
goto error;
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
2009-06-21 21:04:43 +08:00
|
|
|
json_decref(elem);
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-06-21 21:04:43 +08:00
|
|
|
if(lex->token != ',')
|
|
|
|
break;
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(lex->token != ']') {
|
2009-07-11 15:52:57 +08:00
|
|
|
error_set(error, lex, "']' expected");
|
2009-02-07 02:26:27 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
|
|
|
error:
|
|
|
|
json_decref(array);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-15 18:57:48 +08:00
|
|
|
static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
|
|
|
json_t *json;
|
|
|
|
|
|
|
|
switch(lex->token) {
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_STRING: {
|
2009-07-14 02:03:09 +08:00
|
|
|
json = json_string_nocheck(lex->value.string);
|
2009-02-07 02:26:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_INTEGER: {
|
2009-06-23 05:14:28 +08:00
|
|
|
json = json_integer(lex->value.integer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_REAL: {
|
2009-06-23 05:14:28 +08:00
|
|
|
json = json_real(lex->value.real);
|
2009-02-07 02:26:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_TRUE:
|
2009-02-07 02:26:27 +08:00
|
|
|
json = json_true();
|
|
|
|
break;
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_FALSE:
|
2009-02-07 02:26:27 +08:00
|
|
|
json = json_false();
|
|
|
|
break;
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_NULL:
|
2009-02-07 02:26:27 +08:00
|
|
|
json = json_null();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{':
|
2011-05-15 18:57:48 +08:00
|
|
|
json = parse_object(lex, flags, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
2011-05-15 18:57:48 +08:00
|
|
|
json = parse_array(lex, flags, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
break;
|
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
case TOKEN_INVALID:
|
|
|
|
error_set(error, lex, "invalid token");
|
2009-02-07 02:26:27 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
default:
|
2009-07-11 15:52:57 +08:00
|
|
|
error_set(error, lex, "unexpected token");
|
2009-02-07 02:26:27 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!json)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2011-05-15 18:57:48 +08:00
|
|
|
static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
|
2009-07-11 16:16:14 +08:00
|
|
|
{
|
2011-05-29 17:53:25 +08:00
|
|
|
json_t *result;
|
|
|
|
|
2009-07-14 04:34:31 +08:00
|
|
|
lex_scan(lex, error);
|
2009-07-11 16:16:14 +08:00
|
|
|
if(lex->token != '[' && lex->token != '{') {
|
|
|
|
error_set(error, lex, "'[' or '{' expected");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-29 17:53:25 +08:00
|
|
|
result = parse_value(lex, flags, error);
|
|
|
|
if(!result)
|
|
|
|
return NULL;
|
|
|
|
|
2011-05-30 02:19:28 +08:00
|
|
|
if(!(flags & JSON_DISABLE_EOF_CHECK)) {
|
|
|
|
lex_scan(lex, error);
|
|
|
|
if(lex->token != TOKEN_EOF) {
|
|
|
|
error_set(error, lex, "end of file expected");
|
|
|
|
json_decref(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
2011-05-29 17:53:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2009-07-11 16:16:14 +08:00
|
|
|
}
|
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *data;
|
|
|
|
int pos;
|
|
|
|
} string_data_t;
|
|
|
|
|
|
|
|
static int string_get(void *data)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
string_data_t *stream = (string_data_t *)data;
|
2009-07-28 15:37:52 +08:00
|
|
|
c = stream->data[stream->pos];
|
2009-07-13 16:01:20 +08:00
|
|
|
if(c == '\0')
|
|
|
|
return EOF;
|
|
|
|
else
|
2009-07-28 15:37:52 +08:00
|
|
|
{
|
|
|
|
stream->pos++;
|
2011-02-22 16:47:02 +08:00
|
|
|
return (unsigned char)c;
|
2009-07-28 15:37:52 +08:00
|
|
|
}
|
2009-07-13 16:01:20 +08:00
|
|
|
}
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
json_t *json_loads(const char *string, size_t flags, json_error_t *error)
|
2009-02-07 02:26:27 +08:00
|
|
|
{
|
2009-07-11 15:52:57 +08:00
|
|
|
lex_t lex;
|
2009-07-13 16:01:20 +08:00
|
|
|
json_t *result;
|
2011-03-21 15:27:51 +08:00
|
|
|
string_data_t stream_data;
|
2009-07-13 16:01:20 +08:00
|
|
|
|
2011-03-21 15:27:51 +08:00
|
|
|
stream_data.data = string;
|
|
|
|
stream_data.pos = 0;
|
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
if(lex_init(&lex, string_get, (void *)&stream_data))
|
2009-02-07 02:26:27 +08:00
|
|
|
return NULL;
|
|
|
|
|
2010-10-27 04:36:24 +08:00
|
|
|
jsonp_error_init(error, "<string>");
|
2011-05-15 18:57:48 +08:00
|
|
|
result = parse_json(&lex, flags, error);
|
2009-02-07 02:26:27 +08:00
|
|
|
|
2009-07-11 15:52:57 +08:00
|
|
|
lex_close(&lex);
|
2009-02-07 02:26:27 +08:00
|
|
|
return result;
|
|
|
|
}
|
2009-06-14 03:28:26 +08:00
|
|
|
|
2011-04-11 01:54:52 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *data;
|
|
|
|
size_t len;
|
|
|
|
size_t pos;
|
|
|
|
} buffer_data_t;
|
|
|
|
|
|
|
|
static int buffer_get(void *data)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
buffer_data_t *stream = data;
|
|
|
|
if(stream->pos >= stream->len)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
c = stream->data[stream->pos];
|
|
|
|
stream->pos++;
|
|
|
|
return (unsigned char)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error)
|
|
|
|
{
|
|
|
|
lex_t lex;
|
|
|
|
json_t *result;
|
|
|
|
buffer_data_t stream_data;
|
|
|
|
|
|
|
|
stream_data.data = buffer;
|
|
|
|
stream_data.pos = 0;
|
|
|
|
stream_data.len = buflen;
|
|
|
|
|
|
|
|
if(lex_init(&lex, buffer_get, (void *)&stream_data))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
jsonp_error_init(error, "<buffer>");
|
2011-05-15 18:57:48 +08:00
|
|
|
result = parse_json(&lex, flags, error);
|
2011-04-11 01:54:52 +08:00
|
|
|
|
|
|
|
lex_close(&lex);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
|
2009-06-14 03:28:26 +08:00
|
|
|
{
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_t lex;
|
2010-10-27 04:36:24 +08:00
|
|
|
const char *source;
|
2009-07-13 16:01:20 +08:00
|
|
|
json_t *result;
|
2009-06-14 03:28:26 +08:00
|
|
|
|
2011-02-22 16:47:02 +08:00
|
|
|
if(lex_init(&lex, (get_func)fgetc, input))
|
2009-07-13 16:01:20 +08:00
|
|
|
return NULL;
|
2009-06-14 03:28:26 +08:00
|
|
|
|
2010-10-27 04:36:24 +08:00
|
|
|
if(input == stdin)
|
|
|
|
source = "<stdin>";
|
|
|
|
else
|
|
|
|
source = "<stream>";
|
|
|
|
|
|
|
|
jsonp_error_init(error, source);
|
2011-05-15 18:57:48 +08:00
|
|
|
result = parse_json(&lex, flags, error);
|
2009-06-14 03:28:26 +08:00
|
|
|
|
2009-07-13 16:01:20 +08:00
|
|
|
lex_close(&lex);
|
2009-06-14 03:28:26 +08:00
|
|
|
return result;
|
|
|
|
}
|
2009-07-28 15:57:17 +08:00
|
|
|
|
2010-10-27 02:05:40 +08:00
|
|
|
json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
|
2009-07-28 15:57:17 +08:00
|
|
|
{
|
|
|
|
json_t *result;
|
|
|
|
FILE *fp;
|
|
|
|
|
2010-10-27 04:36:24 +08:00
|
|
|
jsonp_error_init(error, path);
|
2009-10-27 23:46:57 +08:00
|
|
|
|
2009-07-28 15:57:17 +08:00
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(!fp)
|
|
|
|
{
|
|
|
|
error_set(error, NULL, "unable to open %s: %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-14 03:19:20 +08:00
|
|
|
result = json_loadf(fp, flags, error);
|
2009-07-28 15:57:17 +08:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return result;
|
|
|
|
}
|