MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
json_tokener.c File Reference
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include "bits.h"
#include "debug.h"
#include "printbuf.h"
#include "arraylist.h"
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "json_util.h"

Go to the source code of this file.

Macros

#define NAN   nan("")
 
#define IS_HIGH_SURROGATE(uc)   (((uc) & 0xFC00) == 0xD800)
 
#define IS_LOW_SURROGATE(uc)   (((uc) & 0xFC00) == 0xDC00)
 
#define DECODE_SURROGATE_PAIR(hi, lo)   ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000)
 
#define state   tok->stack[tok->depth].state
 
#define saved_state   tok->stack[tok->depth].saved_state
 
#define current   tok->stack[tok->depth].current
 
#define obj_field_name   tok->stack[tok->depth].obj_field_name
 
#define PEEK_CHAR(dest, tok)
 
#define ADVANCE_CHAR(str, tok)   ( ++(str), ((tok)->char_offset)++, c)
 

Functions

const char * json_tokener_error_desc (enum json_tokener_error jerr)
 
enum json_tokener_error json_tokener_get_error (json_tokener *tok)
 
struct json_tokenerjson_tokener_new_ex (int depth)
 
struct json_tokenerjson_tokener_new (void)
 
void json_tokener_free (struct json_tokener *tok)
 
static void json_tokener_reset_level (struct json_tokener *tok, int depth)
 
void json_tokener_reset (struct json_tokener *tok)
 
struct json_objectjson_tokener_parse (const char *str)
 
struct json_objectjson_tokener_parse_verbose (const char *str, enum json_tokener_error *error)
 
static void put_extarr_val (struct json_object *cur, struct json_object *o, int extarr_idx)
 
struct json_objectjson_tokener_parse_ex (struct json_tokener *tok, const char *str, int len)
 
void json_tokener_set_flags (struct json_tokener *tok, int flags)
 

Variables

static const char json_null_str [] = "null"
 
static const int json_null_str_len = sizeof(json_null_str) - 1
 
static const char json_inf_str [] = "Infinity"
 
static const int json_inf_str_len = sizeof(json_inf_str) - 1
 
static const char json_nan_str [] = "NaN"
 
static const int json_nan_str_len = sizeof(json_nan_str) - 1
 
static const char json_true_str [] = "true"
 
static const int json_true_str_len = sizeof(json_true_str) - 1
 
static const char json_false_str [] = "false"
 
static const int json_false_str_len = sizeof(json_false_str) - 1
 
static const char * json_tokener_errors []
 
static unsigned char utf8_replacement_char [3] = { 0xEF, 0xBF, 0xBD }
 

Macro Definition Documentation

#define NAN   nan("")

Definition at line 55 of file json_tokener.c.

#define IS_HIGH_SURROGATE (   uc)    (((uc) & 0xFC00) == 0xD800)

Definition at line 101 of file json_tokener.c.

#define IS_LOW_SURROGATE (   uc)    (((uc) & 0xFC00) == 0xDC00)

Definition at line 102 of file json_tokener.c.

#define DECODE_SURROGATE_PAIR (   hi,
  lo 
)    ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000)

Definition at line 103 of file json_tokener.c.

#define state   tok->stack[tok->depth].state

Definition at line 186 of file json_tokener.c.

#define saved_state   tok->stack[tok->depth].saved_state

Definition at line 187 of file json_tokener.c.

#define current   tok->stack[tok->depth].current

Definition at line 188 of file json_tokener.c.

#define obj_field_name   tok->stack[tok->depth].obj_field_name

Definition at line 189 of file json_tokener.c.

#define PEEK_CHAR (   dest,
  tok 
)
Value:
(((tok)->char_offset == len) ? \
(((tok)->err = json_tokener_success), 0) \
: \
(((tok)->err = json_tokener_continue), 0) \
) : \
(((dest) = *str), 1) \
)
#define saved_state
Definition: json_tokener.c:187
#define state
Definition: json_tokener.c:186

Definition at line 209 of file json_tokener.c.

#define ADVANCE_CHAR (   str,
  tok 
)    ( ++(str), ((tok)->char_offset)++, c)

Definition at line 224 of file json_tokener.c.

Function Documentation

const char* json_tokener_error_desc ( enum json_tokener_error  jerr)

Given an error previously returned by json_tokener_get_error(), return a human readable description of the error.

Returns
a generic error message is returned if an invalid error value is provided.

Definition at line 87 of file json_tokener.c.

enum json_tokener_error json_tokener_get_error ( struct json_tokener tok)

Retrieve the error caused by the last call to json_tokener_parse_ex(), or json_tokener_success if there is no error.

When parsing a JSON string in pieces, if the tokener is in the middle of parsing this will return json_tokener_continue.

See also json_tokener_error_desc().

Definition at line 95 of file json_tokener.c.

struct json_tokener* json_tokener_new_ex ( int  depth)

Definition at line 106 of file json_tokener.c.

struct json_tokener* json_tokener_new ( void  )

Definition at line 123 of file json_tokener.c.

