benofsky-yajl-ruby 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG.md +281 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +320 -0
  5. data/Rakefile +40 -0
  6. data/VERSION.yml +5 -0
  7. data/benchmark/encode.rb +58 -0
  8. data/benchmark/encode_json_and_marshal.rb +42 -0
  9. data/benchmark/encode_json_and_yaml.rb +53 -0
  10. data/benchmark/http.rb +32 -0
  11. data/benchmark/parse.rb +59 -0
  12. data/benchmark/parse_json_and_marshal.rb +50 -0
  13. data/benchmark/parse_json_and_yaml.rb +55 -0
  14. data/benchmark/parse_stream.rb +54 -0
  15. data/benchmark/subjects/item.json +1 -0
  16. data/benchmark/subjects/ohai.json +1216 -0
  17. data/benchmark/subjects/ohai.marshal_dump +0 -0
  18. data/benchmark/subjects/ohai.yml +975 -0
  19. data/benchmark/subjects/twitter_search.json +1 -0
  20. data/benchmark/subjects/twitter_stream.json +430 -0
  21. data/benchmark/subjects/unicode.json +1 -0
  22. data/examples/encoding/chunked_encoding.rb +27 -0
  23. data/examples/encoding/one_shot.rb +13 -0
  24. data/examples/encoding/to_an_io.rb +12 -0
  25. data/examples/http/twitter_search_api.rb +12 -0
  26. data/examples/http/twitter_stream_api.rb +26 -0
  27. data/examples/parsing/from_file.rb +14 -0
  28. data/examples/parsing/from_stdin.rb +9 -0
  29. data/examples/parsing/from_string.rb +13 -0
  30. data/ext/api/yajl_common.h +85 -0
  31. data/ext/api/yajl_gen.h +159 -0
  32. data/ext/api/yajl_parse.h +196 -0
  33. data/ext/extconf.rb +9 -0
  34. data/ext/yajl.c +164 -0
  35. data/ext/yajl_alloc.c +65 -0
  36. data/ext/yajl_alloc.h +50 -0
  37. data/ext/yajl_buf.c +119 -0
  38. data/ext/yajl_buf.h +73 -0
  39. data/ext/yajl_bytestack.h +85 -0
  40. data/ext/yajl_encode.c +188 -0
  41. data/ext/yajl_encode.h +50 -0
  42. data/ext/yajl_ext.c +911 -0
  43. data/ext/yajl_ext.h +128 -0
  44. data/ext/yajl_gen.c +317 -0
  45. data/ext/yajl_lex.c +747 -0
  46. data/ext/yajl_lex.h +135 -0
  47. data/ext/yajl_parser.c +450 -0
  48. data/ext/yajl_parser.h +82 -0
  49. data/lib/yajl/bzip2/stream_reader.rb +32 -0
  50. data/lib/yajl/bzip2/stream_writer.rb +15 -0
  51. data/lib/yajl/bzip2.rb +11 -0
  52. data/lib/yajl/deflate/stream_reader.rb +44 -0
  53. data/lib/yajl/deflate/stream_writer.rb +21 -0
  54. data/lib/yajl/deflate.rb +6 -0
  55. data/lib/yajl/gzip/stream_reader.rb +31 -0
  56. data/lib/yajl/gzip/stream_writer.rb +14 -0
  57. data/lib/yajl/gzip.rb +6 -0
  58. data/lib/yajl/http_stream.rb +197 -0
  59. data/lib/yajl/json_gem/encoding.rb +50 -0
  60. data/lib/yajl/json_gem/parsing.rb +27 -0
  61. data/lib/yajl/json_gem.rb +14 -0
  62. data/lib/yajl.rb +93 -0
  63. data/spec/encoding/encoding_spec.rb +234 -0
  64. data/spec/global/global_spec.rb +55 -0
  65. data/spec/http/fixtures/http.bzip2.dump +0 -0
  66. data/spec/http/fixtures/http.chunked.dump +11 -0
  67. data/spec/http/fixtures/http.deflate.dump +0 -0
  68. data/spec/http/fixtures/http.error.dump +12 -0
  69. data/spec/http/fixtures/http.gzip.dump +0 -0
  70. data/spec/http/fixtures/http.html.dump +1220 -0
  71. data/spec/http/fixtures/http.raw.dump +1226 -0
  72. data/spec/http/http_delete_spec.rb +99 -0
  73. data/spec/http/http_error_spec.rb +33 -0
  74. data/spec/http/http_get_spec.rb +110 -0
  75. data/spec/http/http_post_spec.rb +124 -0
  76. data/spec/http/http_put_spec.rb +106 -0
  77. data/spec/json_gem_compatibility/compatibility_spec.rb +203 -0
  78. data/spec/parsing/active_support_spec.rb +64 -0
  79. data/spec/parsing/chunked_spec.rb +98 -0
  80. data/spec/parsing/fixtures/fail.15.json +1 -0
  81. data/spec/parsing/fixtures/fail.16.json +1 -0
  82. data/spec/parsing/fixtures/fail.17.json +1 -0
  83. data/spec/parsing/fixtures/fail.26.json +1 -0
  84. data/spec/parsing/fixtures/fail11.json +1 -0
  85. data/spec/parsing/fixtures/fail12.json +1 -0
  86. data/spec/parsing/fixtures/fail13.json +1 -0
  87. data/spec/parsing/fixtures/fail14.json +1 -0
  88. data/spec/parsing/fixtures/fail19.json +1 -0
  89. data/spec/parsing/fixtures/fail20.json +1 -0
  90. data/spec/parsing/fixtures/fail21.json +1 -0
  91. data/spec/parsing/fixtures/fail22.json +1 -0
  92. data/spec/parsing/fixtures/fail23.json +1 -0
  93. data/spec/parsing/fixtures/fail24.json +1 -0
  94. data/spec/parsing/fixtures/fail25.json +1 -0
  95. data/spec/parsing/fixtures/fail27.json +2 -0
  96. data/spec/parsing/fixtures/fail28.json +2 -0
  97. data/spec/parsing/fixtures/fail3.json +1 -0
  98. data/spec/parsing/fixtures/fail4.json +1 -0
  99. data/spec/parsing/fixtures/fail5.json +1 -0
  100. data/spec/parsing/fixtures/fail6.json +1 -0
  101. data/spec/parsing/fixtures/fail9.json +1 -0
  102. data/spec/parsing/fixtures/pass.array.json +6 -0
  103. data/spec/parsing/fixtures/pass.codepoints_from_unicode_org.json +1 -0
  104. data/spec/parsing/fixtures/pass.contacts.json +1 -0
  105. data/spec/parsing/fixtures/pass.db100.xml.json +1 -0
  106. data/spec/parsing/fixtures/pass.db1000.xml.json +1 -0
  107. data/spec/parsing/fixtures/pass.dc_simple_with_comments.json +11 -0
  108. data/spec/parsing/fixtures/pass.deep_arrays.json +1 -0
  109. data/spec/parsing/fixtures/pass.difficult_json_c_test_case.json +1 -0
  110. data/spec/parsing/fixtures/pass.difficult_json_c_test_case_with_comments.json +1 -0
  111. data/spec/parsing/fixtures/pass.doubles.json +1 -0
  112. data/spec/parsing/fixtures/pass.empty_array.json +1 -0
  113. data/spec/parsing/fixtures/pass.empty_string.json +1 -0
  114. data/spec/parsing/fixtures/pass.escaped_bulgarian.json +4 -0
  115. data/spec/parsing/fixtures/pass.escaped_foobar.json +1 -0
  116. data/spec/parsing/fixtures/pass.item.json +1 -0
  117. data/spec/parsing/fixtures/pass.json-org-sample1.json +23 -0
  118. data/spec/parsing/fixtures/pass.json-org-sample2.json +11 -0
  119. data/spec/parsing/fixtures/pass.json-org-sample3.json +26 -0
  120. data/spec/parsing/fixtures/pass.json-org-sample4-nows.json +88 -0
  121. data/spec/parsing/fixtures/pass.json-org-sample4.json +89 -0
  122. data/spec/parsing/fixtures/pass.json-org-sample5.json +27 -0
  123. data/spec/parsing/fixtures/pass.map-spain.xml.json +1 -0
  124. data/spec/parsing/fixtures/pass.ns-invoice100.xml.json +1 -0
  125. data/spec/parsing/fixtures/pass.ns-soap.xml.json +1 -0
  126. data/spec/parsing/fixtures/pass.numbers-fp-4k.json +6 -0
  127. data/spec/parsing/fixtures/pass.numbers-fp-64k.json +61 -0
  128. data/spec/parsing/fixtures/pass.numbers-int-4k.json +11 -0
  129. data/spec/parsing/fixtures/pass.numbers-int-64k.json +154 -0
  130. data/spec/parsing/fixtures/pass.twitter-search.json +1 -0
  131. data/spec/parsing/fixtures/pass.twitter-search2.json +1 -0
  132. data/spec/parsing/fixtures/pass.unicode.json +3315 -0
  133. data/spec/parsing/fixtures/pass.yelp.json +1 -0
  134. data/spec/parsing/fixtures/pass1.json +56 -0
  135. data/spec/parsing/fixtures/pass2.json +1 -0
  136. data/spec/parsing/fixtures/pass3.json +6 -0
  137. data/spec/parsing/fixtures_spec.rb +41 -0
  138. data/spec/parsing/one_off_spec.rb +81 -0
  139. data/spec/rcov.opts +3 -0
  140. data/spec/spec.opts +2 -0
  141. data/spec/spec_helper.rb +16 -0
  142. data/yajl-ruby.gemspec +203 -0
  143. metadata +232 -0
