yajl-ruby 1.0.0-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of yajl-ruby might be problematic. Click here for more details.

Files changed (152) hide show
  1. data/.gitignore +12 -0
  2. data/.rspec +2 -0
  3. data/CHANGELOG.md +327 -0
  4. data/Gemfile +3 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +362 -0
  7. data/Rakefile +2 -0
  8. data/benchmark/encode.rb +72 -0
  9. data/benchmark/encode_json_and_marshal.rb +42 -0
  10. data/benchmark/encode_json_and_yaml.rb +53 -0
  11. data/benchmark/http.rb +32 -0
  12. data/benchmark/parse.rb +94 -0
  13. data/benchmark/parse_json_and_marshal.rb +50 -0
  14. data/benchmark/parse_json_and_yaml.rb +55 -0
  15. data/benchmark/parse_stream.rb +54 -0
  16. data/benchmark/subjects/item.json +1 -0
  17. data/benchmark/subjects/ohai.json +1216 -0
  18. data/benchmark/subjects/ohai.marshal_dump +0 -0
  19. data/benchmark/subjects/ohai.yml +975 -0
  20. data/benchmark/subjects/twitter_search.json +1 -0
  21. data/benchmark/subjects/twitter_stream.json +430 -0
  22. data/benchmark/subjects/unicode.json +1 -0
  23. data/examples/encoding/chunked_encoding.rb +27 -0
  24. data/examples/encoding/one_shot.rb +13 -0
  25. data/examples/encoding/to_an_io.rb +12 -0
  26. data/examples/http/twitter_search_api.rb +12 -0
  27. data/examples/http/twitter_stream_api.rb +26 -0
  28. data/examples/parsing/from_file.rb +14 -0
  29. data/examples/parsing/from_stdin.rb +9 -0
  30. data/examples/parsing/from_string.rb +13 -0
  31. data/ext/yajl/api/yajl_common.h +89 -0
  32. data/ext/yajl/api/yajl_gen.h +161 -0
  33. data/ext/yajl/api/yajl_parse.h +196 -0
  34. data/ext/yajl/api/yajl_version.h +23 -0
  35. data/ext/yajl/extconf.rb +7 -0
  36. data/ext/yajl/yajl.c +164 -0
  37. data/ext/yajl/yajl_alloc.c +65 -0
  38. data/ext/yajl/yajl_alloc.h +50 -0
  39. data/ext/yajl/yajl_buf.c +119 -0
  40. data/ext/yajl/yajl_buf.h +73 -0
  41. data/ext/yajl/yajl_bytestack.h +85 -0
  42. data/ext/yajl/yajl_encode.c +201 -0
  43. data/ext/yajl/yajl_encode.h +52 -0
  44. data/ext/yajl/yajl_ext.c +905 -0
  45. data/ext/yajl/yajl_ext.h +135 -0
  46. data/ext/yajl/yajl_gen.c +344 -0
  47. data/ext/yajl/yajl_lex.c +748 -0
  48. data/ext/yajl/yajl_lex.h +135 -0
  49. data/ext/yajl/yajl_parser.c +450 -0
  50. data/ext/yajl/yajl_parser.h +82 -0
  51. data/ext/yajl/yajl_version.c +7 -0
  52. data/lib/yajl.rb +75 -0
  53. data/lib/yajl/1.8/yajl.so +0 -0
  54. data/lib/yajl/1.9/yajl.so +0 -0
  55. data/lib/yajl/bzip2.rb +11 -0
  56. data/lib/yajl/bzip2/stream_reader.rb +31 -0
  57. data/lib/yajl/bzip2/stream_writer.rb +14 -0
  58. data/lib/yajl/deflate.rb +6 -0
  59. data/lib/yajl/deflate/stream_reader.rb +43 -0
  60. data/lib/yajl/deflate/stream_writer.rb +20 -0
  61. data/lib/yajl/gzip.rb +6 -0
  62. data/lib/yajl/gzip/stream_reader.rb +30 -0
  63. data/lib/yajl/gzip/stream_writer.rb +13 -0
  64. data/lib/yajl/http_stream.rb +212 -0
  65. data/lib/yajl/json_gem.rb +15 -0
  66. data/lib/yajl/json_gem/encoding.rb +51 -0
  67. data/lib/yajl/json_gem/parsing.rb +26 -0
  68. data/lib/yajl/version.rb +3 -0
  69. data/lib/yajl/yajl.rb +2 -0
  70. data/spec/encoding/encoding_spec.rb +271 -0
  71. data/spec/global/global_spec.rb +54 -0
  72. data/spec/http/fixtures/http.bzip2.dump +0 -0
  73. data/spec/http/fixtures/http.chunked.dump +11 -0
  74. data/spec/http/fixtures/http.deflate.dump +0 -0
  75. data/spec/http/fixtures/http.error.dump +12 -0
  76. data/spec/http/fixtures/http.gzip.dump +0 -0
  77. data/spec/http/fixtures/http.html.dump +1220 -0
  78. data/spec/http/fixtures/http.raw.dump +1226 -0
  79. data/spec/http/http_delete_spec.rb +98 -0
  80. data/spec/http/http_error_spec.rb +32 -0
  81. data/spec/http/http_get_spec.rb +109 -0
  82. data/spec/http/http_post_spec.rb +123 -0
  83. data/spec/http/http_put_spec.rb +105 -0
  84. data/spec/http/http_stream_options_spec.rb +27 -0
  85. data/spec/json_gem_compatibility/compatibility_spec.rb +203 -0
  86. data/spec/parsing/active_support_spec.rb +64 -0
  87. data/spec/parsing/chunked_spec.rb +96 -0
  88. data/spec/parsing/fixtures/fail.15.json +1 -0
  89. data/spec/parsing/fixtures/fail.16.json +1 -0
  90. data/spec/parsing/fixtures/fail.17.json +1 -0
  91. data/spec/parsing/fixtures/fail.26.json +1 -0
  92. data/spec/parsing/fixtures/fail11.json +1 -0
  93. data/spec/parsing/fixtures/fail12.json +1 -0
  94. data/spec/parsing/fixtures/fail13.json +1 -0
  95. data/spec/parsing/fixtures/fail14.json +1 -0
  96. data/spec/parsing/fixtures/fail19.json +1 -0
  97. data/spec/parsing/fixtures/fail20.json +1 -0
  98. data/spec/parsing/fixtures/fail21.json +1 -0
  99. data/spec/parsing/fixtures/fail22.json +1 -0
  100. data/spec/parsing/fixtures/fail23.json +1 -0
  101. data/spec/parsing/fixtures/fail24.json +1 -0
  102. data/spec/parsing/fixtures/fail25.json +1 -0
  103. data/spec/parsing/fixtures/fail27.json +2 -0
  104. data/spec/parsing/fixtures/fail28.json +2 -0
  105. data/spec/parsing/fixtures/fail3.json +1 -0
  106. data/spec/parsing/fixtures/fail4.json +1 -0
  107. data/spec/parsing/fixtures/fail5.json +1 -0
  108. data/spec/parsing/fixtures/fail6.json +1 -0
  109. data/spec/parsing/fixtures/fail9.json +1 -0
  110. data/spec/parsing/fixtures/pass.array.json +6 -0
  111. data/spec/parsing/fixtures/pass.codepoints_from_unicode_org.json +1 -0
  112. data/spec/parsing/fixtures/pass.contacts.json +1 -0
  113. data/spec/parsing/fixtures/pass.db100.xml.json +1 -0
  114. data/spec/parsing/fixtures/pass.db1000.xml.json +1 -0
  115. data/spec/parsing/fixtures/pass.dc_simple_with_comments.json +11 -0
  116. data/spec/parsing/fixtures/pass.deep_arrays.json +1 -0
  117. data/spec/parsing/fixtures/pass.difficult_json_c_test_case.json +1 -0
  118. data/spec/parsing/fixtures/pass.difficult_json_c_test_case_with_comments.json +1 -0
  119. data/spec/parsing/fixtures/pass.doubles.json +1 -0
  120. data/spec/parsing/fixtures/pass.empty_array.json +1 -0
  121. data/spec/parsing/fixtures/pass.empty_string.json +1 -0
  122. data/spec/parsing/fixtures/pass.escaped_bulgarian.json +4 -0
  123. data/spec/parsing/fixtures/pass.escaped_foobar.json +1 -0
  124. data/spec/parsing/fixtures/pass.item.json +1 -0
  125. data/spec/parsing/fixtures/pass.json-org-sample1.json +23 -0
  126. data/spec/parsing/fixtures/pass.json-org-sample2.json +11 -0
  127. data/spec/parsing/fixtures/pass.json-org-sample3.json +26 -0
  128. data/spec/parsing/fixtures/pass.json-org-sample4-nows.json +88 -0
  129. data/spec/parsing/fixtures/pass.json-org-sample4.json +89 -0
  130. data/spec/parsing/fixtures/pass.json-org-sample5.json +27 -0
  131. data/spec/parsing/fixtures/pass.map-spain.xml.json +1 -0
  132. data/spec/parsing/fixtures/pass.ns-invoice100.xml.json +1 -0
  133. data/spec/parsing/fixtures/pass.ns-soap.xml.json +1 -0
  134. data/spec/parsing/fixtures/pass.numbers-fp-4k.json +6 -0
  135. data/spec/parsing/fixtures/pass.numbers-fp-64k.json +61 -0
  136. data/spec/parsing/fixtures/pass.numbers-int-4k.json +11 -0
  137. data/spec/parsing/fixtures/pass.numbers-int-64k.json +154 -0
  138. data/spec/parsing/fixtures/pass.twitter-search.json +1 -0
  139. data/spec/parsing/fixtures/pass.twitter-search2.json +1 -0
  140. data/spec/parsing/fixtures/pass.unicode.json +3315 -0
  141. data/spec/parsing/fixtures/pass.yelp.json +1 -0
  142. data/spec/parsing/fixtures/pass1.json +56 -0
  143. data/spec/parsing/fixtures/pass2.json +1 -0
  144. data/spec/parsing/fixtures/pass3.json +6 -0
  145. data/spec/parsing/fixtures_spec.rb +40 -0
  146. data/spec/parsing/one_off_spec.rb +85 -0
  147. data/spec/rcov.opts +3 -0
  148. data/spec/spec_helper.rb +16 -0
  149. data/tasks/compile.rake +35 -0
  150. data/tasks/rspec.rake +16 -0
  151. data/yajl-ruby.gemspec +24 -0
  152. metadata +335 -0
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ obj = {
7
+ :a_test => "of encoding in one pass",
8
+ :which_will => "simply return a string when finished",
9
+ :as_easy_as => 123
10
+ }
11
+
12
+ str = Yajl::Encoder.encode(obj)
13
+ puts str
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ obj = {
7
+ :a_test => "of encoding directly to an IO stream",
8
+ :which_will => "simply return a string when finished",
9
+ :as_easy_as => 123
10
+ }
11
+
12
+ Yajl::Encoder.encode(obj, STDOUT)
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl/http_stream'
5
+ require 'uri'
6
+
7
+ unless keywords = ARGV[0]
8
+ puts "\nUsage: ruby examples/http/twitter_search_api.rb keyword\n\n"
9
+ exit(0)
10
+ end
11
+
12
+ puts Yajl::HttpStream.get("http://search.twitter.com/search.json?q=#{keywords}").inspect
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl/gzip'
5
+ require 'yajl/deflate'
6
+ require 'yajl/http_stream'
7
+ require 'uri'
8
+
9
+ unless (username = ARGV[0]) && (password = ARGV[1])
10
+ puts "\nUsage: ruby examples/http/twitter_stream_api.rb username password\n\n"
11
+ exit(0)
12
+ end
13
+ captured = 0
14
+ uri = URI.parse("http://#{username}:#{password}@stream.twitter.com/1/statuses/sample.json")
15
+
16
+ trap('INT') {
17
+ puts "\n\nCaptured #{captured} objects from the stream"
18
+ puts "CTRL+C caught, later!"
19
+ exit(0)
20
+ }
21
+
22
+ Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
23
+ STDOUT.putc '.'
24
+ STDOUT.flush
25
+ captured += 1
26
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ unless file = ARGV[0]
7
+ puts "\nUsage: ruby examples/from_file.rb benchmark/subjects/item.json\n\n"
8
+ exit(0)
9
+ end
10
+
11
+ json = File.new(file, 'r')
12
+
13
+ hash = Yajl::Parser.parse(json)
14
+ puts hash.inspect
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ # Usage: cat benchmark/subjects/item.json | ruby examples/from_stdin.rb
7
+
8
+ hash = Yajl::Parser.parse(STDIN)
9
+ puts hash.inspect
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../..')
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+ require 'stringio'
6
+
7
+ unless string = ARGV[0]
8
+ puts "\nUsage: ruby examples/from_string.rb '{\"foo\": 1145}'\n\n"
9
+ exit(0)
10
+ end
11
+
12
+ hash = Yajl::Parser.parse(string)
13
+ puts hash.inspect
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ #ifndef __YAJL_COMMON_H__
34
+ #define __YAJL_COMMON_H__
35
+
36
+ #ifdef __cplusplus
37
+ extern "C" {
38
+ #endif
39
+
40
+ #define YAJL_MAX_DEPTH 128
41
+
42
+ /* msft dll export gunk. To build a DLL on windows, you
43
+ * must define WIN32, YAJL_SHARED, and YAJL_BUILD. To use a shared
44
+ * DLL, you must define YAJL_SHARED and WIN32 */
45
+ #if defined(WIN32) && defined(YAJL_SHARED)
46
+ # ifdef YAJL_BUILD
47
+ # define YAJL_API __declspec(dllexport)
48
+ # else
49
+ # define YAJL_API __declspec(dllimport)
50
+ # endif
51
+ #else
52
+ # if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 303
53
+ # define YAJL_API __attribute__ ((visibility("default")))
54
+ # else
55
+ # define YAJL_API
56
+ # endif
57
+ #endif
58
+
59
+ /** pointer to a malloc function, supporting client overriding memory
60
+ * allocation routines */
61
+ typedef void * (*yajl_malloc_func)(void *ctx, unsigned int sz);
62
+
63
+ /** pointer to a free function, supporting client overriding memory
64
+ * allocation routines */
65
+ typedef void (*yajl_free_func)(void *ctx, void * ptr);
66
+
67
+ /** pointer to a realloc function which can resize an allocation. */
68
+ typedef void * (*yajl_realloc_func)(void *ctx, void * ptr, unsigned int sz);
69
+
70
+ /** A structure which can be passed to yajl_*_alloc routines to allow the
71
+ * client to specify memory allocation functions to be used. */
72
+ typedef struct
73
+ {
74
+ /** pointer to a function that can allocate uninitialized memory */
75
+ yajl_malloc_func malloc;
76
+ /** pointer to a function that can resize memory allocations */
77
+ yajl_realloc_func realloc;
78
+ /** pointer to a function that can free memory allocated using
79
+ * reallocFunction or mallocFunction */
80
+ yajl_free_func free;
81
+ /** a context pointer that will be passed to above allocation routines */
82
+ void * ctx;
83
+ } yajl_alloc_funcs;
84
+
85
+ #ifdef __cplusplus
86
+ }
87
+ #endif
88
+
89
+ #endif
@@ -0,0 +1,161 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ /**
34
+ * \file yajl_gen.h
35
+ * Interface to YAJL's JSON generation facilities.
36
+ */
37
+
38
+ #include "api/yajl_common.h"
39
+
40
+ #ifndef __YAJL_GEN_H__
41
+ #define __YAJL_GEN_H__
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+ /** generator status codes */
47
+ typedef enum {
48
+ /** no error */
49
+ yajl_gen_status_ok = 0,
50
+ /** at a point where a map key is generated, a function other than
51
+ * yajl_gen_string was called */
52
+ yajl_gen_keys_must_be_strings,
53
+ /** YAJL's maximum generation depth was exceeded. see
54
+ * YAJL_MAX_DEPTH */
55
+ yajl_max_depth_exceeded,
56
+ /** A generator function (yajl_gen_XXX) was called while in an error
57
+ * state */
58
+ yajl_gen_in_error_state,
59
+ /** A complete JSON document has been generated */
60
+ yajl_gen_generation_complete,
61
+ /** yajl_gen_double was passed an invalid floating point value
62
+ * (infinity or NaN). */
63
+ yajl_gen_invalid_number,
64
+ /** A print callback was passed in, so there is no internal
65
+ * buffer to get from */
66
+ yajl_gen_no_buf
67
+ } yajl_gen_status;
68
+
69
+ /** an opaque handle to a generator */
70
+ typedef struct yajl_gen_t * yajl_gen;
71
+
72
+ /** a callback used for "printing" the results. */
73
+ typedef void (*yajl_print_t)(void * ctx,
74
+ const char * str,
75
+ unsigned int len);
76
+
77
+ /** configuration structure for the generator */
78
+ typedef struct {
79
+ /** generate indented (beautiful) output */
80
+ unsigned int beautify;
81
+ /** an opportunity to define an indent string. such as \\t or
82
+ * some number of spaces. default is four spaces ' '. This
83
+ * member is only relevant when beautify is true */
84
+ const char * indentString;
85
+ /** escape the '/' character */
86
+ unsigned int htmlSafe;
87
+ } yajl_gen_config;
88
+
89
+ /** allocate a generator handle
90
+ * \param config a pointer to a structure containing parameters which
91
+ * configure the behavior of the json generator
92
+ * \param allocFuncs an optional pointer to a structure which allows
93
+ * the client to overide the memory allocation
94
+ * used by yajl. May be NULL, in which case
95
+ * malloc/free/realloc will be used.
96
+ *
97
+ * \returns an allocated handle on success, NULL on failure (bad params)
98
+ */
99
+ YAJL_API yajl_gen yajl_gen_alloc(const yajl_gen_config * config,
100
+ const yajl_alloc_funcs * allocFuncs);
101
+
102
+ /** allocate a generator handle that will print to the specified
103
+ * callback rather than storing the results in an internal buffer.
104
+ * \param callback a pointer to a printer function. May be NULL
105
+ * in which case, the results will be store in an
106
+ * internal buffer.
107
+ * \param config a pointer to a structure containing parameters
108
+ * which configure the behavior of the json
109
+ * generator.
110
+ * \param allocFuncs an optional pointer to a structure which allows
111
+ * the client to overide the memory allocation
112
+ * used by yajl. May be NULL, in which case
113
+ * malloc/free/realloc will be used.
114
+ * \param ctx a context pointer that will be passed to the
115
+ * printer callback.
116
+ *
117
+ * \returns an allocated handle on success, NULL on failure (bad params)
118
+ */
119
+ YAJL_API yajl_gen yajl_gen_alloc2(const yajl_print_t callback,
120
+ const yajl_gen_config * config,
121
+ const yajl_alloc_funcs * allocFuncs,
122
+ void * ctx);
123
+
124
+ /** free a generator handle */
125
+ YAJL_API void yajl_gen_free(yajl_gen handle);
126
+
127
+ YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long int number);
128
+ /** generate a floating point number. number may not be infinity or
129
+ * NaN, as these have no representation in JSON. In these cases the
130
+ * generator will return 'yajl_gen_invalid_number' */
131
+ YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number);
132
+ YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand,
133
+ const char * num,
134
+ unsigned int len);
135
+ YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand,
136
+ const unsigned char * str,
137
+ unsigned int len);
138
+ YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand);
139
+ YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean);
140
+ YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand);
141
+ YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand);
142
+ YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand);
143
+ YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand);
144
+
145
+ /** access the null terminated generator buffer. If incrementally
146
+ * outputing JSON, one should call yajl_gen_clear to clear the
147
+ * buffer. This allows stream generation. */
148
+ YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand,
149
+ const unsigned char ** buf,
150
+ unsigned int * len);
151
+
152
+ /** clear yajl's output buffer, but maintain all internal generation
153
+ * state. This function will not "reset" the generator state, and is
154
+ * intended to enable incremental JSON outputing. */
155
+ YAJL_API void yajl_gen_clear(yajl_gen hand);
156
+
157
+ #ifdef __cplusplus
158
+ }
159
+ #endif
160
+
161
+ #endif
@@ -0,0 +1,196 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ /**
34
+ * \file yajl_parse.h
35
+ * Interface to YAJL's JSON parsing facilities.
36
+ */
37
+
38
+ #include "api/yajl_common.h"
39
+
40
+ #ifndef __YAJL_PARSE_H__
41
+ #define __YAJL_PARSE_H__
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+ /** error codes returned from this interface */
47
+ typedef enum {
48
+ /** no error was encountered */
49
+ yajl_status_ok,
50
+ /** a client callback returned zero, stopping the parse */
51
+ yajl_status_client_canceled,
52
+ /** The parse cannot yet complete because more json input text
53
+ * is required, call yajl_parse with the next buffer of input text.
54
+ * (pertinent only when stream parsing) */
55
+ yajl_status_insufficient_data,
56
+ /** An error occured during the parse. Call yajl_get_error for
57
+ * more information about the encountered error */
58
+ yajl_status_error
59
+ } yajl_status;
60
+
61
+ /** attain a human readable, english, string for an error */
62
+ YAJL_API const char * yajl_status_to_string(yajl_status code);
63
+
64
+ /** an opaque handle to a parser */
65
+ typedef struct yajl_handle_t * yajl_handle;
66
+
67
+ /** yajl is an event driven parser. this means as json elements are
68
+ * parsed, you are called back to do something with the data. The
69
+ * functions in this table indicate the various events for which
70
+ * you will be called back. Each callback accepts a "context"
71
+ * pointer, this is a void * that is passed into the yajl_parse
72
+ * function which the client code may use to pass around context.
73
+ *
74
+ * All callbacks return an integer. If non-zero, the parse will
75
+ * continue. If zero, the parse will be canceled and
76
+ * yajl_status_client_canceled will be returned from the parse.
77
+ *
78
+ * Note about handling of numbers:
79
+ * yajl will only convert numbers that can be represented in a double
80
+ * or a long int. All other numbers will be passed to the client
81
+ * in string form using the yajl_number callback. Furthermore, if
82
+ * yajl_number is not NULL, it will always be used to return numbers,
83
+ * that is yajl_integer and yajl_double will be ignored. If
84
+ * yajl_number is NULL but one of yajl_integer or yajl_double are
85
+ * defined, parsing of a number larger than is representable
86
+ * in a double or long int will result in a parse error.
87
+ */
88
+ typedef struct {
89
+ int (* yajl_null)(void * ctx);
90
+ int (* yajl_boolean)(void * ctx, int boolVal);
91
+ int (* yajl_integer)(void * ctx, long integerVal);
92
+ int (* yajl_double)(void * ctx, double doubleVal);
93
+ /** A callback which passes the string representation of the number
94
+ * back to the client. Will be used for all numbers when present */
95
+ int (* yajl_number)(void * ctx, const char * numberVal,
96
+ unsigned int numberLen);
97
+
98
+ /** strings are returned as pointers into the JSON text when,
99
+ * possible, as a result, they are _not_ null padded */
100
+ int (* yajl_string)(void * ctx, const unsigned char * stringVal,
101
+ unsigned int stringLen);
102
+
103
+ int (* yajl_start_map)(void * ctx);
104
+ int (* yajl_map_key)(void * ctx, const unsigned char * key,
105
+ unsigned int stringLen);
106
+ int (* yajl_end_map)(void * ctx);
107
+
108
+ int (* yajl_start_array)(void * ctx);
109
+ int (* yajl_end_array)(void * ctx);
110
+ } yajl_callbacks;
111
+
112
+ /** configuration structure for the generator */
113
+ typedef struct {
114
+ /** if nonzero, javascript style comments will be allowed in
115
+ * the json input, both slash star and slash slash */
116
+ unsigned int allowComments;
117
+ /** if nonzero, invalid UTF8 strings will cause a parse
118
+ * error */
119
+ unsigned int checkUTF8;
120
+ } yajl_parser_config;
121
+
122
+ /** allocate a parser handle
123
+ * \param callbacks a yajl callbacks structure specifying the
124
+ * functions to call when different JSON entities
125
+ * are encountered in the input text. May be NULL,
126
+ * which is only useful for validation.
127
+ * \param config configuration parameters for the parse.
128
+ * \param ctx a context pointer that will be passed to callbacks.
129
+ */
130
+ YAJL_API yajl_handle yajl_alloc(const yajl_callbacks * callbacks,
131
+ const yajl_parser_config * config,
132
+ const yajl_alloc_funcs * allocFuncs,
133
+ void * ctx);
134
+
135
+ /** allow resetting of the lexer without the need to realloc a new parser */
136
+ void yajl_reset_parser(yajl_handle hand);
137
+
138
+ /** free a parser handle */
139
+ YAJL_API void yajl_free(yajl_handle handle);
140
+
141
+ /** Parse some json!
142
+ * \param hand - a handle to the json parser allocated with yajl_alloc
143
+ * \param jsonText - a pointer to the UTF8 json text to be parsed
144
+ * \param jsonTextLength - the length, in bytes, of input text
145
+ */
146
+ YAJL_API yajl_status yajl_parse(yajl_handle hand,
147
+ const unsigned char * jsonText,
148
+ unsigned int jsonTextLength);
149
+
150
+ /** Parse any remaining buffered json.
151
+ * Since yajl is a stream-based parser, without an explicit end of
152
+ * input, yajl sometimes can't decide if content at the end of the
153
+ * stream is valid or not. For example, if "1" has been fed in,
154
+ * yajl can't know whether another digit is next or some character
155
+ * that would terminate the integer token.
156
+ *
157
+ * \param hand - a handle to the json parser allocated with yajl_alloc
158
+ */
159
+ YAJL_API yajl_status yajl_parse_complete(yajl_handle hand);
160
+
161
+ /** get an error string describing the state of the
162
+ * parse.
163
+ *
164
+ * If verbose is non-zero, the message will include the JSON
165
+ * text where the error occured, along with an arrow pointing to
166
+ * the specific char.
167
+ *
168
+ * \returns A dynamically allocated string will be returned which should
169
+ * be freed with yajl_free_error
170
+ */
171
+ YAJL_API unsigned char * yajl_get_error(yajl_handle hand, int verbose,
172
+ const unsigned char * jsonText,
173
+ unsigned int jsonTextLength);
174
+
175
+ /**
176
+ * get the amount of data consumed from the last chunk passed to YAJL.
177
+ *
178
+ * In the case of a successful parse this can help you understand if
179
+ * the entire buffer was consumed (which will allow you to handle
180
+ * "junk at end of input".
181
+ *
182
+ * In the event an error is encountered during parsing, this function
183
+ * affords the client a way to get the offset into the most recent
184
+ * chunk where the error occured. 0 will be returned if no error
185
+ * was encountered.
186
+ */
187
+ YAJL_API unsigned int yajl_get_bytes_consumed(yajl_handle hand);
188
+
189
+ /** free an error returned from yajl_get_error */
190
+ YAJL_API void yajl_free_error(yajl_handle hand, unsigned char * str);
191
+
192
+ #ifdef __cplusplus
193
+ }
194
+ #endif
195
+
196
+ #endif