void json_tokener_free ( struct json_tokener tok)

Definition at line 128 of file json_tokener.c.

static void json_tokener_reset_level ( struct json_tokener tok,
int  depth 
)
static

Definition at line 136 of file json_tokener.c.

void json_tokener_reset ( struct json_tokener tok)

Definition at line 146 of file json_tokener.c.

struct json_object* json_tokener_parse ( const char *  str)

Definition at line 158 of file json_tokener.c.

struct json_object* json_tokener_parse_verbose ( const char *  str,
enum json_tokener_error error 
)

Definition at line 166 of file json_tokener.c.

static void put_extarr_val ( struct json_object cur,
struct json_object o,
int  extarr_idx 
)
static

Definition at line 230 of file json_tokener.c.

struct json_object* json_tokener_parse_ex ( struct json_tokener tok,
const char *  str,
int  len 
)

Parse a string and return a non-NULL json_object if a valid JSON value is found. The string does not need to be a JSON object or array; it can also be a string, number or boolean value.

A partial JSON string can be parsed. If the parsing is incomplete, NULL will be returned and json_tokener_get_error() will be return json_tokener_continue. json_tokener_parse_ex() can then be called with additional bytes in str to continue the parsing.

If json_tokener_parse_ex() returns NULL and the error anything other than json_tokener_continue, a fatal error has occurred and parsing must be halted. Then tok object must not be re-used until json_tokener_reset() is called.

When a valid JSON value is parsed, a non-NULL json_object will be returned. Also, json_tokener_get_error() will return json_tokener_success. Be sure to check the type with json_object_is_type() or json_object_get_type() before using the object.

XXX this shouldn't use internal fields: Trailing characters after the parsed value do not automatically cause an error. It is up to the caller to decide whether to treat this as an error or to handle the additional characters, perhaps by parsing another json value starting from that point.

Extra characters can be detected by comparing the tok->char_offset against the length of the last len parameter passed in.

The tokener does not maintain an internal buffer so the caller is responsible for calling json_tokener_parse_ex with an appropriate str parameter starting with the extra characters.

This interface is presently not 64-bit clean due to the int len argument so the function limits the maximum string size to INT32_MAX (2GB). If the function is called with len == -1 then strlen is called to check the string length is less than INT32_MAX (2GB)

Example:

1 json_object *jobj = NULL;
2 const char *mystring = NULL;
3 int stringlen = 0;
4 enum json_tokener_error jerr;
5 do {
6  mystring = ... // get JSON string, e.g. read from file, etc...
7  stringlen = strlen(mystring);
8  jobj = json_tokener_parse_ex(tok, mystring, stringlen);
9 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
10 if (jerr != json_tokener_success)
11 {
12  fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
13  // Handle errors, as appropriate for your application.
14 }
15 if (tok->char_offset < stringlen) // XXX shouldn't access internal fields
16 {
17  // Handle extra characters after parsed object as desired.
18  // e.g. issue an error, parse another object from that point, etc...
19 }
20 // Success, use jobj here.
Parameters
toka json_tokener previously allocated with json_tokener_new()
stran string with any valid JSON expression, or portion of. This does not need to be null terminated.
lenthe length of str

Definition at line 273 of file json_tokener.c.

void json_tokener_set_flags ( struct json_tokener tok,
int  flags 
)

Set flags that control how parsing will be done.

Definition at line 1097 of file json_tokener.c.

Variable Documentation

const char json_null_str[] = "null"
static

Definition at line 58 of file json_tokener.c.

const int json_null_str_len = sizeof(json_null_str) - 1
static

Definition at line 59 of file json_tokener.c.

const char json_inf_str[] = "Infinity"
static

Definition at line 60 of file json_tokener.c.

const int json_inf_str_len = sizeof(json_inf_str) - 1
static

Definition at line 61 of file json_tokener.c.

const char json_nan_str[] = "NaN"
static

Definition at line 62 of file json_tokener.c.

const int json_nan_str_len = sizeof(json_nan_str) - 1
static

Definition at line 63 of file json_tokener.c.

const char json_true_str[] = "true"
static

Definition at line 64 of file json_tokener.c.

const int json_true_str_len = sizeof(json_true_str) - 1
static

Definition at line 65 of file json_tokener.c.

const char json_false_str[] = "false"
static

Definition at line 66 of file json_tokener.c.

const int json_false_str_len = sizeof(json_false_str) - 1
static

Definition at line 67 of file json_tokener.c.

const char* json_tokener_errors[]
static
Initial value:
= {
"success",
"continue",
"nesting too deep",
"unexpected end of data",
"unexpected character",
"null expected",
"boolean expected",
"number expected",
"array value separator ',' expected",
"quoted object property name expected",
"object property name separator ':' expected",
"object value separator ',' expected",
"invalid string sequence",
"expected comment",
"buffer size overflow"
}

Definition at line 69 of file json_tokener.c.

unsigned char utf8_replacement_char[3] = { 0xEF, 0xBF, 0xBD }
static

Definition at line 104 of file json_tokener.c.