gtl-parsley-ruby 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +3 -0
- data/README +32 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/ext/cparsley.c +152 -0
- data/ext/extconf.rb +82 -0
- data/ext/parsley/.gitignore +32 -0
- data/ext/parsley/AUTHORS +1 -0
- data/ext/parsley/ChangeLog +0 -0
- data/ext/parsley/HACKING +4 -0
- data/ext/parsley/INSTALL +73 -0
- data/ext/parsley/INTRO +84 -0
- data/ext/parsley/Makefile.am +80 -0
- data/ext/parsley/Makefile.in +1009 -0
- data/ext/parsley/NEWS +0 -0
- data/ext/parsley/PAPER +36 -0
- data/ext/parsley/Portfile +18 -0
- data/ext/parsley/Portfile.in +17 -0
- data/ext/parsley/README.C-LANG +92 -0
- data/ext/parsley/README.markdown +1 -0
- data/ext/parsley/TODO +39 -0
- data/ext/parsley/VERSION +1 -0
- data/ext/parsley/aclocal.m4 +8918 -0
- data/ext/parsley/bootstrap.sh +6 -0
- data/ext/parsley/config.guess +1561 -0
- data/ext/parsley/config.sub +1686 -0
- data/ext/parsley/configure +13437 -0
- data/ext/parsley/configure.ac +46 -0
- data/ext/parsley/depcomp +630 -0
- data/ext/parsley/functions.c +368 -0
- data/ext/parsley/functions.h +19 -0
- data/ext/parsley/generate_bisect.sh +12 -0
- data/ext/parsley/hooks/prepare-commit-msg +16 -0
- data/ext/parsley/install-sh +520 -0
- data/ext/parsley/json-c-0.9/AUTHORS +2 -0
- data/ext/parsley/json-c-0.9/COPYING +19 -0
- data/ext/parsley/json-c-0.9/ChangeLog +103 -0
- data/ext/parsley/json-c-0.9/INSTALL +302 -0
- data/ext/parsley/json-c-0.9/Makefile.am +43 -0
- data/ext/parsley/json-c-0.9/Makefile.in +800 -0
- data/ext/parsley/json-c-0.9/NEWS +1 -0
- data/ext/parsley/json-c-0.9/README +20 -0
- data/ext/parsley/json-c-0.9/README-WIN32.html +57 -0
- data/ext/parsley/json-c-0.9/README.html +32 -0
- data/ext/parsley/json-c-0.9/aclocal.m4 +8909 -0
- data/ext/parsley/json-c-0.9/arraylist.c +94 -0
- data/ext/parsley/json-c-0.9/arraylist.h +53 -0
- data/ext/parsley/json-c-0.9/bits.h +27 -0
- data/ext/parsley/json-c-0.9/config.guess +1561 -0
- data/ext/parsley/json-c-0.9/config.h +125 -0
- data/ext/parsley/json-c-0.9/config.h.in +124 -0
- data/ext/parsley/json-c-0.9/config.h.win32 +94 -0
- data/ext/parsley/json-c-0.9/config.sub +1686 -0
- data/ext/parsley/json-c-0.9/configure +13084 -0
- data/ext/parsley/json-c-0.9/configure.in +33 -0
- data/ext/parsley/json-c-0.9/debug.c +98 -0
- data/ext/parsley/json-c-0.9/debug.h +50 -0
- data/ext/parsley/json-c-0.9/depcomp +630 -0
- data/ext/parsley/json-c-0.9/doc/html/annotated.html +40 -0
- data/ext/parsley/json-c-0.9/doc/html/arraylist_8h.html +240 -0
- data/ext/parsley/json-c-0.9/doc/html/bits_8h.html +150 -0
- data/ext/parsley/json-c-0.9/doc/html/classes.html +36 -0
- data/ext/parsley/json-c-0.9/doc/html/config_8h.html +612 -0
- data/ext/parsley/json-c-0.9/doc/html/debug_8h.html +392 -0
- data/ext/parsley/json-c-0.9/doc/html/doxygen.css +441 -0
- data/ext/parsley/json-c-0.9/doc/html/doxygen.png +0 -0
- data/ext/parsley/json-c-0.9/doc/html/files.html +42 -0
- data/ext/parsley/json-c-0.9/doc/html/functions.html +206 -0
- data/ext/parsley/json-c-0.9/doc/html/functions_vars.html +206 -0
- data/ext/parsley/json-c-0.9/doc/html/globals.html +459 -0
- data/ext/parsley/json-c-0.9/doc/html/globals_defs.html +202 -0
- data/ext/parsley/json-c-0.9/doc/html/globals_enum.html +50 -0
- data/ext/parsley/json-c-0.9/doc/html/globals_eval.html +135 -0
- data/ext/parsley/json-c-0.9/doc/html/globals_func.html +194 -0
- data/ext/parsley/json-c-0.9/doc/html/globals_type.html +70 -0
- data/ext/parsley/json-c-0.9/doc/html/globals_vars.html +50 -0
- data/ext/parsley/json-c-0.9/doc/html/index.html +25 -0
- data/ext/parsley/json-c-0.9/doc/html/json_8h.html +32 -0
- data/ext/parsley/json-c-0.9/doc/html/json__object_8h.html +1150 -0
- data/ext/parsley/json-c-0.9/doc/html/json__object__private_8h.html +75 -0
- data/ext/parsley/json-c-0.9/doc/html/json__tokener_8h.html +366 -0
- data/ext/parsley/json-c-0.9/doc/html/json__util_8h.html +106 -0
- data/ext/parsley/json-c-0.9/doc/html/linkhash_8h.html +740 -0
- data/ext/parsley/json-c-0.9/doc/html/printbuf_8h.html +214 -0
- data/ext/parsley/json-c-0.9/doc/html/structarray__list.html +104 -0
- data/ext/parsley/json-c-0.9/doc/html/structjson__object.html +141 -0
- data/ext/parsley/json-c-0.9/doc/html/structjson__object__iter.html +87 -0
- data/ext/parsley/json-c-0.9/doc/html/structjson__tokener.html +206 -0
- data/ext/parsley/json-c-0.9/doc/html/structjson__tokener__srec.html +104 -0
- data/ext/parsley/json-c-0.9/doc/html/structlh__entry.html +105 -0
- data/ext/parsley/json-c-0.9/doc/html/structlh__table.html +275 -0
- data/ext/parsley/json-c-0.9/doc/html/structprintbuf.html +87 -0
- data/ext/parsley/json-c-0.9/doc/html/tab_b.gif +0 -0
- data/ext/parsley/json-c-0.9/doc/html/tab_l.gif +0 -0
- data/ext/parsley/json-c-0.9/doc/html/tab_r.gif +0 -0
- data/ext/parsley/json-c-0.9/doc/html/tabs.css +105 -0
- data/ext/parsley/json-c-0.9/doc/html/unionjson__object_1_1data.html +140 -0
- data/ext/parsley/json-c-0.9/install-sh +520 -0
- data/ext/parsley/json-c-0.9/json.h +31 -0
- data/ext/parsley/json-c-0.9/json.pc +11 -0
- data/ext/parsley/json-c-0.9/json.pc.in +11 -0
- data/ext/parsley/json-c-0.9/json_object.c +512 -0
- data/ext/parsley/json-c-0.9/json_object.h +319 -0
- data/ext/parsley/json-c-0.9/json_object_private.h +52 -0
- data/ext/parsley/json-c-0.9/json_tokener.c +628 -0
- data/ext/parsley/json-c-0.9/json_tokener.h +98 -0
- data/ext/parsley/json-c-0.9/json_util.c +122 -0
- data/ext/parsley/json-c-0.9/json_util.h +31 -0
- data/ext/parsley/json-c-0.9/libjson.la +41 -0
- data/ext/parsley/json-c-0.9/libtool +8890 -0
- data/ext/parsley/json-c-0.9/linkhash.c +216 -0
- data/ext/parsley/json-c-0.9/linkhash.h +272 -0
- data/ext/parsley/json-c-0.9/ltmain.sh +8406 -0
- data/ext/parsley/json-c-0.9/missing +376 -0
- data/ext/parsley/json-c-0.9/printbuf.c +149 -0
- data/ext/parsley/json-c-0.9/printbuf.h +64 -0
- data/ext/parsley/json-c-0.9/stamp-h1 +1 -0
- data/ext/parsley/json-c-0.9/test1 +130 -0
- data/ext/parsley/json-c-0.9/test1.c +164 -0
- data/ext/parsley/json-c-0.9/test2 +130 -0
- data/ext/parsley/json-c-0.9/test2.c +20 -0
- data/ext/parsley/json-c-0.9/test3 +130 -0
- data/ext/parsley/json-c-0.9/test3.c +23 -0
- data/ext/parsley/libtool +8890 -0
- data/ext/parsley/ltmain.sh +8406 -0
- data/ext/parsley/missing +376 -0
- data/ext/parsley/parsed_xpath.c +168 -0
- data/ext/parsley/parsed_xpath.h +34 -0
- data/ext/parsley/parser.y +631 -0
- data/ext/parsley/parsley.c +793 -0
- data/ext/parsley/parsley.h +87 -0
- data/ext/parsley/parsley_main.c +185 -0
- data/ext/parsley/parsleyc_main.c +108 -0
- data/ext/parsley/regexp.c +359 -0
- data/ext/parsley/regexp.h +36 -0
- data/ext/parsley/scanner.l +221 -0
- data/ext/parsley/test/ambiguous.html +207 -0
- data/ext/parsley/test/ambiguous.json +1 -0
- data/ext/parsley/test/ambiguous.let +6 -0
- data/ext/parsley/test/array-regression.html +5 -0
- data/ext/parsley/test/array-regression.json +1 -0
- data/ext/parsley/test/array-regression.let +10 -0
- data/ext/parsley/test/backslash.html +5 -0
- data/ext/parsley/test/backslash.json +1 -0
- data/ext/parsley/test/backslash.let +3 -0
- data/ext/parsley/test/bang.html +17 -0
- data/ext/parsley/test/bang.json +1 -0
- data/ext/parsley/test/bang.let +6 -0
- data/ext/parsley/test/collate_regression.html +324 -0
- data/ext/parsley/test/collate_regression.json +1 -0
- data/ext/parsley/test/collate_regression.let +9 -0
- data/ext/parsley/test/contains.html +3 -0
- data/ext/parsley/test/contains.json +1 -0
- data/ext/parsley/test/contains.let +3 -0
- data/ext/parsley/test/content.html +13 -0
- data/ext/parsley/test/content.json +1 -0
- data/ext/parsley/test/content.let +7 -0
- data/ext/parsley/test/cool.html +575 -0
- data/ext/parsley/test/cool.json +1 -0
- data/ext/parsley/test/cool.let +9 -0
- data/ext/parsley/test/craigs-simple.html +207 -0
- data/ext/parsley/test/craigs-simple.json +1 -0
- data/ext/parsley/test/craigs-simple.let +6 -0
- data/ext/parsley/test/craigs.html +207 -0
- data/ext/parsley/test/craigs.json +1 -0
- data/ext/parsley/test/craigs.let +9 -0
- data/ext/parsley/test/crash.html +157 -0
- data/ext/parsley/test/crash.json +1 -0
- data/ext/parsley/test/crash.let +1 -0
- data/ext/parsley/test/css_attr.html +3 -0
- data/ext/parsley/test/css_attr.json +1 -0
- data/ext/parsley/test/css_attr.let +3 -0
- data/ext/parsley/test/default-namespace.json +1 -0
- data/ext/parsley/test/default-namespace.let +3 -0
- data/ext/parsley/test/default-namespace.xml +1493 -0
- data/ext/parsley/test/div.html +8 -0
- data/ext/parsley/test/div.json +1 -0
- data/ext/parsley/test/div.let +10 -0
- data/ext/parsley/test/empty.html +3 -0
- data/ext/parsley/test/empty.json +1 -0
- data/ext/parsley/test/empty.let +1 -0
- data/ext/parsley/test/emptyish.html +207 -0
- data/ext/parsley/test/emptyish.let +3 -0
- data/ext/parsley/test/fictional-opt.html +43 -0
- data/ext/parsley/test/fictional-opt.json +1 -0
- data/ext/parsley/test/fictional-opt.let +14 -0
- data/ext/parsley/test/fictional.html +43 -0
- data/ext/parsley/test/fictional.json +1 -0
- data/ext/parsley/test/fictional.let +14 -0
- data/ext/parsley/test/function-magic.html +9 -0
- data/ext/parsley/test/function-magic.json +1 -0
- data/ext/parsley/test/function-magic.let +8 -0
- data/ext/parsley/test/hn.html +32 -0
- data/ext/parsley/test/hn.json +1 -0
- data/ext/parsley/test/hn.let +8 -0
- data/ext/parsley/test/malformed-array.html +2329 -0
- data/ext/parsley/test/malformed-array.json +1 -0
- data/ext/parsley/test/malformed-array.let +22 -0
- data/ext/parsley/test/malformed-expr.html +2329 -0
- data/ext/parsley/test/malformed-expr.json +1 -0
- data/ext/parsley/test/malformed-expr.let +16 -0
- data/ext/parsley/test/malformed-function.html +845 -0
- data/ext/parsley/test/malformed-function.json +197 -0
- data/ext/parsley/test/malformed-function.let +8 -0
- data/ext/parsley/test/malformed-json.html +2329 -0
- data/ext/parsley/test/malformed-json.json +1 -0
- data/ext/parsley/test/malformed-json.let +6 -0
- data/ext/parsley/test/malformed-xpath.html +8 -0
- data/ext/parsley/test/malformed-xpath.json +1 -0
- data/ext/parsley/test/malformed-xpath.let +7 -0
- data/ext/parsley/test/match.json +1 -0
- data/ext/parsley/test/match.let +9 -0
- data/ext/parsley/test/match.xml +11 -0
- data/ext/parsley/test/math_ambiguity.html +9 -0
- data/ext/parsley/test/math_ambiguity.json +1 -0
- data/ext/parsley/test/math_ambiguity.let +5 -0
- data/ext/parsley/test/nth-regression.html +13 -0
- data/ext/parsley/test/nth-regression.json +1 -0
- data/ext/parsley/test/nth-regression.let +3 -0
- data/ext/parsley/test/optional.html +2328 -0
- data/ext/parsley/test/optional.json +1 -0
- data/ext/parsley/test/optional.let +8 -0
- data/ext/parsley/test/outer-xml.html +6 -0
- data/ext/parsley/test/outer-xml.json +1 -0
- data/ext/parsley/test/outer-xml.let +5 -0
- data/ext/parsley/test/position.html +8 -0
- data/ext/parsley/test/position.json +1 -0
- data/ext/parsley/test/position.let +6 -0
- data/ext/parsley/test/question_regressions.html +443 -0
- data/ext/parsley/test/question_regressions.json +1 -0
- data/ext/parsley/test/question_regressions.let +6 -0
- data/ext/parsley/test/quote.json +1 -0
- data/ext/parsley/test/quote.let +8 -0
- data/ext/parsley/test/quote.xml +11 -0
- data/ext/parsley/test/reddit.html +1 -0
- data/ext/parsley/test/reddit.json +1 -0
- data/ext/parsley/test/reddit.let +12 -0
- data/ext/parsley/test/remote-fail.json +1 -0
- data/ext/parsley/test/remote.html +3 -0
- data/ext/parsley/test/remote.json +1 -0
- data/ext/parsley/test/remote.let +4 -0
- data/ext/parsley/test/replace.json +1 -0
- data/ext/parsley/test/replace.let +9 -0
- data/ext/parsley/test/replace.xml +11 -0
- data/ext/parsley/test/scope.html +10 -0
- data/ext/parsley/test/scope.json +1 -0
- data/ext/parsley/test/scope.let +6 -0
- data/ext/parsley/test/segfault.html +5 -0
- data/ext/parsley/test/segfault.json +1 -0
- data/ext/parsley/test/segfault.let +9 -0
- data/ext/parsley/test/sg-wrap.html +5 -0
- data/ext/parsley/test/sg-wrap.json +1 -0
- data/ext/parsley/test/sg-wrap.let +3 -0
- data/ext/parsley/test/sg_off.html +5 -0
- data/ext/parsley/test/sg_off.json +1 -0
- data/ext/parsley/test/sg_off.let +3 -0
- data/ext/parsley/test/test.json +1 -0
- data/ext/parsley/test/test.let +6 -0
- data/ext/parsley/test/test.xml +11 -0
- data/ext/parsley/test/trivial.html +2329 -0
- data/ext/parsley/test/trivial.json +1 -0
- data/ext/parsley/test/trivial.let +4 -0
- data/ext/parsley/test/trivial2.html +2329 -0
- data/ext/parsley/test/trivial2.json +1 -0
- data/ext/parsley/test/trivial2.let +7 -0
- data/ext/parsley/test/unbang.html +17 -0
- data/ext/parsley/test/unbang.json +1 -0
- data/ext/parsley/test/unbang.let +6 -0
- data/ext/parsley/test/unicode.html +3 -0
- data/ext/parsley/test/unicode.json +1 -0
- data/ext/parsley/test/unicode.let +1 -0
- data/ext/parsley/test/whitespace.html +8 -0
- data/ext/parsley/test/whitespace.json +1 -0
- data/ext/parsley/test/whitespace.let +3 -0
- data/ext/parsley/test/whitespace_regression.html +4 -0
- data/ext/parsley/test/whitespace_regression.json +1 -0
- data/ext/parsley/test/whitespace_regression.let +3 -0
- data/ext/parsley/test/yelp-benchmark.rb +53 -0
- data/ext/parsley/test/yelp-home.html +1004 -0
- data/ext/parsley/test/yelp-home.json +1 -0
- data/ext/parsley/test/yelp-home.let +6 -0
- data/ext/parsley/test/yelp.html +2329 -0
- data/ext/parsley/test/yelp.json +1 -0
- data/ext/parsley/test/yelp.let +12 -0
- data/ext/parsley/test/youtube.html +1940 -0
- data/ext/parsley/test/youtube.let +11 -0
- data/ext/parsley/util.c +237 -0
- data/ext/parsley/util.h +34 -0
- data/ext/parsley/xml2json.c +47 -0
- data/ext/parsley/xml2json.h +14 -0
- data/ext/parsley/y.tab.h +222 -0
- data/ext/parsley/ylwrap +222 -0
- data/lib/parsley.rb +84 -0
- data/test/test_parsley.rb +120 -0
- data/test/yelp-benchmark.rb +53 -0
- data/test/yelp-home.html +1004 -0
- data/test/yelp-home.let +6 -0
- data/test/yelp.html +2329 -0
- metadata +366 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
|
5
|
+
* Michael Clark <michael@metaparadigm.com>
|
|
6
|
+
*
|
|
7
|
+
* This library is free software; you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT license. See COPYING for details.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
#ifndef _json_object_h_
|
|
13
|
+
#define _json_object_h_
|
|
14
|
+
|
|
15
|
+
#ifdef __cplusplus
|
|
16
|
+
extern "C" {
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#define JSON_OBJECT_DEF_HASH_ENTRIES 16
|
|
20
|
+
|
|
21
|
+
#undef FALSE
|
|
22
|
+
#define FALSE ((boolean)0)
|
|
23
|
+
|
|
24
|
+
#undef TRUE
|
|
25
|
+
#define TRUE ((boolean)1)
|
|
26
|
+
|
|
27
|
+
extern const char *json_number_chars;
|
|
28
|
+
extern const char *json_hex_chars;
|
|
29
|
+
|
|
30
|
+
/* forward structure definitions */
|
|
31
|
+
|
|
32
|
+
typedef int boolean;
|
|
33
|
+
typedef struct printbuf printbuf;
|
|
34
|
+
typedef struct lh_table lh_table;
|
|
35
|
+
typedef struct array_list array_list;
|
|
36
|
+
typedef struct json_object json_object;
|
|
37
|
+
typedef struct json_object_iter json_object_iter;
|
|
38
|
+
typedef struct json_tokener json_tokener;
|
|
39
|
+
|
|
40
|
+
/* supported object types */
|
|
41
|
+
|
|
42
|
+
typedef enum json_type {
|
|
43
|
+
json_type_null,
|
|
44
|
+
json_type_boolean,
|
|
45
|
+
json_type_double,
|
|
46
|
+
json_type_int,
|
|
47
|
+
json_type_object,
|
|
48
|
+
json_type_array,
|
|
49
|
+
json_type_string
|
|
50
|
+
} json_type;
|
|
51
|
+
|
|
52
|
+
/* reference counting functions */
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Increment the reference count of json_object
|
|
56
|
+
* @param obj the json_object instance
|
|
57
|
+
*/
|
|
58
|
+
extern struct json_object* json_object_get(struct json_object *obj);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Decrement the reference count of json_object and free if it reaches zero
|
|
62
|
+
* @param obj the json_object instance
|
|
63
|
+
*/
|
|
64
|
+
extern void json_object_put(struct json_object *obj);
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check if the json_object is of a given type
|
|
69
|
+
* @param obj the json_object instance
|
|
70
|
+
* @param type one of:
|
|
71
|
+
json_type_boolean,
|
|
72
|
+
json_type_double,
|
|
73
|
+
json_type_int,
|
|
74
|
+
json_type_object,
|
|
75
|
+
json_type_array,
|
|
76
|
+
json_type_string,
|
|
77
|
+
*/
|
|
78
|
+
extern int json_object_is_type(struct json_object *obj, enum json_type type);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the type of the json_object
|
|
82
|
+
* @param obj the json_object instance
|
|
83
|
+
* @returns type being one of:
|
|
84
|
+
json_type_boolean,
|
|
85
|
+
json_type_double,
|
|
86
|
+
json_type_int,
|
|
87
|
+
json_type_object,
|
|
88
|
+
json_type_array,
|
|
89
|
+
json_type_string,
|
|
90
|
+
*/
|
|
91
|
+
extern enum json_type json_object_get_type(struct json_object *obj);
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/** Stringify object to json format
|
|
95
|
+
* @param obj the json_object instance
|
|
96
|
+
* @returns a string in JSON format
|
|
97
|
+
*/
|
|
98
|
+
extern const char* json_object_to_json_string(struct json_object *obj);
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
/* object type methods */
|
|
102
|
+
|
|
103
|
+
/** Create a new empty object
|
|
104
|
+
* @returns a json_object of type json_type_object
|
|
105
|
+
*/
|
|
106
|
+
extern struct json_object* json_object_new_object(void);
|
|
107
|
+
|
|
108
|
+
/** Get the hashtable of a json_object of type json_type_object
|
|
109
|
+
* @param obj the json_object instance
|
|
110
|
+
* @returns a linkhash
|
|
111
|
+
*/
|
|
112
|
+
extern struct lh_table* json_object_get_object(struct json_object *obj);
|
|
113
|
+
|
|
114
|
+
/** Add an object field to a json_object of type json_type_object
|
|
115
|
+
*
|
|
116
|
+
* The reference count will *not* be incremented. This is to make adding
|
|
117
|
+
* fields to objects in code more compact. If you want to retain a reference
|
|
118
|
+
* to an added object you must wrap the passed object with json_object_get
|
|
119
|
+
*
|
|
120
|
+
* @param obj the json_object instance
|
|
121
|
+
* @param key the object field name (a private copy will be duplicated)
|
|
122
|
+
* @param val a json_object or NULL member to associate with the given field
|
|
123
|
+
*/
|
|
124
|
+
extern void json_object_object_add(struct json_object* obj, const char *key,
|
|
125
|
+
struct json_object *val);
|
|
126
|
+
|
|
127
|
+
/** Get the json_object associate with a given object field
|
|
128
|
+
* @param obj the json_object instance
|
|
129
|
+
* @param key the object field name
|
|
130
|
+
* @returns the json_object associated with the given field name
|
|
131
|
+
*/
|
|
132
|
+
extern struct json_object* json_object_object_get(struct json_object* obj,
|
|
133
|
+
const char *key);
|
|
134
|
+
|
|
135
|
+
/** Delete the given json_object field
|
|
136
|
+
*
|
|
137
|
+
* The reference count will be decremented for the deleted object
|
|
138
|
+
*
|
|
139
|
+
* @param obj the json_object instance
|
|
140
|
+
* @param key the object field name
|
|
141
|
+
*/
|
|
142
|
+
extern void json_object_object_del(struct json_object* obj, const char *key);
|
|
143
|
+
|
|
144
|
+
/** Iterate through all keys and values of an object
|
|
145
|
+
* @param obj the json_object instance
|
|
146
|
+
* @param key the local name for the char* key variable defined in the body
|
|
147
|
+
* @param val the local name for the json_object* object variable defined in the body
|
|
148
|
+
*/
|
|
149
|
+
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
|
150
|
+
|
|
151
|
+
# define json_object_object_foreach(obj,key,val) \
|
|
152
|
+
char *key; struct json_object *val; \
|
|
153
|
+
for(struct lh_entry *entry = json_object_get_object(obj)->head; ({ if(entry) { key = (char*)entry->k; val = (struct json_object*)entry->v; } ; entry; }); entry = entry->next )
|
|
154
|
+
|
|
155
|
+
#else /* ANSI C or MSC */
|
|
156
|
+
|
|
157
|
+
# define json_object_object_foreach(obj,key,val) \
|
|
158
|
+
char *key; struct json_object *val; struct lh_entry *entry; \
|
|
159
|
+
for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
|
|
160
|
+
|
|
161
|
+
#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
|
|
162
|
+
|
|
163
|
+
/** Iterate through all keys and values of an object (ANSI C Safe)
|
|
164
|
+
* @param obj the json_object instance
|
|
165
|
+
* @param iter the object iterator
|
|
166
|
+
*/
|
|
167
|
+
#define json_object_object_foreachC(obj,iter) \
|
|
168
|
+
for(iter.entry = json_object_get_object(obj)->head; (iter.entry ? (iter.key = (char*)iter.entry->k, iter.val = (struct json_object*)iter.entry->v, iter.entry) : 0); iter.entry = iter.entry->next)
|
|
169
|
+
|
|
170
|
+
/* Array type methods */
|
|
171
|
+
|
|
172
|
+
/** Create a new empty json_object of type json_type_array
|
|
173
|
+
* @returns a json_object of type json_type_array
|
|
174
|
+
*/
|
|
175
|
+
extern struct json_object* json_object_new_array(void);
|
|
176
|
+
|
|
177
|
+
/** Get the arraylist of a json_object of type json_type_array
|
|
178
|
+
* @param obj the json_object instance
|
|
179
|
+
* @returns an arraylist
|
|
180
|
+
*/
|
|
181
|
+
extern struct array_list* json_object_get_array(struct json_object *obj);
|
|
182
|
+
|
|
183
|
+
/** Get the length of a json_object of type json_type_array
|
|
184
|
+
* @param obj the json_object instance
|
|
185
|
+
* @returns an int
|
|
186
|
+
*/
|
|
187
|
+
extern int json_object_array_length(struct json_object *obj);
|
|
188
|
+
|
|
189
|
+
/** Add an element to the end of a json_object of type json_type_array
|
|
190
|
+
*
|
|
191
|
+
* The reference count will *not* be incremented. This is to make adding
|
|
192
|
+
* fields to objects in code more compact. If you want to retain a reference
|
|
193
|
+
* to an added object you must wrap the passed object with json_object_get
|
|
194
|
+
*
|
|
195
|
+
* @param obj the json_object instance
|
|
196
|
+
* @param val the json_object to be added
|
|
197
|
+
*/
|
|
198
|
+
extern int json_object_array_add(struct json_object *obj,
|
|
199
|
+
struct json_object *val);
|
|
200
|
+
|
|
201
|
+
/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
|
|
202
|
+
*
|
|
203
|
+
* The reference count will *not* be incremented. This is to make adding
|
|
204
|
+
* fields to objects in code more compact. If you want to retain a reference
|
|
205
|
+
* to an added object you must wrap the passed object with json_object_get
|
|
206
|
+
*
|
|
207
|
+
* The reference count of a replaced object will be decremented.
|
|
208
|
+
*
|
|
209
|
+
* The array size will be automatically be expanded to the size of the
|
|
210
|
+
* index if the index is larger than the current size.
|
|
211
|
+
*
|
|
212
|
+
* @param obj the json_object instance
|
|
213
|
+
* @param idx the index to insert the element at
|
|
214
|
+
* @param val the json_object to be added
|
|
215
|
+
*/
|
|
216
|
+
extern int json_object_array_put_idx(struct json_object *obj, int idx,
|
|
217
|
+
struct json_object *val);
|
|
218
|
+
|
|
219
|
+
/** Get the element at specificed index of the array (a json_object of type json_type_array)
|
|
220
|
+
* @param obj the json_object instance
|
|
221
|
+
* @param idx the index to get the element at
|
|
222
|
+
* @returns the json_object at the specified index (or NULL)
|
|
223
|
+
*/
|
|
224
|
+
extern struct json_object* json_object_array_get_idx(struct json_object *obj,
|
|
225
|
+
int idx);
|
|
226
|
+
|
|
227
|
+
/* boolean type methods */
|
|
228
|
+
|
|
229
|
+
/** Create a new empty json_object of type json_type_boolean
|
|
230
|
+
* @param b a boolean TRUE or FALSE (0 or 1)
|
|
231
|
+
* @returns a json_object of type json_type_boolean
|
|
232
|
+
*/
|
|
233
|
+
extern struct json_object* json_object_new_boolean(boolean b);
|
|
234
|
+
|
|
235
|
+
/** Get the boolean value of a json_object
|
|
236
|
+
*
|
|
237
|
+
* The type is coerced to a boolean if the passed object is not a boolean.
|
|
238
|
+
* integer and double objects will return FALSE if there value is zero
|
|
239
|
+
* or TRUE otherwise. If the passed object is a string it will return
|
|
240
|
+
* TRUE if it has a non zero length. If any other object type is passed
|
|
241
|
+
* TRUE will be returned if the object is not NULL.
|
|
242
|
+
*
|
|
243
|
+
* @param obj the json_object instance
|
|
244
|
+
* @returns a boolean
|
|
245
|
+
*/
|
|
246
|
+
extern boolean json_object_get_boolean(struct json_object *obj);
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
/* int type methods */
|
|
250
|
+
|
|
251
|
+
/** Create a new empty json_object of type json_type_int
|
|
252
|
+
* @param i the integer
|
|
253
|
+
* @returns a json_object of type json_type_int
|
|
254
|
+
*/
|
|
255
|
+
extern struct json_object* json_object_new_int(int i);
|
|
256
|
+
|
|
257
|
+
/** Get the int value of a json_object
|
|
258
|
+
*
|
|
259
|
+
* The type is coerced to a int if the passed object is not a int.
|
|
260
|
+
* double objects will return their integer conversion. Strings will be
|
|
261
|
+
* parsed as an integer. If no conversion exists then 0 is returned.
|
|
262
|
+
*
|
|
263
|
+
* @param obj the json_object instance
|
|
264
|
+
* @returns an int
|
|
265
|
+
*/
|
|
266
|
+
extern int json_object_get_int(struct json_object *obj);
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
/* double type methods */
|
|
270
|
+
|
|
271
|
+
/** Create a new empty json_object of type json_type_double
|
|
272
|
+
* @param d the double
|
|
273
|
+
* @returns a json_object of type json_type_double
|
|
274
|
+
*/
|
|
275
|
+
extern struct json_object* json_object_new_double(double d);
|
|
276
|
+
|
|
277
|
+
/** Get the double value of a json_object
|
|
278
|
+
*
|
|
279
|
+
* The type is coerced to a double if the passed object is not a double.
|
|
280
|
+
* integer objects will return their dboule conversion. Strings will be
|
|
281
|
+
* parsed as a double. If no conversion exists then 0.0 is returned.
|
|
282
|
+
*
|
|
283
|
+
* @param obj the json_object instance
|
|
284
|
+
* @returns an double
|
|
285
|
+
*/
|
|
286
|
+
extern double json_object_get_double(struct json_object *obj);
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
/* string type methods */
|
|
290
|
+
|
|
291
|
+
/** Create a new empty json_object of type json_type_string
|
|
292
|
+
*
|
|
293
|
+
* A copy of the string is made and the memory is managed by the json_object
|
|
294
|
+
*
|
|
295
|
+
* @param s the string
|
|
296
|
+
* @returns a json_object of type json_type_string
|
|
297
|
+
*/
|
|
298
|
+
extern struct json_object* json_object_new_string(const char *s);
|
|
299
|
+
|
|
300
|
+
extern struct json_object* json_object_new_string_len(const char *s, int len);
|
|
301
|
+
|
|
302
|
+
/** Get the string value of a json_object
|
|
303
|
+
*
|
|
304
|
+
* If the passed object is not of type json_type_string then the JSON
|
|
305
|
+
* representation of the object is returned.
|
|
306
|
+
*
|
|
307
|
+
* The returned string memory is managed by the json_object and will
|
|
308
|
+
* be freed when the reference count of the json_object drops to zero.
|
|
309
|
+
*
|
|
310
|
+
* @param obj the json_object instance
|
|
311
|
+
* @returns a string
|
|
312
|
+
*/
|
|
313
|
+
extern const char* json_object_get_string(struct json_object *obj);
|
|
314
|
+
|
|
315
|
+
#ifdef __cplusplus
|
|
316
|
+
}
|
|
317
|
+
#endif
|
|
318
|
+
|
|
319
|
+
#endif
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* $Id: json_object_private.h,v 1.4 2006/01/26 02:16:28 mclark Exp $
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
|
5
|
+
* Michael Clark <michael@metaparadigm.com>
|
|
6
|
+
*
|
|
7
|
+
* This library is free software; you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT license. See COPYING for details.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
#ifndef _json_object_private_h_
|
|
13
|
+
#define _json_object_private_h_
|
|
14
|
+
|
|
15
|
+
#ifdef __cplusplus
|
|
16
|
+
extern "C" {
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
typedef void (json_object_delete_fn)(struct json_object *o);
|
|
20
|
+
typedef int (json_object_to_json_string_fn)(struct json_object *o,
|
|
21
|
+
struct printbuf *pb);
|
|
22
|
+
|
|
23
|
+
struct json_object
|
|
24
|
+
{
|
|
25
|
+
enum json_type o_type;
|
|
26
|
+
json_object_delete_fn *_delete;
|
|
27
|
+
json_object_to_json_string_fn *_to_json_string;
|
|
28
|
+
int _ref_count;
|
|
29
|
+
struct printbuf *_pb;
|
|
30
|
+
union data {
|
|
31
|
+
boolean c_boolean;
|
|
32
|
+
double c_double;
|
|
33
|
+
int c_int;
|
|
34
|
+
struct lh_table *c_object;
|
|
35
|
+
struct array_list *c_array;
|
|
36
|
+
char *c_string;
|
|
37
|
+
} o;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/* CAW: added for ANSI C iteration correctness */
|
|
41
|
+
struct json_object_iter
|
|
42
|
+
{
|
|
43
|
+
char *key;
|
|
44
|
+
struct json_object *val;
|
|
45
|
+
struct lh_entry *entry;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
#ifdef __cplusplus
|
|
49
|
+
}
|
|
50
|
+
#endif
|
|
51
|
+
|
|
52
|
+
#endif
|
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
|
5
|
+
* Michael Clark <michael@metaparadigm.com>
|
|
6
|
+
*
|
|
7
|
+
* This library is free software; you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT license. See COPYING for details.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
|
|
12
|
+
* The copyrights to the contents of this file are licensed under the MIT License
|
|
13
|
+
* (http://www.opensource.org/licenses/mit-license.php)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#include "config.h"
|
|
17
|
+
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
#include <stdlib.h>
|
|
20
|
+
#include <stddef.h>
|
|
21
|
+
#include <ctype.h>
|
|
22
|
+
#include <string.h>
|
|
23
|
+
|
|
24
|
+
#include "bits.h"
|
|
25
|
+
#include "debug.h"
|
|
26
|
+
#include "printbuf.h"
|
|
27
|
+
#include "arraylist.h"
|
|
28
|
+
#include "json_object.h"
|
|
29
|
+
#include "json_tokener.h"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
#if !HAVE_STRNCASECMP && defined(_MSC_VER)
|
|
33
|
+
/* MSC has the version as _strnicmp */
|
|
34
|
+
# define strncasecmp _strnicmp
|
|
35
|
+
#elif !HAVE_STRNCASECMP
|
|
36
|
+
# error You do not have strncasecmp on your system.
|
|
37
|
+
#endif /* HAVE_STRNCASECMP */
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
static const char* json_null_str = "null";
|
|
41
|
+
static const char* json_true_str = "true";
|
|
42
|
+
static const char* json_false_str = "false";
|
|
43
|
+
|
|
44
|
+
const char* json_tokener_errors[] = {
|
|
45
|
+
"success",
|
|
46
|
+
"continue",
|
|
47
|
+
"nesting to deep",
|
|
48
|
+
"unexpected end of data",
|
|
49
|
+
"unexpected character",
|
|
50
|
+
"null expected",
|
|
51
|
+
"boolean expected",
|
|
52
|
+
"number expected",
|
|
53
|
+
"array value separator ',' expected",
|
|
54
|
+
"quoted object property name expected",
|
|
55
|
+
"object property name separator ':' expected",
|
|
56
|
+
"object value separator ',' expected",
|
|
57
|
+
"invalid string sequence",
|
|
58
|
+
"expected comment",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
struct json_tokener* json_tokener_new(void)
|
|
63
|
+
{
|
|
64
|
+
struct json_tokener *tok;
|
|
65
|
+
|
|
66
|
+
tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener));
|
|
67
|
+
if (!tok) return NULL;
|
|
68
|
+
tok->pb = printbuf_new();
|
|
69
|
+
json_tokener_reset(tok);
|
|
70
|
+
return tok;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
void json_tokener_free(struct json_tokener *tok)
|
|
74
|
+
{
|
|
75
|
+
json_tokener_reset(tok);
|
|
76
|
+
if(tok) printbuf_free(tok->pb);
|
|
77
|
+
free(tok);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static void json_tokener_reset_level(struct json_tokener *tok, int depth)
|
|
81
|
+
{
|
|
82
|
+
tok->stack[depth].state = json_tokener_state_eatws;
|
|
83
|
+
tok->stack[depth].saved_state = json_tokener_state_start;
|
|
84
|
+
json_object_put(tok->stack[depth].current);
|
|
85
|
+
tok->stack[depth].current = NULL;
|
|
86
|
+
free(tok->stack[depth].obj_field_name);
|
|
87
|
+
tok->stack[depth].obj_field_name = NULL;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
void json_tokener_reset(struct json_tokener *tok)
|
|
91
|
+
{
|
|
92
|
+
int i;
|
|
93
|
+
if (!tok)
|
|
94
|
+
return;
|
|
95
|
+
|
|
96
|
+
for(i = tok->depth; i >= 0; i--)
|
|
97
|
+
json_tokener_reset_level(tok, i);
|
|
98
|
+
tok->depth = 0;
|
|
99
|
+
tok->err = json_tokener_success;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
struct json_object* json_tokener_parse(const char *str)
|
|
103
|
+
{
|
|
104
|
+
struct json_tokener* tok;
|
|
105
|
+
struct json_object* obj;
|
|
106
|
+
|
|
107
|
+
tok = json_tokener_new();
|
|
108
|
+
obj = json_tokener_parse_ex(tok, str, -1);
|
|
109
|
+
if(tok->err != json_tokener_success)
|
|
110
|
+
obj = (struct json_object*)error_ptr(-tok->err);
|
|
111
|
+
json_tokener_free(tok);
|
|
112
|
+
return obj;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
#if !HAVE_STRNDUP
|
|
117
|
+
/* CAW: compliant version of strndup() */
|
|
118
|
+
char* strndup(const char* str, size_t n)
|
|
119
|
+
{
|
|
120
|
+
if(str) {
|
|
121
|
+
size_t len = strlen(str);
|
|
122
|
+
size_t nn = json_min(len,n);
|
|
123
|
+
char* s = (char*)malloc(sizeof(char) * (nn + 1));
|
|
124
|
+
|
|
125
|
+
if(s) {
|
|
126
|
+
memcpy(s, str, nn);
|
|
127
|
+
s[nn] = '\0';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return s;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return NULL;
|
|
134
|
+
}
|
|
135
|
+
#endif
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
#define state tok->stack[tok->depth].state
|
|
139
|
+
#define saved_state tok->stack[tok->depth].saved_state
|
|
140
|
+
#define current tok->stack[tok->depth].current
|
|
141
|
+
#define obj_field_name tok->stack[tok->depth].obj_field_name
|
|
142
|
+
|
|
143
|
+
/* Optimization:
|
|
144
|
+
* json_tokener_parse_ex() consumed a lot of CPU in its main loop,
|
|
145
|
+
* iterating character-by character. A large performance boost is
|
|
146
|
+
* achieved by using tighter loops to locally handle units such as
|
|
147
|
+
* comments and strings. Loops that handle an entire token within
|
|
148
|
+
* their scope also gather entire strings and pass them to
|
|
149
|
+
* printbuf_memappend() in a single call, rather than calling
|
|
150
|
+
* printbuf_memappend() one char at a time.
|
|
151
|
+
*
|
|
152
|
+
* POP_CHAR() and ADVANCE_CHAR() macros are used for code that is
|
|
153
|
+
* common to both the main loop and the tighter loops.
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
/* POP_CHAR(dest, tok) macro:
|
|
157
|
+
* Not really a pop()...peeks at the current char and stores it in dest.
|
|
158
|
+
* Returns 1 on success, sets tok->err and returns 0 if no more chars.
|
|
159
|
+
* Implicit inputs: str, len vars
|
|
160
|
+
*/
|
|
161
|
+
#define POP_CHAR(dest, tok) \
|
|
162
|
+
(((tok)->char_offset == len) ? \
|
|
163
|
+
(((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_finish) ? \
|
|
164
|
+
(((tok)->err = json_tokener_success), 0) \
|
|
165
|
+
: \
|
|
166
|
+
(((tok)->err = json_tokener_continue), 0) \
|
|
167
|
+
) : \
|
|
168
|
+
(((dest) = *str), 1) \
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
/* ADVANCE_CHAR() macro:
|
|
172
|
+
* Incrementes str & tok->char_offset.
|
|
173
|
+
* For convenience of existing conditionals, returns the old value of c (0 on eof)
|
|
174
|
+
* Implicit inputs: c var
|
|
175
|
+
*/
|
|
176
|
+
#define ADVANCE_CHAR(str, tok) \
|
|
177
|
+
( ++(str), ((tok)->char_offset)++, c)
|
|
178
|
+
|
|
179
|
+
/* End optimization macro defs */
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
|
|
183
|
+
const char *str, int len)
|
|
184
|
+
{
|
|
185
|
+
struct json_object *obj = NULL;
|
|
186
|
+
char c = '\1';
|
|
187
|
+
|
|
188
|
+
tok->char_offset = 0;
|
|
189
|
+
tok->err = json_tokener_success;
|
|
190
|
+
|
|
191
|
+
while (POP_CHAR(c, tok)) {
|
|
192
|
+
|
|
193
|
+
redo_char:
|
|
194
|
+
switch(state) {
|
|
195
|
+
|
|
196
|
+
case json_tokener_state_eatws:
|
|
197
|
+
/* Advance until we change state */
|
|
198
|
+
while (isspace(c)) {
|
|
199
|
+
if ((!ADVANCE_CHAR(str, tok)) || (!POP_CHAR(c, tok)))
|
|
200
|
+
goto out;
|
|
201
|
+
}
|
|
202
|
+
if(c == '/') {
|
|
203
|
+
printbuf_reset(tok->pb);
|
|
204
|
+
printbuf_memappend_fast(tok->pb, &c, 1);
|
|
205
|
+
state = json_tokener_state_comment_start;
|
|
206
|
+
} else {
|
|
207
|
+
state = saved_state;
|
|
208
|
+
goto redo_char;
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
|
|
212
|
+
case json_tokener_state_start:
|
|
213
|
+
switch(c) {
|
|
214
|
+
case '{':
|
|
215
|
+
state = json_tokener_state_eatws;
|
|
216
|
+
saved_state = json_tokener_state_object_field_start;
|
|
217
|
+
current = json_object_new_object();
|
|
218
|
+
break;
|
|
219
|
+
case '[':
|
|
220
|
+
state = json_tokener_state_eatws;
|
|
221
|
+
saved_state = json_tokener_state_array;
|
|
222
|
+
current = json_object_new_array();
|
|
223
|
+
break;
|
|
224
|
+
case 'N':
|
|
225
|
+
case 'n':
|
|
226
|
+
state = json_tokener_state_null;
|
|
227
|
+
printbuf_reset(tok->pb);
|
|
228
|
+
tok->st_pos = 0;
|
|
229
|
+
goto redo_char;
|
|
230
|
+
case '"':
|
|
231
|
+
case '\'':
|
|
232
|
+
state = json_tokener_state_string;
|
|
233
|
+
printbuf_reset(tok->pb);
|
|
234
|
+
tok->quote_char = c;
|
|
235
|
+
break;
|
|
236
|
+
case 'T':
|
|
237
|
+
case 't':
|
|
238
|
+
case 'F':
|
|
239
|
+
case 'f':
|
|
240
|
+
state = json_tokener_state_boolean;
|
|
241
|
+
printbuf_reset(tok->pb);
|
|
242
|
+
tok->st_pos = 0;
|
|
243
|
+
goto redo_char;
|
|
244
|
+
#if defined(__GNUC__)
|
|
245
|
+
case '0' ... '9':
|
|
246
|
+
#else
|
|
247
|
+
case '0':
|
|
248
|
+
case '1':
|
|
249
|
+
case '2':
|
|
250
|
+
case '3':
|
|
251
|
+
case '4':
|
|
252
|
+
case '5':
|
|
253
|
+
case '6':
|
|
254
|
+
case '7':
|
|
255
|
+
case '8':
|
|
256
|
+
case '9':
|
|
257
|
+
#endif
|
|
258
|
+
case '-':
|
|
259
|
+
state = json_tokener_state_number;
|
|
260
|
+
printbuf_reset(tok->pb);
|
|
261
|
+
tok->is_double = 0;
|
|
262
|
+
goto redo_char;
|
|
263
|
+
default:
|
|
264
|
+
tok->err = json_tokener_error_parse_unexpected;
|
|
265
|
+
goto out;
|
|
266
|
+
}
|
|
267
|
+
break;
|
|
268
|
+
|
|
269
|
+
case json_tokener_state_finish:
|
|
270
|
+
if(tok->depth == 0) goto out;
|
|
271
|
+
obj = json_object_get(current);
|
|
272
|
+
json_tokener_reset_level(tok, tok->depth);
|
|
273
|
+
tok->depth--;
|
|
274
|
+
goto redo_char;
|
|
275
|
+
|
|
276
|
+
case json_tokener_state_null:
|
|
277
|
+
printbuf_memappend_fast(tok->pb, &c, 1);
|
|
278
|
+
if(strncasecmp(json_null_str, tok->pb->buf,
|
|
279
|
+
json_min(tok->st_pos+1, strlen(json_null_str))) == 0) {
|
|
280
|
+
if(tok->st_pos == strlen(json_null_str)) {
|
|
281
|
+
current = NULL;
|
|
282
|
+
saved_state = json_tokener_state_finish;
|
|
283
|
+
state = json_tokener_state_eatws;
|
|
284
|
+
goto redo_char;
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
tok->err = json_tokener_error_parse_null;
|
|
288
|
+
goto out;
|
|
289
|
+
}
|
|
290
|
+
tok->st_pos++;
|
|
291
|
+
break;
|
|
292
|
+
|
|
293
|
+
case json_tokener_state_comment_start:
|
|
294
|
+
if(c == '*') {
|
|
295
|
+
state = json_tokener_state_comment;
|
|
296
|
+
} else if(c == '/') {
|
|
297
|
+
state = json_tokener_state_comment_eol;
|
|
298
|
+
} else {
|
|
299
|
+
tok->err = json_tokener_error_parse_comment;
|
|
300
|
+
goto out;
|
|
301
|
+
}
|
|
302
|
+
printbuf_memappend_fast(tok->pb, &c, 1);
|
|
303
|
+
break;
|
|
304
|
+
|
|
305
|
+
case json_tokener_state_comment:
|
|
306
|
+
{
|
|
307
|
+
/* Advance until we change state */
|
|
308
|
+
const char *case_start = str;
|
|
309
|
+
while(c != '*') {
|
|
310
|
+
if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) {
|
|
311
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
312
|
+
goto out;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
printbuf_memappend_fast(tok->pb, case_start, 1+str-case_start);
|
|
316
|
+
state = json_tokener_state_comment_end;
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
|
|
320
|
+
case json_tokener_state_comment_eol:
|
|
321
|
+
{
|
|
322
|
+
/* Advance until we change state */
|
|
323
|
+
const char *case_start = str;
|
|
324
|
+
while(c != '\n') {
|
|
325
|
+
if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) {
|
|
326
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
327
|
+
goto out;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
331
|
+
MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
|
|
332
|
+
state = json_tokener_state_eatws;
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
|
|
336
|
+
case json_tokener_state_comment_end:
|
|
337
|
+
printbuf_memappend_fast(tok->pb, &c, 1);
|
|
338
|
+
if(c == '/') {
|
|
339
|
+
MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
|
|
340
|
+
state = json_tokener_state_eatws;
|
|
341
|
+
} else {
|
|
342
|
+
state = json_tokener_state_comment;
|
|
343
|
+
}
|
|
344
|
+
break;
|
|
345
|
+
|
|
346
|
+
case json_tokener_state_string:
|
|
347
|
+
{
|
|
348
|
+
/* Advance until we change state */
|
|
349
|
+
const char *case_start = str;
|
|
350
|
+
while(1) {
|
|
351
|
+
if(c == tok->quote_char) {
|
|
352
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
353
|
+
current = json_object_new_string(tok->pb->buf);
|
|
354
|
+
saved_state = json_tokener_state_finish;
|
|
355
|
+
state = json_tokener_state_eatws;
|
|
356
|
+
break;
|
|
357
|
+
} else if(c == '\\') {
|
|
358
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
359
|
+
saved_state = json_tokener_state_string;
|
|
360
|
+
state = json_tokener_state_string_escape;
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) {
|
|
364
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
365
|
+
goto out;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
break;
|
|
370
|
+
|
|
371
|
+
case json_tokener_state_string_escape:
|
|
372
|
+
switch(c) {
|
|
373
|
+
case '"':
|
|
374
|
+
case '\\':
|
|
375
|
+
case '/':
|
|
376
|
+
printbuf_memappend_fast(tok->pb, &c, 1);
|
|
377
|
+
state = saved_state;
|
|
378
|
+
break;
|
|
379
|
+
case 'b':
|
|
380
|
+
case 'n':
|
|
381
|
+
case 'r':
|
|
382
|
+
case 't':
|
|
383
|
+
if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1);
|
|
384
|
+
else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1);
|
|
385
|
+
else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1);
|
|
386
|
+
else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1);
|
|
387
|
+
state = saved_state;
|
|
388
|
+
break;
|
|
389
|
+
case 'u':
|
|
390
|
+
tok->ucs_char = 0;
|
|
391
|
+
tok->st_pos = 0;
|
|
392
|
+
state = json_tokener_state_escape_unicode;
|
|
393
|
+
break;
|
|
394
|
+
default:
|
|
395
|
+
tok->err = json_tokener_error_parse_string;
|
|
396
|
+
goto out;
|
|
397
|
+
}
|
|
398
|
+
break;
|
|
399
|
+
|
|
400
|
+
case json_tokener_state_escape_unicode:
|
|
401
|
+
/* Note that the following code is inefficient for handling large
|
|
402
|
+
* chunks of extended chars, calling printbuf_memappend() once
|
|
403
|
+
* for each multi-byte character of input.
|
|
404
|
+
* This is a good area for future optimization.
|
|
405
|
+
*/
|
|
406
|
+
{
|
|
407
|
+
/* Advance until we change state */
|
|
408
|
+
while(1) {
|
|
409
|
+
if(strchr(json_hex_chars, c)) {
|
|
410
|
+
tok->ucs_char += ((unsigned int)hexdigit(c) << ((3-tok->st_pos++)*4));
|
|
411
|
+
if(tok->st_pos == 4) {
|
|
412
|
+
unsigned char utf_out[3];
|
|
413
|
+
if (tok->ucs_char < 0x80) {
|
|
414
|
+
utf_out[0] = tok->ucs_char;
|
|
415
|
+
printbuf_memappend_fast(tok->pb, (char*)utf_out, 1);
|
|
416
|
+
} else if (tok->ucs_char < 0x800) {
|
|
417
|
+
utf_out[0] = 0xc0 | (tok->ucs_char >> 6);
|
|
418
|
+
utf_out[1] = 0x80 | (tok->ucs_char & 0x3f);
|
|
419
|
+
printbuf_memappend_fast(tok->pb, (char*)utf_out, 2);
|
|
420
|
+
} else {
|
|
421
|
+
utf_out[0] = 0xe0 | (tok->ucs_char >> 12);
|
|
422
|
+
utf_out[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
|
|
423
|
+
utf_out[2] = 0x80 | (tok->ucs_char & 0x3f);
|
|
424
|
+
printbuf_memappend_fast(tok->pb, (char*)utf_out, 3);
|
|
425
|
+
}
|
|
426
|
+
state = saved_state;
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
} else {
|
|
430
|
+
tok->err = json_tokener_error_parse_string;
|
|
431
|
+
goto out;
|
|
432
|
+
}
|
|
433
|
+
if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok))
|
|
434
|
+
goto out;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
|
|
439
|
+
case json_tokener_state_boolean:
|
|
440
|
+
printbuf_memappend_fast(tok->pb, &c, 1);
|
|
441
|
+
if(strncasecmp(json_true_str, tok->pb->buf,
|
|
442
|
+
json_min(tok->st_pos+1, strlen(json_true_str))) == 0) {
|
|
443
|
+
if(tok->st_pos == strlen(json_true_str)) {
|
|
444
|
+
current = json_object_new_boolean(1);
|
|
445
|
+
saved_state = json_tokener_state_finish;
|
|
446
|
+
state = json_tokener_state_eatws;
|
|
447
|
+
goto redo_char;
|
|
448
|
+
}
|
|
449
|
+
} else if(strncasecmp(json_false_str, tok->pb->buf,
|
|
450
|
+
json_min(tok->st_pos+1, strlen(json_false_str))) == 0) {
|
|
451
|
+
if(tok->st_pos == strlen(json_false_str)) {
|
|
452
|
+
current = json_object_new_boolean(0);
|
|
453
|
+
saved_state = json_tokener_state_finish;
|
|
454
|
+
state = json_tokener_state_eatws;
|
|
455
|
+
goto redo_char;
|
|
456
|
+
}
|
|
457
|
+
} else {
|
|
458
|
+
tok->err = json_tokener_error_parse_boolean;
|
|
459
|
+
goto out;
|
|
460
|
+
}
|
|
461
|
+
tok->st_pos++;
|
|
462
|
+
break;
|
|
463
|
+
|
|
464
|
+
case json_tokener_state_number:
|
|
465
|
+
{
|
|
466
|
+
/* Advance until we change state */
|
|
467
|
+
const char *case_start = str;
|
|
468
|
+
int case_len=0;
|
|
469
|
+
while(c && strchr(json_number_chars, c)) {
|
|
470
|
+
++case_len;
|
|
471
|
+
if(c == '.' || c == 'e') tok->is_double = 1;
|
|
472
|
+
if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) {
|
|
473
|
+
printbuf_memappend_fast(tok->pb, case_start, case_len);
|
|
474
|
+
goto out;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (case_len>0)
|
|
478
|
+
printbuf_memappend_fast(tok->pb, case_start, case_len);
|
|
479
|
+
}
|
|
480
|
+
{
|
|
481
|
+
int numi;
|
|
482
|
+
double numd;
|
|
483
|
+
if(!tok->is_double && sscanf(tok->pb->buf, "%d", &numi) == 1) {
|
|
484
|
+
current = json_object_new_int(numi);
|
|
485
|
+
} else if(tok->is_double && sscanf(tok->pb->buf, "%lf", &numd) == 1) {
|
|
486
|
+
current = json_object_new_double(numd);
|
|
487
|
+
} else {
|
|
488
|
+
tok->err = json_tokener_error_parse_number;
|
|
489
|
+
goto out;
|
|
490
|
+
}
|
|
491
|
+
saved_state = json_tokener_state_finish;
|
|
492
|
+
state = json_tokener_state_eatws;
|
|
493
|
+
goto redo_char;
|
|
494
|
+
}
|
|
495
|
+
break;
|
|
496
|
+
|
|
497
|
+
case json_tokener_state_array:
|
|
498
|
+
if(c == ']') {
|
|
499
|
+
saved_state = json_tokener_state_finish;
|
|
500
|
+
state = json_tokener_state_eatws;
|
|
501
|
+
} else {
|
|
502
|
+
if(tok->depth >= JSON_TOKENER_MAX_DEPTH-1) {
|
|
503
|
+
tok->err = json_tokener_error_depth;
|
|
504
|
+
goto out;
|
|
505
|
+
}
|
|
506
|
+
state = json_tokener_state_array_add;
|
|
507
|
+
tok->depth++;
|
|
508
|
+
json_tokener_reset_level(tok, tok->depth);
|
|
509
|
+
goto redo_char;
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
|
|
513
|
+
case json_tokener_state_array_add:
|
|
514
|
+
json_object_array_add(current, obj);
|
|
515
|
+
saved_state = json_tokener_state_array_sep;
|
|
516
|
+
state = json_tokener_state_eatws;
|
|
517
|
+
goto redo_char;
|
|
518
|
+
|
|
519
|
+
case json_tokener_state_array_sep:
|
|
520
|
+
if(c == ']') {
|
|
521
|
+
saved_state = json_tokener_state_finish;
|
|
522
|
+
state = json_tokener_state_eatws;
|
|
523
|
+
} else if(c == ',') {
|
|
524
|
+
saved_state = json_tokener_state_array;
|
|
525
|
+
state = json_tokener_state_eatws;
|
|
526
|
+
} else {
|
|
527
|
+
tok->err = json_tokener_error_parse_array;
|
|
528
|
+
goto out;
|
|
529
|
+
}
|
|
530
|
+
break;
|
|
531
|
+
|
|
532
|
+
case json_tokener_state_object_field_start:
|
|
533
|
+
if(c == '}') {
|
|
534
|
+
saved_state = json_tokener_state_finish;
|
|
535
|
+
state = json_tokener_state_eatws;
|
|
536
|
+
} else if (c == '"' || c == '\'') {
|
|
537
|
+
tok->quote_char = c;
|
|
538
|
+
printbuf_reset(tok->pb);
|
|
539
|
+
state = json_tokener_state_object_field;
|
|
540
|
+
} else {
|
|
541
|
+
tok->err = json_tokener_error_parse_object_key_name;
|
|
542
|
+
goto out;
|
|
543
|
+
}
|
|
544
|
+
break;
|
|
545
|
+
|
|
546
|
+
case json_tokener_state_object_field:
|
|
547
|
+
{
|
|
548
|
+
/* Advance until we change state */
|
|
549
|
+
const char *case_start = str;
|
|
550
|
+
while(1) {
|
|
551
|
+
if(c == tok->quote_char) {
|
|
552
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
553
|
+
obj_field_name = strdup(tok->pb->buf);
|
|
554
|
+
saved_state = json_tokener_state_object_field_end;
|
|
555
|
+
state = json_tokener_state_eatws;
|
|
556
|
+
break;
|
|
557
|
+
} else if(c == '\\') {
|
|
558
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
559
|
+
saved_state = json_tokener_state_object_field;
|
|
560
|
+
state = json_tokener_state_string_escape;
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
if (!ADVANCE_CHAR(str, tok) || !POP_CHAR(c, tok)) {
|
|
564
|
+
printbuf_memappend_fast(tok->pb, case_start, str-case_start);
|
|
565
|
+
goto out;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
break;
|
|
570
|
+
|
|
571
|
+
case json_tokener_state_object_field_end:
|
|
572
|
+
if(c == ':') {
|
|
573
|
+
saved_state = json_tokener_state_object_value;
|
|
574
|
+
state = json_tokener_state_eatws;
|
|
575
|
+
} else {
|
|
576
|
+
tok->err = json_tokener_error_parse_object_key_sep;
|
|
577
|
+
goto out;
|
|
578
|
+
}
|
|
579
|
+
break;
|
|
580
|
+
|
|
581
|
+
case json_tokener_state_object_value:
|
|
582
|
+
if(tok->depth >= JSON_TOKENER_MAX_DEPTH-1) {
|
|
583
|
+
tok->err = json_tokener_error_depth;
|
|
584
|
+
goto out;
|
|
585
|
+
}
|
|
586
|
+
state = json_tokener_state_object_value_add;
|
|
587
|
+
tok->depth++;
|
|
588
|
+
json_tokener_reset_level(tok, tok->depth);
|
|
589
|
+
goto redo_char;
|
|
590
|
+
|
|
591
|
+
case json_tokener_state_object_value_add:
|
|
592
|
+
json_object_object_add(current, obj_field_name, obj);
|
|
593
|
+
free(obj_field_name);
|
|
594
|
+
obj_field_name = NULL;
|
|
595
|
+
saved_state = json_tokener_state_object_sep;
|
|
596
|
+
state = json_tokener_state_eatws;
|
|
597
|
+
goto redo_char;
|
|
598
|
+
|
|
599
|
+
case json_tokener_state_object_sep:
|
|
600
|
+
if(c == '}') {
|
|
601
|
+
saved_state = json_tokener_state_finish;
|
|
602
|
+
state = json_tokener_state_eatws;
|
|
603
|
+
} else if(c == ',') {
|
|
604
|
+
saved_state = json_tokener_state_object_field_start;
|
|
605
|
+
state = json_tokener_state_eatws;
|
|
606
|
+
} else {
|
|
607
|
+
tok->err = json_tokener_error_parse_object_value_sep;
|
|
608
|
+
goto out;
|
|
609
|
+
}
|
|
610
|
+
break;
|
|
611
|
+
|
|
612
|
+
}
|
|
613
|
+
if (!ADVANCE_CHAR(str, tok))
|
|
614
|
+
goto out;
|
|
615
|
+
} /* while(POP_CHAR) */
|
|
616
|
+
|
|
617
|
+
out:
|
|
618
|
+
if (!c) { /* We hit an eof char (0) */
|
|
619
|
+
if(state != json_tokener_state_finish &&
|
|
620
|
+
saved_state != json_tokener_state_finish)
|
|
621
|
+
tok->err = json_tokener_error_parse_eof;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
if(tok->err == json_tokener_success) return json_object_get(current);
|
|
625
|
+
MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n",
|
|
626
|
+
json_tokener_errors[tok->err], tok->char_offset);
|
|
627
|
+
return NULL;
|
|
628
|
+
}
|