data/ext/yajl_ext.h ADDED
@@ -0,0 +1,128 @@
1
+ /*
2
+ * Copyright (c) 2008-2009 Brian Lopez - http://github.com/brianmario
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files (the
6
+ * "Software"), to deal in the Software without restriction, including
7
+ * without limitation the rights to use, copy, modify, merge, publish,
8
+ * distribute, sublicense, and/or sell copies of the Software, and to
9
+ * permit persons to whom the Software is furnished to do so, subject to
10
+ * the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ #include "api/yajl_parse.h"
25
+ #include "api/yajl_gen.h"
26
+ #include <ruby.h>
27
+
28
+ #ifdef HAVE_RUBY_ENCODING_H
29
+ #include <ruby/encoding.h>
30
+ static rb_encoding *utf8Encoding;
31
+ #endif
32
+
33
+ #define READ_BUFSIZE 8192
34
+ #define WRITE_BUFSIZE 8192
35
+
36
+ /* Older versions of Ruby (< 1.8.6) need these */
37
+ #ifndef RSTRING_PTR
38
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
39
+ #endif
40
+ #ifndef RSTRING_LEN
41
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
42
+ #endif
43
+ #ifndef RARRAY_PTR
44
+ #define RARRAY_PTR(s) (RARRAY(s)->ptr)
45
+ #endif
46
+ #ifndef RARRAY_LEN
47
+ #define RARRAY_LEN(s) (RARRAY(s)->len)
48
+ #endif
49
+
50
+ static VALUE cParseError, cEncodeError, mYajl, cParser, cEncoder;
51
+ static ID intern_io_read, intern_call, intern_keys, intern_to_s,
52
+ intern_to_json, intern_has_key, intern_to_sym, intern_as_json;
53
+ static ID sym_allow_comments, sym_check_utf8, sym_pretty, sym_indent, sym_terminator, sym_symbolize_keys;
54
+
55
+ #define GetParser(obj, sval) (sval = (yajl_parser_wrapper*)DATA_PTR(obj));
56
+ #define GetEncoder(obj, sval) (sval = (yajl_encoder_wrapper*)DATA_PTR(obj));
57
+
58
+ inline void yajl_check_and_fire_callback(void * ctx);
59
+ inline void yajl_set_static_value(void * ctx, VALUE val);
60
+ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io);
61
+ void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle parser);
62
+
63
+ static int yajl_found_null(void * ctx);
64
+ static int yajl_found_boolean(void * ctx, int boolean);
65
+ static int yajl_found_number(void * ctx, const char * numberVal, unsigned int numberLen);
66
+ static int yajl_found_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen);
67
+ static int yajl_found_hash_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen);
68
+ static int yajl_found_start_hash(void * ctx);
69
+ static int yajl_found_end_hash(void * ctx);
70
+ static int yajl_found_start_array(void * ctx);
71
+ static int yajl_found_end_array(void * ctx);
72
+
73
+ static yajl_callbacks callbacks = {
74
+ yajl_found_null,
75
+ yajl_found_boolean,
76
+ NULL,
77
+ NULL,
78
+ yajl_found_number,
79
+ yajl_found_string,
80
+ yajl_found_start_hash,
81
+ yajl_found_hash_key,
82
+ yajl_found_end_hash,
83
+ yajl_found_start_array,
84
+ yajl_found_end_array
85
+ };
86
+
87
+ typedef struct {
88
+ VALUE builderStack;
89
+ VALUE parse_complete_callback;
90
+ int nestedArrayLevel;
91
+ int nestedHashLevel;
92
+ int objectsFound;
93
+ int symbolizeKeys;
94
+ yajl_handle parser;
95
+ } yajl_parser_wrapper;
96
+
97
+ typedef struct {
98
+ VALUE on_progress_callback;
99
+ VALUE terminator;
100
+ yajl_gen encoder;
101
+ } yajl_encoder_wrapper;
102
+
103
+ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE self);
104
+ static VALUE rb_yajl_parser_init(int argc, VALUE * argv, VALUE self);
105
+ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self);
106
+ static VALUE rb_yajl_parser_parse_chunk(VALUE self, VALUE chunk);
107
+ static VALUE rb_yajl_parser_set_complete_cb(VALUE self, VALUE callback);
108
+ static void yajl_parser_wrapper_free(void * wrapper);
109
+ static void yajl_parser_wrapper_mark(void * wrapper);
110
+
111
+ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass);
112
+ static VALUE rb_yajl_encoder_init(int argc, VALUE * argv, VALUE self);
113
+ static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self);
114
+ static VALUE rb_yajl_encoder_set_progress_cb(VALUE self, VALUE callback);
115
+ static void yajl_encoder_wrapper_free(void * wrapper);
116
+ static void yajl_encoder_wrapper_mark(void * wrapper);
117
+
118
+ static VALUE rb_yajl_json_ext_hash_to_json(int argc, VALUE * argv, VALUE self);
119
+ static VALUE rb_yajl_json_ext_array_to_json(int argc, VALUE * argv, VALUE self);
120
+ static VALUE rb_yajl_json_ext_fixnum_to_json(int argc, VALUE * argv, VALUE self);
121
+ static VALUE rb_yajl_json_ext_float_to_json(int argc, VALUE * argv, VALUE self);
122
+ static VALUE rb_yajl_json_ext_string_to_json(int argc, VALUE * argv, VALUE self);
123
+ static VALUE rb_yajl_json_ext_true_to_json(int argc, VALUE * argv, VALUE self);
124
+ static VALUE rb_yajl_json_ext_false_to_json(int argc, VALUE * argv, VALUE self);
125
+ static VALUE rb_yajl_json_ext_nil_to_json(int argc, VALUE * argv, VALUE self);
126
+ static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass);
127
+
128
+ void Init_yajl_ext();
data/ext/yajl_gen.c ADDED
@@ -0,0 +1,317 @@
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
+ #include "api/yajl_gen.h"
34
+ #include "yajl_buf.h"
35
+ #include "yajl_encode.h"
36
+
37
+ #include <stdlib.h>
38
+ #include <string.h>
39
+ #include <stdio.h>
40
+ #include <math.h>
41
+
42
+ typedef enum {
43
+ yajl_gen_start,
44
+ yajl_gen_map_start,
45
+ yajl_gen_map_key,
46
+ yajl_gen_map_val,
47
+ yajl_gen_array_start,
48
+ yajl_gen_in_array,
49
+ yajl_gen_complete,
50
+ yajl_gen_error
51
+ } yajl_gen_state;
52
+
53
+ struct yajl_gen_t
54
+ {
55
+ unsigned int depth;
56
+ unsigned int pretty;
57
+ const char * indentString;
58
+ yajl_gen_state state[YAJL_MAX_DEPTH];
59
+ yajl_print_t print;
60
+ void * ctx; /* yajl_buf */
61
+ /* memory allocation routines */
62
+ yajl_alloc_funcs alloc;
63
+ };
64
+
65
+ yajl_gen
66
+ yajl_gen_alloc(const yajl_gen_config * config,
67
+ const yajl_alloc_funcs * afs)
68
+ {
69
+ return yajl_gen_alloc2(NULL, config, afs, NULL);
70
+ }
71
+
72
+ yajl_gen
73
+ yajl_gen_alloc2(const yajl_print_t callback,
74
+ const yajl_gen_config * config,
75
+ const yajl_alloc_funcs * afs,
76
+ void * ctx)
77
+ {
78
+ yajl_gen g = NULL;
79
+ yajl_alloc_funcs afsBuffer;
80
+
81
+ /* first order of business is to set up memory allocation routines */
82
+ if (afs != NULL) {
83
+ if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
84
+ {
85
+ return NULL;
86
+ }
87
+ } else {
88
+ yajl_set_default_alloc_funcs(&afsBuffer);
89
+ afs = &afsBuffer;
90
+ }
91
+
92
+ g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t));
93
+ memset((void *) g, 0, sizeof(struct yajl_gen_t));
94
+ /* copy in pointers to allocation routines */
95
+ memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
96
+
97
+ if (config) {
98
+ g->pretty = config->beautify;
99
+ g->indentString = config->indentString ? config->indentString : " ";
100
+ }
101
+
102
+ if (callback) {
103
+ g->print = callback;
104
+ g->ctx = ctx;
105
+ } else {
106
+ g->print = (yajl_print_t)&yajl_buf_append;
107
+ g->ctx = yajl_buf_alloc(&(g->alloc));
108
+ }
109
+
110
+ return g;
111
+ }
112
+
113
+ void
114
+ yajl_gen_free(yajl_gen g)
115
+ {
116
+ if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_free((yajl_buf)g->ctx);
117
+ YA_FREE(&(g->alloc), g);
118
+ }
119
+
120
+ #define INSERT_SEP \
121
+ if (g->state[g->depth] == yajl_gen_map_key || \
122
+ g->state[g->depth] == yajl_gen_in_array) { \
123
+ g->print(g->ctx, ",", 1); \
124
+ if (g->pretty) g->print(g->ctx, "\n", 1); \
125
+ } else if (g->state[g->depth] == yajl_gen_map_val) { \
126
+ g->print(g->ctx, ":", 1); \
127
+ if (g->pretty) g->print(g->ctx, " ", 1); \
128
+ }
129
+
130
+ #define INSERT_WHITESPACE \
131
+ if (g->pretty) { \
132
+ if (g->state[g->depth] != yajl_gen_map_val) { \
133
+ unsigned int _i; \
134
+ for (_i=0;_i<g->depth;_i++) \
135
+ g->print(g->ctx, g->indentString, \
136
+ strlen(g->indentString)); \
137
+ } \
138
+ }
139
+
140
+ #define ENSURE_NOT_KEY \
141
+ if (g->state[g->depth] == yajl_gen_map_key) { \
142
+ return yajl_gen_keys_must_be_strings; \
143
+ } \
144
+
145
+ /* check that we're not complete, or in error state. in a valid state
146
+ * to be generating */
147
+ #define ENSURE_VALID_STATE \
148
+ if (g->state[g->depth] == yajl_gen_error) { \
149
+ return yajl_gen_in_error_state;\
150
+ } else if (g->state[g->depth] == yajl_gen_complete) { \
151
+ return yajl_gen_generation_complete; \
152
+ }
153
+
154
+ #define INCREMENT_DEPTH \
155
+ if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
156
+
157
+ #define APPENDED_ATOM \
158
+ switch (g->state[g->depth]) { \
159
+ case yajl_gen_map_start: \
160
+ case yajl_gen_map_key: \
161
+ g->state[g->depth] = yajl_gen_map_val; \
162
+ break; \
163
+ case yajl_gen_array_start: \
164
+ g->state[g->depth] = yajl_gen_in_array; \
165
+ break; \
166
+ case yajl_gen_map_val: \
167
+ g->state[g->depth] = yajl_gen_map_key; \
168
+ break; \
169
+ default: \
170
+ break; \
171
+ } \
172
+
173
+ #define FINAL_NEWLINE
174
+
175
+ yajl_gen_status
176
+ yajl_gen_integer(yajl_gen g, long int number)
177
+ {
178
+ char i[32];
179
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
180
+ sprintf(i, "%ld", number);
181
+ g->print(g->ctx, i, strlen(i));
182
+ APPENDED_ATOM;
183
+ FINAL_NEWLINE;
184
+ return yajl_gen_status_ok;
185
+ }
186
+
187
+ #ifdef WIN32
188
+ #include <float.h>
189
+ #define isnan _isnan
190
+ #define isinf !_finite
191
+ #endif
192
+
193
+ yajl_gen_status
194
+ yajl_gen_double(yajl_gen g, double number)
195
+ {
196
+ char i[32];
197
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY;
198
+ if (isnan(number) || isinf(number)) return yajl_gen_invalid_number;
199
+ INSERT_SEP; INSERT_WHITESPACE;
200
+ sprintf(i, "%g", number);
201
+ g->print(g->ctx, i, strlen(i));
202
+ APPENDED_ATOM;
203
+ FINAL_NEWLINE;
204
+ return yajl_gen_status_ok;
205
+ }
206
+
207
+ yajl_gen_status
208
+ yajl_gen_number(yajl_gen g, const char * s, unsigned int l)
209
+ {
210
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
211
+ g->print(g->ctx, s, l);
212
+ APPENDED_ATOM;
213
+ FINAL_NEWLINE;
214
+ return yajl_gen_status_ok;
215
+ }
216
+
217
+ yajl_gen_status
218
+ yajl_gen_string(yajl_gen g, const unsigned char * str,
219
+ unsigned int len)
220
+ {
221
+ ENSURE_VALID_STATE; INSERT_SEP; INSERT_WHITESPACE;
222
+ g->print(g->ctx, "\"", 1);
223
+ yajl_string_encode2(g->print, g->ctx, str, len);
224
+ g->print(g->ctx, "\"", 1);
225
+ APPENDED_ATOM;
226
+ FINAL_NEWLINE;
227
+ return yajl_gen_status_ok;
228
+ }
229
+
230
+ yajl_gen_status
231
+ yajl_gen_null(yajl_gen g)
232
+ {
233
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
234
+ g->print(g->ctx, "null", strlen("null"));
235
+ APPENDED_ATOM;
236
+ FINAL_NEWLINE;
237
+ return yajl_gen_status_ok;
238
+ }
239
+
240
+ yajl_gen_status
241
+ yajl_gen_bool(yajl_gen g, int boolean)
242
+ {
243
+ const char * val = boolean ? "true" : "false";
244
+
245
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
246
+ g->print(g->ctx, val, strlen(val));
247
+ APPENDED_ATOM;
248
+ FINAL_NEWLINE;
249
+ return yajl_gen_status_ok;
250
+ }
251
+
252
+ yajl_gen_status
253
+ yajl_gen_map_open(yajl_gen g)
254
+ {
255
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
256
+ INCREMENT_DEPTH;
257
+
258
+ g->state[g->depth] = yajl_gen_map_start;
259
+ g->print(g->ctx, "{", 1);
260
+ if (g->pretty) g->print(g->ctx, "\n", 1);
261
+ FINAL_NEWLINE;
262
+ return yajl_gen_status_ok;
263
+ }
264
+
265
+ yajl_gen_status
266
+ yajl_gen_map_close(yajl_gen g)
267
+ {
268
+ ENSURE_VALID_STATE;
269
+ (g->depth)--;
270
+ if (g->pretty) g->print(g->ctx, "\n", 1);
271
+ APPENDED_ATOM;
272
+ INSERT_WHITESPACE;
273
+ g->print(g->ctx, "}", 1);
274
+ FINAL_NEWLINE;
275
+ return yajl_gen_status_ok;
276
+ }
277
+
278
+ yajl_gen_status
279
+ yajl_gen_array_open(yajl_gen g)
280
+ {
281
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
282
+ INCREMENT_DEPTH;
283
+ g->state[g->depth] = yajl_gen_array_start;
284
+ g->print(g->ctx, "[", 1);
285
+ if (g->pretty) g->print(g->ctx, "\n", 1);
286
+ FINAL_NEWLINE;
287
+ return yajl_gen_status_ok;
288
+ }
289
+
290
+ yajl_gen_status
291
+ yajl_gen_array_close(yajl_gen g)
292
+ {
293
+ ENSURE_VALID_STATE;
294
+ if (g->pretty) g->print(g->ctx, "\n", 1);
295
+ (g->depth)--;
296
+ APPENDED_ATOM;
297
+ INSERT_WHITESPACE;
298
+ g->print(g->ctx, "]", 1);
299
+ FINAL_NEWLINE;
300
+ return yajl_gen_status_ok;
301
+ }
302
+
303
+ yajl_gen_status
304
+ yajl_gen_get_buf(yajl_gen g, const unsigned char ** buf,
305
+ unsigned int * len)
306
+ {
307
+ if (g->print != (yajl_print_t)&yajl_buf_append) return yajl_gen_no_buf;
308
+ *buf = yajl_buf_data((yajl_buf)g->ctx);
309
+ *len = yajl_buf_len((yajl_buf)g->ctx);
310
+ return yajl_gen_status_ok;
311
+ }
312
+
313
+ void
314
+ yajl_gen_clear(yajl_gen g)
315
+ {
316
+ if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_clear((yajl_buf)g->ctx);
317
+ }