yajl-ruby 1.0.0-x86-mingw32

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,135 @@
1
+ /*
2
+ * Copyright (c) 2008-2011 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
+
27
+ // tell rbx not to use it's caching compat layer
28
+ // by doing this we're making a promize to RBX that
29
+ // we'll never modify the pointers we get back from RSTRING_PTR
30
+ #define RSTRING_NOT_MODIFIED
31
+
32
+ #include <ruby.h>
33
+
34
+ #ifdef HAVE_RUBY_ENCODING_H
35
+ #include <ruby/encoding.h>
36
+ static rb_encoding *utf8Encoding;
37
+ #endif
38
+
39
+ #define READ_BUFSIZE 8192
40
+ #define WRITE_BUFSIZE 8192
41
+
42
+ /* Older versions of Ruby (< 1.8.6) need these */
43
+ #ifndef RSTRING_PTR
44
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
45
+ #endif
46
+ #ifndef RSTRING_LEN
47
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
48
+ #endif
49
+ #ifndef RARRAY_PTR
50
+ #define RARRAY_PTR(s) (RARRAY(s)->ptr)
51
+ #endif
52
+ #ifndef RARRAY_LEN
53
+ #define RARRAY_LEN(s) (RARRAY(s)->len)
54
+ #endif
55
+
56
+ static VALUE cParseError, cEncodeError, mYajl, cParser, cEncoder;
57
+ static ID intern_io_read, intern_call, intern_keys, intern_to_s,
58
+ intern_to_json, intern_has_key, intern_to_sym, intern_as_json;
59
+ static ID sym_allow_comments, sym_check_utf8, sym_pretty, sym_indent, sym_terminator, sym_symbolize_keys, sym_html_safe;
60
+
61
+ #define GetParser(obj, sval) (sval = (yajl_parser_wrapper*)DATA_PTR(obj));
62
+ #define GetEncoder(obj, sval) (sval = (yajl_encoder_wrapper*)DATA_PTR(obj));
63
+
64
+ inline void yajl_check_and_fire_callback(void * ctx);
65
+ inline void yajl_set_static_value(void * ctx, VALUE val);
66
+ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io);
67
+ void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle parser);
68
+
69
+ static int yajl_found_null(void * ctx);
70
+ static int yajl_found_boolean(void * ctx, int boolean);
71
+ static int yajl_found_number(void * ctx, const char * numberVal, unsigned int numberLen);
72
+ static int yajl_found_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen);
73
+ static int yajl_found_hash_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen);
74
+ static int yajl_found_start_hash(void * ctx);
75
+ static int yajl_found_end_hash(void * ctx);
76
+ static int yajl_found_start_array(void * ctx);
77
+ static int yajl_found_end_array(void * ctx);
78
+
79
+ static yajl_callbacks callbacks = {
80
+ yajl_found_null,
81
+ yajl_found_boolean,
82
+ NULL,
83
+ NULL,
84
+ yajl_found_number,
85
+ yajl_found_string,
86
+ yajl_found_start_hash,
87
+ yajl_found_hash_key,
88
+ yajl_found_end_hash,
89
+ yajl_found_start_array,
90
+ yajl_found_end_array
91
+ };
92
+
93
+ typedef struct {
94
+ VALUE builderStack;
95
+ VALUE parse_complete_callback;
96
+ int nestedArrayLevel;
97
+ int nestedHashLevel;
98
+ int objectsFound;
99
+ int symbolizeKeys;
100
+ yajl_handle parser;
101
+ } yajl_parser_wrapper;
102
+
103
+ typedef struct {
104
+ VALUE on_progress_callback;
105
+ VALUE terminator;
106
+ yajl_gen encoder;
107
+ unsigned char *indentString;
108
+ } yajl_encoder_wrapper;
109
+
110
+ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE self);
111
+ static VALUE rb_yajl_parser_init(int argc, VALUE * argv, VALUE self);
112
+ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self);
113
+ static VALUE rb_yajl_parser_parse_chunk(VALUE self, VALUE chunk);
114
+ static VALUE rb_yajl_parser_set_complete_cb(VALUE self, VALUE callback);
115
+ static void yajl_parser_wrapper_free(void * wrapper);
116
+ static void yajl_parser_wrapper_mark(void * wrapper);
117
+
118
+ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass);
119
+ static VALUE rb_yajl_encoder_init(int argc, VALUE * argv, VALUE self);
120
+ static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self);
121
+ static VALUE rb_yajl_encoder_set_progress_cb(VALUE self, VALUE callback);
122
+ static void yajl_encoder_wrapper_free(void * wrapper);
123
+ static void yajl_encoder_wrapper_mark(void * wrapper);
124
+
125
+ static VALUE rb_yajl_json_ext_hash_to_json(int argc, VALUE * argv, VALUE self);
126
+ static VALUE rb_yajl_json_ext_array_to_json(int argc, VALUE * argv, VALUE self);
127
+ static VALUE rb_yajl_json_ext_fixnum_to_json(int argc, VALUE * argv, VALUE self);
128
+ static VALUE rb_yajl_json_ext_float_to_json(int argc, VALUE * argv, VALUE self);
129
+ static VALUE rb_yajl_json_ext_string_to_json(int argc, VALUE * argv, VALUE self);
130
+ static VALUE rb_yajl_json_ext_true_to_json(int argc, VALUE * argv, VALUE self);
131
+ static VALUE rb_yajl_json_ext_false_to_json(int argc, VALUE * argv, VALUE self);
132
+ static VALUE rb_yajl_json_ext_nil_to_json(int argc, VALUE * argv, VALUE self);
133
+ static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass);
134
+
135
+ void Init_yajl();
@@ -0,0 +1,344 @@
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
+ unsigned int htmlSafe;
64
+ };
65
+
66
+ yajl_gen
67
+ yajl_gen_alloc(const yajl_gen_config * config,
68
+ const yajl_alloc_funcs * afs)
69
+ {
70
+ return yajl_gen_alloc2(NULL, config, afs, NULL);
71
+ }
72
+
73
+ yajl_gen
74
+ yajl_gen_alloc2(const yajl_print_t callback,
75
+ const yajl_gen_config * config,
76
+ const yajl_alloc_funcs * afs,
77
+ void * ctx)
78
+ {
79
+ yajl_gen g = NULL;
80
+ yajl_alloc_funcs afsBuffer;
81
+
82
+ /* first order of business is to set up memory allocation routines */
83
+ if (afs != NULL) {
84
+ if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
85
+ {
86
+ return NULL;
87
+ }
88
+ } else {
89
+ yajl_set_default_alloc_funcs(&afsBuffer);
90
+ afs = &afsBuffer;
91
+ }
92
+
93
+ g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t));
94
+ if (!g) return NULL;
95
+
96
+ memset((void *) g, 0, sizeof(struct yajl_gen_t));
97
+ /* copy in pointers to allocation routines */
98
+ memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
99
+
100
+ if (config) {
101
+ const char *indent = config->indentString;
102
+ g->pretty = config->beautify;
103
+ g->indentString = config->indentString;
104
+ if (indent) {
105
+ for (; *indent; indent++) {
106
+ if (*indent != '\n'
107
+ && *indent != '\v'
108
+ && *indent != '\f'
109
+ && *indent != '\t'
110
+ && *indent != '\r'
111
+ && *indent != ' ') {
112
+ g->indentString = NULL;
113
+ break;
114
+ }
115
+ }
116
+ }
117
+ if (!g->indentString) {
118
+ g->indentString = " ";
119
+ }
120
+ g->htmlSafe = config->htmlSafe;
121
+ }
122
+
123
+ if (callback) {
124
+ g->print = callback;
125
+ g->ctx = ctx;
126
+ } else {
127
+ g->print = (yajl_print_t)&yajl_buf_append;
128
+ g->ctx = yajl_buf_alloc(&(g->alloc));
129
+ }
130
+
131
+ return g;
132
+ }
133
+
134
+ void
135
+ yajl_gen_free(yajl_gen g)
136
+ {
137
+ if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_free((yajl_buf)g->ctx);
138
+ YA_FREE(&(g->alloc), g);
139
+ }
140
+
141
+ #define INSERT_SEP \
142
+ if (g->state[g->depth] == yajl_gen_map_key || \
143
+ g->state[g->depth] == yajl_gen_in_array) { \
144
+ g->print(g->ctx, ",", 1); \
145
+ if (g->pretty) g->print(g->ctx, "\n", 1); \
146
+ } else if (g->state[g->depth] == yajl_gen_map_val) { \
147
+ g->print(g->ctx, ":", 1); \
148
+ if (g->pretty) g->print(g->ctx, " ", 1); \
149
+ }
150
+
151
+ #define INSERT_WHITESPACE \
152
+ if (g->pretty) { \
153
+ if (g->state[g->depth] != yajl_gen_map_val) { \
154
+ unsigned int _i; \
155
+ for (_i=0;_i<g->depth;_i++) \
156
+ g->print(g->ctx, \
157
+ g->indentString, \
158
+ (unsigned int)strlen(g->indentString)); \
159
+ } \
160
+ }
161
+
162
+ #define ENSURE_NOT_KEY \
163
+ if (g->state[g->depth] == yajl_gen_map_key || \
164
+ g->state[g->depth] == yajl_gen_map_start) { \
165
+ return yajl_gen_keys_must_be_strings; \
166
+ } \
167
+
168
+ /* check that we're not complete, or in error state. in a valid state
169
+ * to be generating */
170
+ #define ENSURE_VALID_STATE \
171
+ if (g->state[g->depth] == yajl_gen_error) { \
172
+ return yajl_gen_in_error_state;\
173
+ } else if (g->state[g->depth] == yajl_gen_complete) { \
174
+ return yajl_gen_generation_complete; \
175
+ }
176
+
177
+ #define INCREMENT_DEPTH \
178
+ if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
179
+
180
+ #define DECREMENT_DEPTH \
181
+ if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_gen_error;
182
+
183
+ #define APPENDED_ATOM \
184
+ switch (g->state[g->depth]) { \
185
+ case yajl_gen_map_start: \
186
+ case yajl_gen_map_key: \
187
+ g->state[g->depth] = yajl_gen_map_val; \
188
+ break; \
189
+ case yajl_gen_array_start: \
190
+ g->state[g->depth] = yajl_gen_in_array; \
191
+ break; \
192
+ case yajl_gen_map_val: \
193
+ g->state[g->depth] = yajl_gen_map_key; \
194
+ break; \
195
+ default: \
196
+ break; \
197
+ } \
198
+
199
+ #define FINAL_NEWLINE
200
+
201
+ yajl_gen_status
202
+ yajl_gen_integer(yajl_gen g, long int number)
203
+ {
204
+ char i[32];
205
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
206
+ sprintf(i, "%ld", number);
207
+ g->print(g->ctx, i, (unsigned int)strlen(i));
208
+ APPENDED_ATOM;
209
+ FINAL_NEWLINE;
210
+ return yajl_gen_status_ok;
211
+ }
212
+
213
+ #ifdef WIN32
214
+ #include <float.h>
215
+ #define isnan _isnan
216
+ #define isinf !_finite
217
+ #endif
218
+
219
+ yajl_gen_status
220
+ yajl_gen_double(yajl_gen g, double number)
221
+ {
222
+ char i[32];
223
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY;
224
+ if (isnan(number) || isinf(number)) return yajl_gen_invalid_number;
225
+ INSERT_SEP; INSERT_WHITESPACE;
226
+ sprintf(i, "%.20g", number);
227
+ g->print(g->ctx, i, (unsigned int)strlen(i));
228
+ APPENDED_ATOM;
229
+ FINAL_NEWLINE;
230
+ return yajl_gen_status_ok;
231
+ }
232
+
233
+ yajl_gen_status
234
+ yajl_gen_number(yajl_gen g, const char * s, unsigned int l)
235
+ {
236
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
237
+ g->print(g->ctx, s, l);
238
+ APPENDED_ATOM;
239
+ FINAL_NEWLINE;
240
+ return yajl_gen_status_ok;
241
+ }
242
+
243
+ yajl_gen_status
244
+ yajl_gen_string(yajl_gen g, const unsigned char * str,
245
+ unsigned int len)
246
+ {
247
+ ENSURE_VALID_STATE; INSERT_SEP; INSERT_WHITESPACE;
248
+ g->print(g->ctx, "\"", 1);
249
+ yajl_string_encode2(g->print, g->ctx, str, len, g->htmlSafe);
250
+ g->print(g->ctx, "\"", 1);
251
+ APPENDED_ATOM;
252
+ FINAL_NEWLINE;
253
+ return yajl_gen_status_ok;
254
+ }
255
+
256
+ yajl_gen_status
257
+ yajl_gen_null(yajl_gen g)
258
+ {
259
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
260
+ g->print(g->ctx, "null", strlen("null"));
261
+ APPENDED_ATOM;
262
+ FINAL_NEWLINE;
263
+ return yajl_gen_status_ok;
264
+ }
265
+
266
+ yajl_gen_status
267
+ yajl_gen_bool(yajl_gen g, int boolean)
268
+ {
269
+ const char * val = boolean ? "true" : "false";
270
+
271
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
272
+ g->print(g->ctx, val, (unsigned int)strlen(val));
273
+ APPENDED_ATOM;
274
+ FINAL_NEWLINE;
275
+ return yajl_gen_status_ok;
276
+ }
277
+
278
+ yajl_gen_status
279
+ yajl_gen_map_open(yajl_gen g)
280
+ {
281
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
282
+ INCREMENT_DEPTH;
283
+
284
+ g->state[g->depth] = yajl_gen_map_start;
285
+ g->print(g->ctx, "{", 1);
286
+ if (g->pretty) g->print(g->ctx, "\n", 1);
287
+ FINAL_NEWLINE;
288
+ return yajl_gen_status_ok;
289
+ }
290
+
291
+ yajl_gen_status
292
+ yajl_gen_map_close(yajl_gen g)
293
+ {
294
+ ENSURE_VALID_STATE;
295
+ DECREMENT_DEPTH;
296
+
297
+ if (g->pretty) g->print(g->ctx, "\n", 1);
298
+ APPENDED_ATOM;
299
+ INSERT_WHITESPACE;
300
+ g->print(g->ctx, "}", 1);
301
+ FINAL_NEWLINE;
302
+ return yajl_gen_status_ok;
303
+ }
304
+
305
+ yajl_gen_status
306
+ yajl_gen_array_open(yajl_gen g)
307
+ {
308
+ ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
309
+ INCREMENT_DEPTH;
310
+ g->state[g->depth] = yajl_gen_array_start;
311
+ g->print(g->ctx, "[", 1);
312
+ if (g->pretty) g->print(g->ctx, "\n", 1);
313
+ FINAL_NEWLINE;
314
+ return yajl_gen_status_ok;
315
+ }
316
+
317
+ yajl_gen_status
318
+ yajl_gen_array_close(yajl_gen g)
319
+ {
320
+ ENSURE_VALID_STATE;
321
+ DECREMENT_DEPTH;
322
+ if (g->pretty) g->print(g->ctx, "\n", 1);
323
+ APPENDED_ATOM;
324
+ INSERT_WHITESPACE;
325
+ g->print(g->ctx, "]", 1);
326
+ FINAL_NEWLINE;
327
+ return yajl_gen_status_ok;
328
+ }
329
+
330
+ yajl_gen_status
331
+ yajl_gen_get_buf(yajl_gen g, const unsigned char ** buf,
332
+ unsigned int * len)
333
+ {
334
+ if (g->print != (yajl_print_t)&yajl_buf_append) return yajl_gen_no_buf;
335
+ *buf = yajl_buf_data((yajl_buf)g->ctx);
336
+ *len = yajl_buf_len((yajl_buf)g->ctx);
337
+ return yajl_gen_status_ok;
338
+ }
339
+
340
+ void
341
+ yajl_gen_clear(yajl_gen g)
342
+ {
343
+ if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_clear((yajl_buf)g->ctx);
344
+ }