ffi-yajl 0.0.1-universal-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +17 -0
- data/Rakefile +62 -0
- data/bin/ffi-yajl-bench +36 -0
- data/ext/ffi_yajl/ext/encoder/encoder.c +240 -0
- data/ext/ffi_yajl/ext/encoder/extconf.rb +29 -0
- data/ext/ffi_yajl/ext/parser/extconf.rb +29 -0
- data/ext/ffi_yajl/ext/parser/parser.c +199 -0
- data/ext/libyajl2/extconf.rb +65 -0
- data/ext/libyajl2/vendored/.gitignore +3 -0
- data/ext/libyajl2/vendored/BUILDING +23 -0
- data/ext/libyajl2/vendored/BUILDING.win32 +27 -0
- data/ext/libyajl2/vendored/CMakeLists.txt +79 -0
- data/ext/libyajl2/vendored/COPYING +13 -0
- data/ext/libyajl2/vendored/ChangeLog +175 -0
- data/ext/libyajl2/vendored/README +74 -0
- data/ext/libyajl2/vendored/TODO +9 -0
- data/ext/libyajl2/vendored/YAJLDoc.cmake +26 -0
- data/ext/libyajl2/vendored/configure +79 -0
- data/ext/libyajl2/vendored/example/CMakeLists.txt +23 -0
- data/ext/libyajl2/vendored/example/README.md +7 -0
- data/ext/libyajl2/vendored/example/parse_config.c +69 -0
- data/ext/libyajl2/vendored/example/sample.config +101 -0
- data/ext/libyajl2/vendored/perf/CMakeLists.txt +23 -0
- data/ext/libyajl2/vendored/perf/documents.c +1418 -0
- data/ext/libyajl2/vendored/perf/documents.h +28 -0
- data/ext/libyajl2/vendored/perf/perftest.c +134 -0
- data/ext/libyajl2/vendored/reformatter/CMakeLists.txt +39 -0
- data/ext/libyajl2/vendored/reformatter/json_reformat.c +194 -0
- data/ext/libyajl2/vendored/src/CMakeLists.txt +86 -0
- data/ext/libyajl2/vendored/src/YAJL.dxy +1258 -0
- data/ext/libyajl2/vendored/src/api/yajl_common.h +75 -0
- data/ext/libyajl2/vendored/src/api/yajl_gen.h +157 -0
- data/ext/libyajl2/vendored/src/api/yajl_parse.h +226 -0
- data/ext/libyajl2/vendored/src/api/yajl_tree.h +185 -0
- data/ext/libyajl2/vendored/src/api/yajl_version.h.cmake +23 -0
- data/ext/libyajl2/vendored/src/yajl +33 -0
- data/ext/libyajl2/vendored/src/yajl.c +175 -0
- data/ext/libyajl2/vendored/src/yajl.pc.cmake +9 -0
- data/ext/libyajl2/vendored/src/yajl_alloc.c +52 -0
- data/ext/libyajl2/vendored/src/yajl_alloc.h +34 -0
- data/ext/libyajl2/vendored/src/yajl_buf.c +103 -0
- data/ext/libyajl2/vendored/src/yajl_buf.h +57 -0
- data/ext/libyajl2/vendored/src/yajl_bytestack.h +69 -0
- data/ext/libyajl2/vendored/src/yajl_encode.c +220 -0
- data/ext/libyajl2/vendored/src/yajl_encode.h +34 -0
- data/ext/libyajl2/vendored/src/yajl_gen.c +354 -0
- data/ext/libyajl2/vendored/src/yajl_lex.c +763 -0
- data/ext/libyajl2/vendored/src/yajl_lex.h +117 -0
- data/ext/libyajl2/vendored/src/yajl_parser.c +498 -0
- data/ext/libyajl2/vendored/src/yajl_parser.h +78 -0
- data/ext/libyajl2/vendored/src/yajl_tree.c +503 -0
- data/ext/libyajl2/vendored/src/yajl_version.c +7 -0
- data/ext/libyajl2/vendored/test/CMakeLists.txt +23 -0
- data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json.gold +36 -0
- data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json +11 -0
- data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/am_eof.json +1 -0
- data/ext/libyajl2/vendored/test/cases/am_eof.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/am_integers.json +1 -0
- data/ext/libyajl2/vendored/test/cases/am_integers.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/am_multiple.json +3 -0
- data/ext/libyajl2/vendored/test/cases/am_multiple.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/am_stuff.json +7 -0
- data/ext/libyajl2/vendored/test/cases/am_stuff.json.gold +14 -0
- data/ext/libyajl2/vendored/test/cases/ap_array_open.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_array_open.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ap_eof_str.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_eof_str.json.gold +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_map_open.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_map_open.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json +1 -0
- data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/array.json +6 -0
- data/ext/libyajl2/vendored/test/cases/array.json.gold +22 -0
- data/ext/libyajl2/vendored/test/cases/array_close.json +1 -0
- data/ext/libyajl2/vendored/test/cases/array_close.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/bignums.json +1 -0
- data/ext/libyajl2/vendored/test/cases/bignums.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/bogus_char.json +4 -0
- data/ext/libyajl2/vendored/test/cases/bogus_char.json.gold +10 -0
- data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json +1 -0
- data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/deep_arrays.json +1 -0
- data/ext/libyajl2/vendored/test/cases/deep_arrays.json.gold +2049 -0
- data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json +1 -0
- data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json.gold +36 -0
- data/ext/libyajl2/vendored/test/cases/doubles.json +1 -0
- data/ext/libyajl2/vendored/test/cases/doubles.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/doubles_in_array.json +1 -0
- data/ext/libyajl2/vendored/test/cases/doubles_in_array.json.gold +8 -0
- data/ext/libyajl2/vendored/test/cases/empty_array.json +1 -0
- data/ext/libyajl2/vendored/test/cases/empty_array.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/empty_string.json +1 -0
- data/ext/libyajl2/vendored/test/cases/empty_string.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json +4 -0
- data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/escaped_foobar.json +1 -0
- data/ext/libyajl2/vendored/test/cases/escaped_foobar.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/false.json +1 -0
- data/ext/libyajl2/vendored/test/cases/false.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/fg_issue_7.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_issue_7.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json +1 -0
- data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json +2 -0
- data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/high_overflow.json +1 -0
- data/ext/libyajl2/vendored/test/cases/high_overflow.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/integers.json +3 -0
- data/ext/libyajl2/vendored/test/cases/integers.json.gold +14 -0
- data/ext/libyajl2/vendored/test/cases/invalid_utf8.json +1 -0
- data/ext/libyajl2/vendored/test/cases/invalid_utf8.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json +1 -0
- data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json +1 -0
- data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json +7 -0
- data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/lonely_number.json +1 -0
- data/ext/libyajl2/vendored/test/cases/lonely_number.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/low_overflow.json +1 -0
- data/ext/libyajl2/vendored/test/cases/low_overflow.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/map_close.json +1 -0
- data/ext/libyajl2/vendored/test/cases/map_close.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json +1 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json +1 -0
- data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/multiple.json +3 -0
- data/ext/libyajl2/vendored/test/cases/multiple.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json +1 -0
- data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json.gold +8 -0
- data/ext/libyajl2/vendored/test/cases/np_partial_bad.json +1 -0
- data/ext/libyajl2/vendored/test/cases/np_partial_bad.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/null.json +1 -0
- data/ext/libyajl2/vendored/test/cases/null.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json +5 -0
- data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/simple.json +5 -0
- data/ext/libyajl2/vendored/test/cases/simple.json.gold +9 -0
- data/ext/libyajl2/vendored/test/cases/simple_with_comments.json +11 -0
- data/ext/libyajl2/vendored/test/cases/simple_with_comments.json.gold +5 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json +1 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json.gold +3 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json +1 -0
- data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/string_with_escapes.json +3 -0
- data/ext/libyajl2/vendored/test/cases/string_with_escapes.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json +2 -0
- data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json +1 -0
- data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json.gold +7 -0
- data/ext/libyajl2/vendored/test/cases/true.json +1 -0
- data/ext/libyajl2/vendored/test/cases/true.json.gold +2 -0
- data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json +1 -0
- data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json.gold +4 -0
- data/ext/libyajl2/vendored/test/cases/zerobyte.json +1 -0
- data/ext/libyajl2/vendored/test/cases/zerobyte.json.gold +0 -0
- data/ext/libyajl2/vendored/test/run_tests.sh +94 -0
- data/ext/libyajl2/vendored/test/yajl_test.c +281 -0
- data/ext/libyajl2/vendored/verify/CMakeLists.txt +39 -0
- data/ext/libyajl2/vendored/verify/json_verify.c +116 -0
- data/lib/ffi_yajl.rb +14 -0
- data/lib/ffi_yajl/benchmark.rb +7 -0
- data/lib/ffi_yajl/benchmark/MIT-LICENSE +20 -0
- data/lib/ffi_yajl/benchmark/encode.rb +135 -0
- data/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb +42 -0
- data/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb +53 -0
- data/lib/ffi_yajl/benchmark/encode_profile.rb +38 -0
- data/lib/ffi_yajl/benchmark/http.rb +32 -0
- data/lib/ffi_yajl/benchmark/parse.rb +133 -0
- data/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +50 -0
- data/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +55 -0
- data/lib/ffi_yajl/benchmark/parse_profile.rb +37 -0
- data/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +39 -0
- data/lib/ffi_yajl/benchmark/parse_stream.rb +54 -0
- data/lib/ffi_yajl/benchmark/subjects/item.json +1 -0
- data/lib/ffi_yajl/benchmark/subjects/ohai.json +1216 -0
- data/lib/ffi_yajl/benchmark/subjects/ohai.marshal_dump +0 -0
- data/lib/ffi_yajl/benchmark/subjects/ohai.yml +975 -0
- data/lib/ffi_yajl/benchmark/subjects/twitter_search.json +1 -0
- data/lib/ffi_yajl/benchmark/subjects/twitter_stream.json +430 -0
- data/lib/ffi_yajl/benchmark/subjects/unicode.json +1 -0
- data/lib/ffi_yajl/encoder.rb +53 -0
- data/lib/ffi_yajl/ext.rb +22 -0
- data/lib/ffi_yajl/ext/.keep +0 -0
- data/lib/ffi_yajl/ffi.rb +129 -0
- data/lib/ffi_yajl/ffi/encoder.rb +175 -0
- data/lib/ffi_yajl/ffi/parser.rb +145 -0
- data/lib/ffi_yajl/json_gem.rb +121 -0
- data/lib/ffi_yajl/parser.rb +23 -0
- data/lib/ffi_yajl/version.rb +3 -0
- data/lib/libyajl.so +0 -0
- data/lib/libyajl.so.2 +0 -0
- data/lib/libyajl.so.2.0.5 +0 -0
- data/lib/libyajl_s.a +0 -0
- data/spec/ffi_yajl/encoder_spec.rb +39 -0
- data/spec/ffi_yajl/json_gem_spec.rb +355 -0
- data/spec/ffi_yajl/parser_spec.rb +78 -0
- data/spec/spec_helper.rb +14 -0
- metadata +330 -0
@@ -0,0 +1,185 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2010-2011 Florian Forster <ff at octo.it>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/**
|
18
|
+
* \file yajl_tree.h
|
19
|
+
*
|
20
|
+
* Parses JSON data and returns the data in tree form.
|
21
|
+
*
|
22
|
+
* \author Florian Forster
|
23
|
+
* \date August 2010
|
24
|
+
*
|
25
|
+
* This interface makes quick parsing and extraction of
|
26
|
+
* smallish JSON docs trivial:
|
27
|
+
*
|
28
|
+
* \include example/parse_config.c
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef YAJL_TREE_H
|
32
|
+
#define YAJL_TREE_H 1
|
33
|
+
|
34
|
+
#include <yajl/yajl_common.h>
|
35
|
+
|
36
|
+
#ifdef __cplusplus
|
37
|
+
extern "C" {
|
38
|
+
#endif
|
39
|
+
|
40
|
+
/** possible data types that a yajl_val_s can hold */
|
41
|
+
typedef enum {
|
42
|
+
yajl_t_string = 1,
|
43
|
+
yajl_t_number = 2,
|
44
|
+
yajl_t_object = 3,
|
45
|
+
yajl_t_array = 4,
|
46
|
+
yajl_t_true = 5,
|
47
|
+
yajl_t_false = 6,
|
48
|
+
yajl_t_null = 7,
|
49
|
+
/** The any type isn't valid for yajl_val_s.type, but can be
|
50
|
+
* used as an argument to routines like yajl_tree_get().
|
51
|
+
*/
|
52
|
+
yajl_t_any = 8
|
53
|
+
} yajl_type;
|
54
|
+
|
55
|
+
#define YAJL_NUMBER_INT_VALID 0x01
|
56
|
+
#define YAJL_NUMBER_DOUBLE_VALID 0x02
|
57
|
+
|
58
|
+
/** A pointer to a node in the parse tree */
|
59
|
+
typedef struct yajl_val_s * yajl_val;
|
60
|
+
|
61
|
+
/**
|
62
|
+
* A JSON value representation capable of holding one of the seven
|
63
|
+
* types above. For "string", "number", "object", and "array"
|
64
|
+
* additional data is available in the union. The "YAJL_IS_*"
|
65
|
+
* and "YAJL_GET_*" macros below allow type checking and convenient
|
66
|
+
* value extraction.
|
67
|
+
*/
|
68
|
+
struct yajl_val_s
|
69
|
+
{
|
70
|
+
/** Type of the value contained. Use the "YAJL_IS_*" macors to check for a
|
71
|
+
* specific type. */
|
72
|
+
yajl_type type;
|
73
|
+
/** Type-specific data. You may use the "YAJL_GET_*" macros to access these
|
74
|
+
* members. */
|
75
|
+
union
|
76
|
+
{
|
77
|
+
char * string;
|
78
|
+
struct {
|
79
|
+
long long i; /*< integer value, if representable. */
|
80
|
+
double d; /*< double value, if representable. */
|
81
|
+
/** Signals whether the \em i and \em d members are
|
82
|
+
* valid. See \c YAJL_NUMBER_INT_VALID and
|
83
|
+
* \c YAJL_NUMBER_DOUBLE_VALID. */
|
84
|
+
char *r; /*< unparsed number in string form. */
|
85
|
+
unsigned int flags;
|
86
|
+
} number;
|
87
|
+
struct {
|
88
|
+
const char **keys; /*< Array of keys */
|
89
|
+
yajl_val *values; /*< Array of values. */
|
90
|
+
size_t len; /*< Number of key-value-pairs. */
|
91
|
+
} object;
|
92
|
+
struct {
|
93
|
+
yajl_val *values; /*< Array of elements. */
|
94
|
+
size_t len; /*< Number of elements. */
|
95
|
+
} array;
|
96
|
+
} u;
|
97
|
+
};
|
98
|
+
|
99
|
+
/**
|
100
|
+
* Parse a string.
|
101
|
+
*
|
102
|
+
* Parses an null-terminated string containing JSON data and returns a pointer
|
103
|
+
* to the top-level value (root of the parse tree).
|
104
|
+
*
|
105
|
+
* \param input Pointer to a null-terminated utf8 string containing
|
106
|
+
* JSON data.
|
107
|
+
* \param error_buffer Pointer to a buffer in which an error message will
|
108
|
+
* be stored if \em yajl_tree_parse fails, or
|
109
|
+
* \c NULL. The buffer will be initialized before
|
110
|
+
* parsing, so its content will be destroyed even if
|
111
|
+
* \em yajl_tree_parse succeeds.
|
112
|
+
* \param error_buffer_size Size of the memory area pointed to by
|
113
|
+
* \em error_buffer_size. If \em error_buffer_size is
|
114
|
+
* \c NULL, this argument is ignored.
|
115
|
+
*
|
116
|
+
* \returns Pointer to the top-level value or \c NULL on error. The memory
|
117
|
+
* pointed to must be freed using \em yajl_tree_free. In case of an error, a
|
118
|
+
* null terminated message describing the error in more detail is stored in
|
119
|
+
* \em error_buffer if it is not \c NULL.
|
120
|
+
*/
|
121
|
+
YAJL_API yajl_val yajl_tree_parse (const char *input,
|
122
|
+
char *error_buffer, size_t error_buffer_size);
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Free a parse tree returned by "yajl_tree_parse".
|
126
|
+
*
|
127
|
+
* \param v Pointer to a JSON value returned by "yajl_tree_parse". Passing NULL
|
128
|
+
* is valid and results in a no-op.
|
129
|
+
*/
|
130
|
+
YAJL_API void yajl_tree_free (yajl_val v);
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Access a nested value inside a tree.
|
134
|
+
*
|
135
|
+
* \param parent the node under which you'd like to extract values.
|
136
|
+
* \param path A null terminated array of strings, each the name of an object key
|
137
|
+
* \param type the yajl_type of the object you seek, or yajl_t_any if any will do.
|
138
|
+
*
|
139
|
+
* \returns a pointer to the found value, or NULL if we came up empty.
|
140
|
+
*
|
141
|
+
* Future Ideas: it'd be nice to move path to a string and implement support for
|
142
|
+
* a teeny tiny micro language here, so you can extract array elements, do things
|
143
|
+
* like .first and .last, even .length. Inspiration from JSONPath and css selectors?
|
144
|
+
* No it wouldn't be fast, but that's not what this API is about.
|
145
|
+
*/
|
146
|
+
YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);
|
147
|
+
|
148
|
+
/* Various convenience macros to check the type of a `yajl_val` */
|
149
|
+
#define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
|
150
|
+
#define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
|
151
|
+
#define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID))
|
152
|
+
#define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))
|
153
|
+
#define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object))
|
154
|
+
#define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == yajl_t_array ))
|
155
|
+
#define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == yajl_t_true ))
|
156
|
+
#define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == yajl_t_false ))
|
157
|
+
#define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == yajl_t_null ))
|
158
|
+
|
159
|
+
/** Given a yajl_val_string return a ptr to the bare string it contains,
|
160
|
+
* or NULL if the value is not a string. */
|
161
|
+
#define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
|
162
|
+
|
163
|
+
/** Get the string representation of a number. You should check type first,
|
164
|
+
* perhaps using YAJL_IS_NUMBER */
|
165
|
+
#define YAJL_GET_NUMBER(v) ((v)->u.number.r)
|
166
|
+
|
167
|
+
/** Get the double representation of a number. You should check type first,
|
168
|
+
* perhaps using YAJL_IS_DOUBLE */
|
169
|
+
#define YAJL_GET_DOUBLE(v) ((v)->u.number.d)
|
170
|
+
|
171
|
+
/** Get the 64bit (long long) integer representation of a number. You should
|
172
|
+
* check type first, perhaps using YAJL_IS_INTEGER */
|
173
|
+
#define YAJL_GET_INTEGER(v) ((v)->u.number.i)
|
174
|
+
|
175
|
+
/** Get a pointer to a yajl_val_object or NULL if the value is not an object. */
|
176
|
+
#define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
|
177
|
+
|
178
|
+
/** Get a pointer to a yajl_val_array or NULL if the value is not an object. */
|
179
|
+
#define YAJL_GET_ARRAY(v) (YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL)
|
180
|
+
|
181
|
+
#ifdef __cplusplus
|
182
|
+
}
|
183
|
+
#endif
|
184
|
+
|
185
|
+
#endif /* YAJL_TREE_H */
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#ifndef YAJL_VERSION_H_
|
2
|
+
#define YAJL_VERSION_H_
|
3
|
+
|
4
|
+
#include <yajl/yajl_common.h>
|
5
|
+
|
6
|
+
#define YAJL_MAJOR ${YAJL_MAJOR}
|
7
|
+
#define YAJL_MINOR ${YAJL_MINOR}
|
8
|
+
#define YAJL_MICRO ${YAJL_MICRO}
|
9
|
+
|
10
|
+
#define YAJL_VERSION ((YAJL_MAJOR * 10000) + (YAJL_MINOR * 100) + YAJL_MICRO)
|
11
|
+
|
12
|
+
#ifdef __cplusplus
|
13
|
+
extern "C" {
|
14
|
+
#endif
|
15
|
+
|
16
|
+
extern int YAJL_API yajl_version(void);
|
17
|
+
|
18
|
+
#ifdef __cplusplus
|
19
|
+
}
|
20
|
+
#endif
|
21
|
+
|
22
|
+
#endif /* YAJL_VERSION_H_ */
|
23
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
/**
|
2
|
+
\example reformatter/json_reformat.c
|
3
|
+
\example example/parse_config.c
|
4
|
+
*/
|
5
|
+
|
6
|
+
/*!
|
7
|
+
\mainpage Yet Another JSON Library (YAJL)
|
8
|
+
\author Lloyd Hilaiel
|
9
|
+
\date 2007-2011
|
10
|
+
|
11
|
+
Yet Another JSON Library (YAJL) is a small event-driven (SAX-style)
|
12
|
+
JSON parser written in ANSI C, and a small validating JSON
|
13
|
+
generator. YAJL is released under the permissive ISC license.
|
14
|
+
|
15
|
+
\section features Features
|
16
|
+
|
17
|
+
-# Stream (incremental) parsing and generation of JSON
|
18
|
+
-# ANSI C
|
19
|
+
-# Human readable error messages with context
|
20
|
+
-# tiny
|
21
|
+
-# event driven
|
22
|
+
-# support for generating "beautified" JSON
|
23
|
+
-# includes
|
24
|
+
It also includes a small simplified tree interface for
|
25
|
+
simplified parsing and extraction of data from smallish JSON documents.
|
26
|
+
|
27
|
+
\section usage Usage
|
28
|
+
|
29
|
+
See <a href="reformatter_2json_reformat_8c-example.html">json_reformat.c</a> for a complete example of stream based parsing
|
30
|
+
and generation of JSON. See <a href="example_2parse_config_8c-example.html">parse_config.c</a> for an example of the
|
31
|
+
simplified tree interface.
|
32
|
+
|
33
|
+
*/
|
@@ -0,0 +1,175 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include "api/yajl_parse.h"
|
18
|
+
#include "yajl_lex.h"
|
19
|
+
#include "yajl_parser.h"
|
20
|
+
#include "yajl_alloc.h"
|
21
|
+
|
22
|
+
#include <stdlib.h>
|
23
|
+
#include <string.h>
|
24
|
+
#include <stdarg.h>
|
25
|
+
#include <assert.h>
|
26
|
+
|
27
|
+
const char *
|
28
|
+
yajl_status_to_string(yajl_status stat)
|
29
|
+
{
|
30
|
+
const char * statStr = "unknown";
|
31
|
+
switch (stat) {
|
32
|
+
case yajl_status_ok:
|
33
|
+
statStr = "ok, no error";
|
34
|
+
break;
|
35
|
+
case yajl_status_client_canceled:
|
36
|
+
statStr = "client canceled parse";
|
37
|
+
break;
|
38
|
+
case yajl_status_error:
|
39
|
+
statStr = "parse error";
|
40
|
+
break;
|
41
|
+
}
|
42
|
+
return statStr;
|
43
|
+
}
|
44
|
+
|
45
|
+
yajl_handle
|
46
|
+
yajl_alloc(const yajl_callbacks * callbacks,
|
47
|
+
yajl_alloc_funcs * afs,
|
48
|
+
void * ctx)
|
49
|
+
{
|
50
|
+
yajl_handle hand = NULL;
|
51
|
+
yajl_alloc_funcs afsBuffer;
|
52
|
+
|
53
|
+
/* first order of business is to set up memory allocation routines */
|
54
|
+
if (afs != NULL) {
|
55
|
+
if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
|
56
|
+
{
|
57
|
+
return NULL;
|
58
|
+
}
|
59
|
+
} else {
|
60
|
+
yajl_set_default_alloc_funcs(&afsBuffer);
|
61
|
+
afs = &afsBuffer;
|
62
|
+
}
|
63
|
+
|
64
|
+
hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
|
65
|
+
|
66
|
+
/* copy in pointers to allocation routines */
|
67
|
+
memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
|
68
|
+
|
69
|
+
hand->callbacks = callbacks;
|
70
|
+
hand->ctx = ctx;
|
71
|
+
hand->lexer = NULL;
|
72
|
+
hand->bytesConsumed = 0;
|
73
|
+
hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
|
74
|
+
hand->flags = 0;
|
75
|
+
yajl_bs_init(hand->stateStack, &(hand->alloc));
|
76
|
+
yajl_bs_push(hand->stateStack, yajl_state_start);
|
77
|
+
|
78
|
+
return hand;
|
79
|
+
}
|
80
|
+
|
81
|
+
int
|
82
|
+
yajl_config(yajl_handle h, yajl_option opt, ...)
|
83
|
+
{
|
84
|
+
int rv = 1;
|
85
|
+
va_list ap;
|
86
|
+
va_start(ap, opt);
|
87
|
+
|
88
|
+
switch(opt) {
|
89
|
+
case yajl_allow_comments:
|
90
|
+
case yajl_dont_validate_strings:
|
91
|
+
case yajl_allow_trailing_garbage:
|
92
|
+
case yajl_allow_multiple_values:
|
93
|
+
case yajl_allow_partial_values:
|
94
|
+
if (va_arg(ap, int)) h->flags |= opt;
|
95
|
+
else h->flags &= ~opt;
|
96
|
+
break;
|
97
|
+
default:
|
98
|
+
rv = 0;
|
99
|
+
}
|
100
|
+
va_end(ap);
|
101
|
+
|
102
|
+
return rv;
|
103
|
+
}
|
104
|
+
|
105
|
+
void
|
106
|
+
yajl_free(yajl_handle handle)
|
107
|
+
{
|
108
|
+
yajl_bs_free(handle->stateStack);
|
109
|
+
yajl_buf_free(handle->decodeBuf);
|
110
|
+
if (handle->lexer) {
|
111
|
+
yajl_lex_free(handle->lexer);
|
112
|
+
handle->lexer = NULL;
|
113
|
+
}
|
114
|
+
YA_FREE(&(handle->alloc), handle);
|
115
|
+
}
|
116
|
+
|
117
|
+
yajl_status
|
118
|
+
yajl_parse(yajl_handle hand, const unsigned char * jsonText,
|
119
|
+
size_t jsonTextLen)
|
120
|
+
{
|
121
|
+
yajl_status status;
|
122
|
+
|
123
|
+
/* lazy allocation of the lexer */
|
124
|
+
if (hand->lexer == NULL) {
|
125
|
+
hand->lexer = yajl_lex_alloc(&(hand->alloc),
|
126
|
+
hand->flags & yajl_allow_comments,
|
127
|
+
!(hand->flags & yajl_dont_validate_strings));
|
128
|
+
}
|
129
|
+
|
130
|
+
status = yajl_do_parse(hand, jsonText, jsonTextLen);
|
131
|
+
return status;
|
132
|
+
}
|
133
|
+
|
134
|
+
|
135
|
+
yajl_status
|
136
|
+
yajl_complete_parse(yajl_handle hand)
|
137
|
+
{
|
138
|
+
/* The lexer is lazy allocated in the first call to parse. if parse is
|
139
|
+
* never called, then no data was provided to parse at all. This is a
|
140
|
+
* "premature EOF" error unless yajl_allow_partial_values is specified.
|
141
|
+
* allocating the lexer now is the simplest possible way to handle this
|
142
|
+
* case while preserving all the other semantics of the parser
|
143
|
+
* (multiple values, partial values, etc). */
|
144
|
+
if (hand->lexer == NULL) {
|
145
|
+
hand->lexer = yajl_lex_alloc(&(hand->alloc),
|
146
|
+
hand->flags & yajl_allow_comments,
|
147
|
+
!(hand->flags & yajl_dont_validate_strings));
|
148
|
+
}
|
149
|
+
|
150
|
+
return yajl_do_finish(hand);
|
151
|
+
}
|
152
|
+
|
153
|
+
unsigned char *
|
154
|
+
yajl_get_error(yajl_handle hand, int verbose,
|
155
|
+
const unsigned char * jsonText, size_t jsonTextLen)
|
156
|
+
{
|
157
|
+
return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
|
158
|
+
}
|
159
|
+
|
160
|
+
size_t
|
161
|
+
yajl_get_bytes_consumed(yajl_handle hand)
|
162
|
+
{
|
163
|
+
if (!hand) return 0;
|
164
|
+
else return hand->bytesConsumed;
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
void
|
169
|
+
yajl_free_error(yajl_handle hand, unsigned char * str)
|
170
|
+
{
|
171
|
+
/* use memory allocation functions if set */
|
172
|
+
YA_FREE(&(hand->alloc), str);
|
173
|
+
}
|
174
|
+
|
175
|
+
/* XXX: add utility routines to parse from file */
|
@@ -0,0 +1,9 @@
|
|
1
|
+
prefix=${CMAKE_INSTALL_PREFIX}
|
2
|
+
libdir=${dollar}{prefix}/lib${LIB_SUFFIX}
|
3
|
+
includedir=${dollar}{prefix}/include/yajl
|
4
|
+
|
5
|
+
Name: Yet Another JSON Library
|
6
|
+
Description: A Portable JSON parsing and serialization library in ANSI C
|
7
|
+
Version: ${YAJL_MAJOR}.${YAJL_MINOR}.${YAJL_MICRO}
|
8
|
+
Cflags: -I${dollar}{includedir}
|
9
|
+
Libs: -L${dollar}{libdir} -lyajl
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/**
|
18
|
+
* \file yajl_alloc.h
|
19
|
+
* default memory allocation routines for yajl which use malloc/realloc and
|
20
|
+
* free
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include "yajl_alloc.h"
|
24
|
+
#include <stdlib.h>
|
25
|
+
|
26
|
+
static void * yajl_internal_malloc(void *ctx, size_t sz)
|
27
|
+
{
|
28
|
+
(void)ctx;
|
29
|
+
return malloc(sz);
|
30
|
+
}
|
31
|
+
|
32
|
+
static void * yajl_internal_realloc(void *ctx, void * previous,
|
33
|
+
size_t sz)
|
34
|
+
{
|
35
|
+
(void)ctx;
|
36
|
+
return realloc(previous, sz);
|
37
|
+
}
|
38
|
+
|
39
|
+
static void yajl_internal_free(void *ctx, void * ptr)
|
40
|
+
{
|
41
|
+
(void)ctx;
|
42
|
+
free(ptr);
|
43
|
+
}
|
44
|
+
|
45
|
+
void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf)
|
46
|
+
{
|
47
|
+
yaf->malloc = yajl_internal_malloc;
|
48
|
+
yaf->free = yajl_internal_free;
|
49
|
+
yaf->realloc = yajl_internal_realloc;
|
50
|
+
yaf->ctx = NULL;
|
51
|
+
}
|
52
|
+
|