yajl-ruby 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. data/.gitignore +5 -0
  2. data/CHANGELOG.md +164 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +264 -0
  5. data/Rakefile +29 -0
  6. data/VERSION.yml +4 -0
  7. data/benchmark/encode.rb +46 -0
  8. data/benchmark/encode_json_and_marshal.rb +35 -0
  9. data/benchmark/encode_json_and_yaml.rb +47 -0
  10. data/benchmark/http.rb +30 -0
  11. data/benchmark/parse.rb +49 -0
  12. data/benchmark/parse_json_and_marshal.rb +47 -0
  13. data/benchmark/parse_json_and_yaml.rb +56 -0
  14. data/benchmark/parse_stream.rb +48 -0
  15. data/benchmark/subjects/contacts.json +1 -0
  16. data/benchmark/subjects/contacts.marshal_dump +0 -0
  17. data/benchmark/subjects/contacts.yml +114685 -0
  18. data/benchmark/subjects/item.json +1 -0
  19. data/benchmark/subjects/ohai.json +1216 -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/http/twitter_search_api.rb +15 -0
  24. data/examples/http/twitter_stream_api.rb +25 -0
  25. data/examples/parsing/from_file.rb +14 -0
  26. data/examples/parsing/from_stdin.rb +9 -0
  27. data/examples/parsing/from_string.rb +15 -0
  28. data/ext/api/yajl_common.h +85 -0
  29. data/ext/api/yajl_gen.h +123 -0
  30. data/ext/api/yajl_parse.h +182 -0
  31. data/ext/extconf.rb +8 -0
  32. data/ext/yajl.c +157 -0
  33. data/ext/yajl_alloc.c +65 -0
  34. data/ext/yajl_alloc.h +50 -0
  35. data/ext/yajl_buf.c +119 -0
  36. data/ext/yajl_buf.h +73 -0
  37. data/ext/yajl_bytestack.h +85 -0
  38. data/ext/yajl_encode.c +179 -0
  39. data/ext/yajl_encode.h +44 -0
  40. data/ext/yajl_ext.c +774 -0
  41. data/ext/yajl_ext.h +74 -0
  42. data/ext/yajl_gen.c +290 -0
  43. data/ext/yajl_lex.c +744 -0
  44. data/ext/yajl_lex.h +135 -0
  45. data/ext/yajl_parser.c +447 -0
  46. data/ext/yajl_parser.h +79 -0
  47. data/lib/yajl.rb +80 -0
  48. data/lib/yajl/bzip2.rb +11 -0
  49. data/lib/yajl/bzip2/stream_reader.rb +29 -0
  50. data/lib/yajl/bzip2/stream_writer.rb +15 -0
  51. data/lib/yajl/deflate.rb +6 -0
  52. data/lib/yajl/deflate/stream_reader.rb +37 -0
  53. data/lib/yajl/deflate/stream_writer.rb +21 -0
  54. data/lib/yajl/gzip.rb +6 -0
  55. data/lib/yajl/gzip/stream_reader.rb +28 -0
  56. data/lib/yajl/gzip/stream_writer.rb +14 -0
  57. data/lib/yajl/http_stream.rb +101 -0
  58. data/lib/yajl/json_gem.rb +69 -0
  59. data/spec/encoding/encoding_spec.rb +186 -0
  60. data/spec/http/fixtures/http.bzip2.dump +0 -0
  61. data/spec/http/fixtures/http.deflate.dump +0 -0
  62. data/spec/http/fixtures/http.gzip.dump +0 -0
  63. data/spec/http/fixtures/http.raw.dump +12 -0
  64. data/spec/http/http_spec.rb +94 -0
  65. data/spec/json_gem_compatibility/compatibility_spec.rb +170 -0
  66. data/spec/parsing/active_support_spec.rb +68 -0
  67. data/spec/parsing/chunked_spec.rb +98 -0
  68. data/spec/parsing/fixtures/fail.15.json +1 -0
  69. data/spec/parsing/fixtures/fail.16.json +1 -0
  70. data/spec/parsing/fixtures/fail.17.json +1 -0
  71. data/spec/parsing/fixtures/fail.26.json +1 -0
  72. data/spec/parsing/fixtures/fail11.json +1 -0
  73. data/spec/parsing/fixtures/fail12.json +1 -0
  74. data/spec/parsing/fixtures/fail13.json +1 -0
  75. data/spec/parsing/fixtures/fail14.json +1 -0
  76. data/spec/parsing/fixtures/fail19.json +1 -0
  77. data/spec/parsing/fixtures/fail20.json +1 -0
  78. data/spec/parsing/fixtures/fail21.json +1 -0
  79. data/spec/parsing/fixtures/fail22.json +1 -0
  80. data/spec/parsing/fixtures/fail23.json +1 -0
  81. data/spec/parsing/fixtures/fail24.json +1 -0
  82. data/spec/parsing/fixtures/fail25.json +1 -0
  83. data/spec/parsing/fixtures/fail27.json +2 -0
  84. data/spec/parsing/fixtures/fail28.json +2 -0
  85. data/spec/parsing/fixtures/fail3.json +1 -0
  86. data/spec/parsing/fixtures/fail4.json +1 -0
  87. data/spec/parsing/fixtures/fail5.json +1 -0
  88. data/spec/parsing/fixtures/fail6.json +1 -0
  89. data/spec/parsing/fixtures/fail9.json +1 -0
  90. data/spec/parsing/fixtures/pass.array.json +6 -0
  91. data/spec/parsing/fixtures/pass.codepoints_from_unicode_org.json +1 -0
  92. data/spec/parsing/fixtures/pass.contacts.json +1 -0
  93. data/spec/parsing/fixtures/pass.db100.xml.json +1 -0
  94. data/spec/parsing/fixtures/pass.db1000.xml.json +1 -0
  95. data/spec/parsing/fixtures/pass.dc_simple_with_comments.json +11 -0
  96. data/spec/parsing/fixtures/pass.deep_arrays.json +1 -0
  97. data/spec/parsing/fixtures/pass.difficult_json_c_test_case.json +1 -0
  98. data/spec/parsing/fixtures/pass.difficult_json_c_test_case_with_comments.json +1 -0
  99. data/spec/parsing/fixtures/pass.doubles.json +1 -0
  100. data/spec/parsing/fixtures/pass.empty_array.json +1 -0
  101. data/spec/parsing/fixtures/pass.empty_string.json +1 -0
  102. data/spec/parsing/fixtures/pass.escaped_bulgarian.json +4 -0
  103. data/spec/parsing/fixtures/pass.escaped_foobar.json +1 -0
  104. data/spec/parsing/fixtures/pass.item.json +1 -0
  105. data/spec/parsing/fixtures/pass.json-org-sample1.json +23 -0
  106. data/spec/parsing/fixtures/pass.json-org-sample2.json +11 -0
  107. data/spec/parsing/fixtures/pass.json-org-sample3.json +26 -0
  108. data/spec/parsing/fixtures/pass.json-org-sample4-nows.json +88 -0
  109. data/spec/parsing/fixtures/pass.json-org-sample4.json +89 -0
  110. data/spec/parsing/fixtures/pass.json-org-sample5.json +27 -0
  111. data/spec/parsing/fixtures/pass.map-spain.xml.json +1 -0
  112. data/spec/parsing/fixtures/pass.ns-invoice100.xml.json +1 -0
  113. data/spec/parsing/fixtures/pass.ns-soap.xml.json +1 -0
  114. data/spec/parsing/fixtures/pass.numbers-fp-4k.json +6 -0
  115. data/spec/parsing/fixtures/pass.numbers-fp-64k.json +61 -0
  116. data/spec/parsing/fixtures/pass.numbers-int-4k.json +11 -0
  117. data/spec/parsing/fixtures/pass.numbers-int-64k.json +154 -0
  118. data/spec/parsing/fixtures/pass.twitter-search.json +1 -0
  119. data/spec/parsing/fixtures/pass.twitter-search2.json +1 -0
  120. data/spec/parsing/fixtures/pass.unicode.json +3315 -0
  121. data/spec/parsing/fixtures/pass.yelp.json +1 -0
  122. data/spec/parsing/fixtures/pass1.json +56 -0
  123. data/spec/parsing/fixtures/pass2.json +1 -0
  124. data/spec/parsing/fixtures/pass3.json +6 -0
  125. data/spec/parsing/fixtures_spec.rb +45 -0
  126. data/spec/parsing/one_off_spec.rb +58 -0
  127. data/spec/spec_helper.rb +11 -0
  128. data/yajl-ruby.gemspec +176 -0
  129. metadata +196 -0
@@ -0,0 +1,85 @@
1
+ /*
2
+ * Copyright 2009, 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
+ * A header only implementation of a simple stack of bytes, used in YAJL
35
+ * to maintain parse state.
36
+ */
37
+
38
+ #ifndef __YAJL_BYTESTACK_H__
39
+ #define __YAJL_BYTESTACK_H__
40
+
41
+ #include "api/yajl_common.h"
42
+
43
+ #define YAJL_BS_INC 128
44
+
45
+ typedef struct yajl_bytestack_t
46
+ {
47
+ unsigned char * stack;
48
+ unsigned int size;
49
+ unsigned int used;
50
+ yajl_alloc_funcs * yaf;
51
+ } yajl_bytestack;
52
+
53
+ /* initialize a bytestack */
54
+ #define yajl_bs_init(obs, _yaf) { \
55
+ (obs).stack = NULL; \
56
+ (obs).size = 0; \
57
+ (obs).used = 0; \
58
+ (obs).yaf = (_yaf); \
59
+ } \
60
+
61
+
62
+ /* initialize a bytestack */
63
+ #define yajl_bs_free(obs) \
64
+ if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
65
+
66
+ #define yajl_bs_current(obs) \
67
+ (assert((obs).used > 0), (obs).stack[(obs).used - 1])
68
+
69
+ #define yajl_bs_push(obs, byte) { \
70
+ if (((obs).size - (obs).used) == 0) { \
71
+ (obs).size += YAJL_BS_INC; \
72
+ (obs).stack = (obs).yaf->realloc((obs).yaf->ctx,\
73
+ (void *) (obs).stack, (obs).size);\
74
+ } \
75
+ (obs).stack[((obs).used)++] = (byte); \
76
+ }
77
+
78
+ /* removes the top item of the stack, returns nothing */
79
+ #define yajl_bs_pop(obs) { ((obs).used)--; }
80
+
81
+ #define yajl_bs_set(obs, byte) \
82
+ (obs).stack[((obs).used) - 1] = (byte);
83
+
84
+
85
+ #endif
data/ext/yajl_encode.c ADDED
@@ -0,0 +1,179 @@
1
+ /*
2
+ * Copyright 2007-2009, 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 "yajl_encode.h"
34
+
35
+ #include <assert.h>
36
+ #include <stdlib.h>
37
+ #include <string.h>
38
+ #include <stdio.h>
39
+
40
+ static void CharToHex(unsigned char c, char * hexBuf)
41
+ {
42
+ const char * hexchar = "0123456789ABCDEF";
43
+ hexBuf[0] = hexchar[c >> 4];
44
+ hexBuf[1] = hexchar[c & 0x0F];
45
+ }
46
+
47
+ void
48
+ yajl_string_encode(yajl_buf buf, const unsigned char * str,
49
+ unsigned int len)
50
+ {
51
+ unsigned int beg = 0;
52
+ unsigned int end = 0;
53
+ char hexBuf[7];
54
+ hexBuf[0] = '\\'; hexBuf[1] = 'u'; hexBuf[2] = '0'; hexBuf[3] = '0';
55
+ hexBuf[6] = 0;
56
+
57
+ while (end < len) {
58
+ const char * escaped = NULL;
59
+ switch (str[end]) {
60
+ case '\r': escaped = "\\r"; break;
61
+ case '\n': escaped = "\\n"; break;
62
+ case '\\': escaped = "\\\\"; break;
63
+ /* case '/': escaped = "\\/"; break; */
64
+ case '"': escaped = "\\\""; break;
65
+ case '\f': escaped = "\\f"; break;
66
+ case '\b': escaped = "\\b"; break;
67
+ case '\t': escaped = "\\t"; break;
68
+ default:
69
+ if ((unsigned char) str[end] < 32) {
70
+ CharToHex(str[end], hexBuf + 4);
71
+ escaped = hexBuf;
72
+ }
73
+ break;
74
+ }
75
+ if (escaped != NULL) {
76
+ yajl_buf_append(buf, str + beg, end - beg);
77
+ yajl_buf_append(buf, escaped, strlen(escaped));
78
+ beg = ++end;
79
+ } else {
80
+ ++end;
81
+ }
82
+ }
83
+ yajl_buf_append(buf, str + beg, end - beg);
84
+ }
85
+
86
+ static void hexToDigit(unsigned int * val, const unsigned char * hex)
87
+ {
88
+ unsigned int i;
89
+ for (i=0;i<4;i++) {
90
+ unsigned char c = hex[i];
91
+ if (c >= 'A') c = (c & ~0x20) - 7;
92
+ c -= '0';
93
+ assert(!(c & 0xF0));
94
+ *val = (*val << 4) | c;
95
+ }
96
+ }
97
+
98
+ static void Utf32toUtf8(unsigned int codepoint, char * utf8Buf)
99
+ {
100
+ if (codepoint < 0x80) {
101
+ utf8Buf[0] = (char) codepoint;
102
+ utf8Buf[1] = 0;
103
+ } else if (codepoint < 0x0800) {
104
+ utf8Buf[0] = (char) ((codepoint >> 6) | 0xC0);
105
+ utf8Buf[1] = (char) ((codepoint & 0x3F) | 0x80);
106
+ utf8Buf[2] = 0;
107
+ } else if (codepoint < 0x10000) {
108
+ utf8Buf[0] = (char) ((codepoint >> 12) | 0xE0);
109
+ utf8Buf[1] = (char) (((codepoint >> 6) & 0x3F) | 0x80);
110
+ utf8Buf[2] = (char) ((codepoint & 0x3F) | 0x80);
111
+ utf8Buf[3] = 0;
112
+ } else if (codepoint < 0x200000) {
113
+ utf8Buf[0] =(char)((codepoint >> 18) | 0xF0);
114
+ utf8Buf[1] =(char)(((codepoint >> 12) & 0x3F) | 0x80);
115
+ utf8Buf[2] =(char)(((codepoint >> 6) & 0x3F) | 0x80);
116
+ utf8Buf[3] =(char)((codepoint & 0x3F) | 0x80);
117
+ utf8Buf[4] = 0;
118
+ } else {
119
+ utf8Buf[0] = '?';
120
+ utf8Buf[1] = 0;
121
+ }
122
+ }
123
+
124
+ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
125
+ unsigned int len)
126
+ {
127
+ unsigned int beg = 0;
128
+ unsigned int end = 0;
129
+
130
+ while (end < len) {
131
+ if (str[end] == '\\') {
132
+ char utf8Buf[5];
133
+ const char * unescaped = "?";
134
+ yajl_buf_append(buf, str + beg, end - beg);
135
+ switch (str[++end]) {
136
+ case 'r': unescaped = "\r"; break;
137
+ case 'n': unescaped = "\n"; break;
138
+ case '\\': unescaped = "\\"; break;
139
+ case '/': unescaped = "/"; break;
140
+ case '"': unescaped = "\""; break;
141
+ case 'f': unescaped = "\f"; break;
142
+ case 'b': unescaped = "\b"; break;
143
+ case 't': unescaped = "\t"; break;
144
+ case 'u': {
145
+ unsigned int codepoint = 0;
146
+ hexToDigit(&codepoint, str + ++end);
147
+ end+=3;
148
+ /* check if this is a surrogate */
149
+ if ((codepoint & 0xFC00) == 0xD800) {
150
+ end++;
151
+ if (str[end] == '\\' && str[end + 1] == 'u') {
152
+ unsigned int surrogate = 0;
153
+ hexToDigit(&surrogate, str + end + 2);
154
+ codepoint =
155
+ (((codepoint & 0x3F) << 10) |
156
+ ((((codepoint >> 6) & 0xF) + 1) << 16) |
157
+ (surrogate & 0x3FF));
158
+ end += 5;
159
+ } else {
160
+ unescaped = "?";
161
+ break;
162
+ }
163
+ }
164
+
165
+ Utf32toUtf8(codepoint, utf8Buf);
166
+ unescaped = utf8Buf;
167
+ break;
168
+ }
169
+ default:
170
+ assert("this should never happen" == NULL);
171
+ }
172
+ yajl_buf_append(buf, unescaped, strlen(unescaped));
173
+ beg = ++end;
174
+ } else {
175
+ end++;
176
+ }
177
+ }
178
+ yajl_buf_append(buf, str + beg, end - beg);
179
+ }
data/ext/yajl_encode.h ADDED
@@ -0,0 +1,44 @@
1
+ /*
2
+ * Copyright 2007-2009, 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_ENCODE_H__
34
+ #define __YAJL_ENCODE_H__
35
+
36
+ #include "yajl_buf.h"
37
+
38
+ void yajl_string_encode(yajl_buf buf, const unsigned char * str,
39
+ unsigned int length);
40
+
41
+ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
42
+ unsigned int length);
43
+
44
+ #endif
data/ext/yajl_ext.c ADDED
@@ -0,0 +1,774 @@
1
+ #include "yajl_ext.h"
2
+
3
+ // Helpers for building objects
4
+ inline void yajl_check_and_fire_callback(void * ctx) {
5
+ struct yajl_parser_wrapper * wrapper;
6
+ GetParser((VALUE)ctx, wrapper);
7
+
8
+ // No need to do any of this if the callback isn't even setup
9
+ if (wrapper->parse_complete_callback != Qnil) {
10
+ int len = RARRAY_LEN(wrapper->builderStack);
11
+ if (len == 1 && wrapper->nestedArrayLevel == 0 && wrapper->nestedHashLevel == 0) {
12
+ rb_funcall(wrapper->parse_complete_callback, intern_call, 1, rb_ary_pop(wrapper->builderStack));
13
+ }
14
+ } else {
15
+ int len = RARRAY_LEN(wrapper->builderStack);
16
+ if (len == 1 && wrapper->nestedArrayLevel == 0 && wrapper->nestedHashLevel == 0) {
17
+ wrapper->objectsFound++;
18
+ if (wrapper->objectsFound > 1) {
19
+ rb_raise(cParseError, "%s", "Found multiple JSON objects in the stream but no block or the on_parse_complete callback was assigned to handle them.");
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ inline void yajl_set_static_value(void * ctx, VALUE val) {
26
+ struct yajl_parser_wrapper * wrapper;
27
+ VALUE lastEntry, hash;
28
+ int len;
29
+
30
+ GetParser((VALUE)ctx, wrapper);
31
+
32
+ len = RARRAY_LEN(wrapper->builderStack);
33
+ if (len > 0) {
34
+ lastEntry = rb_ary_entry(wrapper->builderStack, len-1);
35
+ switch (TYPE(lastEntry)) {
36
+ case T_ARRAY:
37
+ rb_ary_push(lastEntry, val);
38
+ if (TYPE(val) == T_HASH || TYPE(val) == T_ARRAY) {
39
+ rb_ary_push(wrapper->builderStack, val);
40
+ }
41
+ break;
42
+ case T_HASH:
43
+ rb_hash_aset(lastEntry, val, Qnil);
44
+ rb_ary_push(wrapper->builderStack, val);
45
+ break;
46
+ case T_STRING:
47
+ case T_SYMBOL:
48
+ hash = rb_ary_entry(wrapper->builderStack, len-2);
49
+ if (TYPE(hash) == T_HASH) {
50
+ rb_hash_aset(hash, lastEntry, val);
51
+ rb_ary_pop(wrapper->builderStack);
52
+ if (TYPE(val) == T_HASH || TYPE(val) == T_ARRAY) {
53
+ rb_ary_push(wrapper->builderStack, val);
54
+ }
55
+ }
56
+ break;
57
+ }
58
+ } else {
59
+ rb_ary_push(wrapper->builderStack, val);
60
+ }
61
+ }
62
+
63
+ void yajl_encode_part(yajl_gen hand, VALUE obj, VALUE io) {
64
+ VALUE str, outBuff, otherObj;
65
+ yajl_gen_status status;
66
+ int idx = 0;
67
+ const unsigned char * buffer;
68
+ unsigned int len;
69
+
70
+ if (io != Qnil) {
71
+ status = yajl_gen_get_buf(hand, &buffer, &len);
72
+ if (len >= WRITE_BUFSIZE) {
73
+ outBuff = rb_str_new((const char *)buffer, len);
74
+ rb_io_write(io, outBuff);
75
+ yajl_gen_clear(hand);
76
+ }
77
+ }
78
+
79
+ switch (TYPE(obj)) {
80
+ case T_HASH:
81
+ status = yajl_gen_map_open(hand);
82
+
83
+ // TODO: itterate through keys in the hash
84
+ VALUE keys = rb_funcall(obj, intern_keys, 0);
85
+ VALUE entry, keyStr;
86
+ for(idx=0; idx<RARRAY_LEN(keys); idx++) {
87
+ entry = rb_ary_entry(keys, idx);
88
+ keyStr = rb_funcall(entry, intern_to_s, 0); // key must be a string
89
+ // the key
90
+ yajl_encode_part(hand, keyStr, io);
91
+ // the value
92
+ yajl_encode_part(hand, rb_hash_aref(obj, entry), io);
93
+ }
94
+
95
+ status = yajl_gen_map_close(hand);
96
+ break;
97
+ case T_ARRAY:
98
+ status = yajl_gen_array_open(hand);
99
+ for(idx=0; idx<RARRAY_LEN(obj); idx++) {
100
+ otherObj = rb_ary_entry(obj, idx);
101
+ yajl_encode_part(hand, otherObj, io);
102
+ }
103
+ status = yajl_gen_array_close(hand);
104
+ break;
105
+ case T_NIL:
106
+ status = yajl_gen_null(hand);
107
+ break;
108
+ case T_TRUE:
109
+ status = yajl_gen_bool(hand, 1);
110
+ break;
111
+ case T_FALSE:
112
+ status = yajl_gen_bool(hand, 0);
113
+ break;
114
+ case T_FIXNUM:
115
+ case T_FLOAT:
116
+ case T_BIGNUM:
117
+ str = rb_funcall(obj, intern_to_s, 0);
118
+ status = yajl_gen_number(hand, RSTRING_PTR(str), (unsigned int)RSTRING_LEN(str));
119
+ break;
120
+ case T_STRING:
121
+ status = yajl_gen_string(hand, (const unsigned char *)RSTRING_PTR(obj), (unsigned int)RSTRING_LEN(obj));
122
+ break;
123
+ default:
124
+ if (rb_respond_to(obj, intern_to_json)) {
125
+ str = rb_funcall(obj, intern_to_json, 0);
126
+ } else {
127
+ str = rb_funcall(obj, intern_to_s, 0);
128
+ }
129
+ status = yajl_gen_string(hand, (const unsigned char *)RSTRING_PTR(str), (unsigned int)RSTRING_LEN(str));
130
+ break;
131
+ }
132
+ }
133
+
134
+ void yajl_parser_wrapper_free(void * wrapper) {
135
+ struct yajl_parser_wrapper * w = wrapper;
136
+ yajl_free(w->parser);
137
+ free(w);
138
+ }
139
+
140
+ void yajl_parser_wrapper_mark(void * wrapper) {
141
+ struct yajl_parser_wrapper * w = wrapper;
142
+ rb_gc_mark(w->builderStack);
143
+ rb_gc_mark(w->parse_complete_callback);
144
+ }
145
+
146
+ void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle parser) {
147
+ yajl_status stat;
148
+
149
+ stat = yajl_parse(parser, chunk, len);
150
+
151
+ if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
152
+ unsigned char * str = yajl_get_error(parser, 1, chunk, len);
153
+ rb_raise(cParseError, "%s", (const char *) str);
154
+ yajl_free_error(parser, str);
155
+ }
156
+ }
157
+
158
+ // YAJL Callbacks
159
+ static int yajl_found_null(void * ctx) {
160
+ yajl_set_static_value(ctx, Qnil);
161
+ yajl_check_and_fire_callback(ctx);
162
+ return 1;
163
+ }
164
+
165
+ static int yajl_found_boolean(void * ctx, int boolean) {
166
+ yajl_set_static_value(ctx, boolean ? Qtrue : Qfalse);
167
+ yajl_check_and_fire_callback(ctx);
168
+ return 1;
169
+ }
170
+
171
+ static int yajl_found_number(void * ctx, const char * numberVal, unsigned int numberLen) {
172
+ VALUE subString = rb_str_new(numberVal, numberLen);
173
+ char * cSubString = RSTRING_PTR(subString);
174
+
175
+ if (strstr(cSubString, ".") != NULL || strstr(cSubString, "e") != NULL || strstr(cSubString, "E") != NULL) {
176
+ yajl_set_static_value(ctx, rb_Float(subString));
177
+ } else {
178
+ yajl_set_static_value(ctx, rb_Integer(subString));
179
+ }
180
+ yajl_check_and_fire_callback(ctx);
181
+ return 1;
182
+ }
183
+
184
+ static int yajl_found_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen) {
185
+ yajl_set_static_value(ctx, rb_str_new((const char *)stringVal, stringLen));
186
+ yajl_check_and_fire_callback(ctx);
187
+ return 1;
188
+ }
189
+
190
+ static int yajl_found_hash_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen) {
191
+ struct yajl_parser_wrapper * wrapper;
192
+ GetParser((VALUE)ctx, wrapper);
193
+ VALUE keyStr = rb_str_new((const char *)stringVal, stringLen);
194
+
195
+ if (wrapper->symbolizeKeys) {
196
+ ID key = rb_intern(RSTRING_PTR(keyStr));
197
+ yajl_set_static_value(ctx, ID2SYM(key));
198
+ } else {
199
+ yajl_set_static_value(ctx, keyStr);
200
+ }
201
+ yajl_check_and_fire_callback(ctx);
202
+ return 1;
203
+ }
204
+
205
+ static int yajl_found_start_hash(void * ctx) {
206
+ struct yajl_parser_wrapper * wrapper;
207
+ GetParser((VALUE)ctx, wrapper);
208
+ wrapper->nestedHashLevel++;
209
+ yajl_set_static_value(ctx, rb_hash_new());
210
+ return 1;
211
+ }
212
+
213
+ static int yajl_found_end_hash(void * ctx) {
214
+ struct yajl_parser_wrapper * wrapper;
215
+ GetParser((VALUE)ctx, wrapper);
216
+ wrapper->nestedHashLevel--;
217
+ if (RARRAY_LEN(wrapper->builderStack) > 1) {
218
+ rb_ary_pop(wrapper->builderStack);
219
+ }
220
+ yajl_check_and_fire_callback(ctx);
221
+ return 1;
222
+ }
223
+
224
+ static int yajl_found_start_array(void * ctx) {
225
+ struct yajl_parser_wrapper * wrapper;
226
+ GetParser((VALUE)ctx, wrapper);
227
+ wrapper->nestedArrayLevel++;
228
+ yajl_set_static_value(ctx, rb_ary_new());
229
+ return 1;
230
+ }
231
+
232
+ static int yajl_found_end_array(void * ctx) {
233
+ struct yajl_parser_wrapper * wrapper;
234
+ GetParser((VALUE)ctx, wrapper);
235
+ wrapper->nestedArrayLevel--;
236
+ if (RARRAY_LEN(wrapper->builderStack) > 1) {
237
+ rb_ary_pop(wrapper->builderStack);
238
+ }
239
+ yajl_check_and_fire_callback(ctx);
240
+ return 1;
241
+ }
242
+
243
+
244
+ // Ruby Interface
245
+
246
+ /*
247
+ * Document-class: Yajl::Parser
248
+ *
249
+ * This class contains methods for parsing JSON directly from an IO object.
250
+ * The only basic requirment currently is that the IO object respond to #read(len) and #eof?
251
+ * The IO is parsed until a complete JSON object has been read and a ruby object will be returned.
252
+ */
253
+
254
+ /*
255
+ * Document-method: new
256
+ *
257
+ * call-seq: new([:symbolize_keys => true, [:allow_comments => false[, :check_utf8 => false]]])
258
+ *
259
+ * :symbolize_keys will turn hash keys into Ruby symbols, defaults to false.
260
+ *
261
+ * :allow_comments will turn on/off the check for comments inside the JSON stream, defaults to true.
262
+ *
263
+ * :check_utf8 will validate UTF8 characters found in the JSON stream, defaults to true.
264
+ */
265
+ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE klass) {
266
+ struct yajl_parser_wrapper * wrapper;
267
+ yajl_parser_config cfg;
268
+ VALUE opts, obj;
269
+ int allowComments = 1, checkUTF8 = 1, symbolizeKeys = 0;
270
+
271
+ // Scan off config vars
272
+ if (rb_scan_args(argc, argv, "01", &opts) == 1) {
273
+ Check_Type(opts, T_HASH);
274
+
275
+ if (rb_hash_aref(opts, ID2SYM(sym_allow_comments)) == Qfalse) {
276
+ allowComments = 0;
277
+ }
278
+ if (rb_hash_aref(opts, ID2SYM(sym_check_utf8)) == Qfalse) {
279
+ checkUTF8 = 0;
280
+ }
281
+ if (rb_hash_aref(opts, ID2SYM(sym_symbolize_keys)) == Qtrue) {
282
+ symbolizeKeys = 1;
283
+ }
284
+ }
285
+ cfg = (yajl_parser_config){allowComments, checkUTF8};
286
+
287
+ obj = Data_Make_Struct(klass, struct yajl_parser_wrapper, yajl_parser_wrapper_mark, yajl_parser_wrapper_free, wrapper);
288
+ wrapper->parser = yajl_alloc(&callbacks, &cfg, NULL, (void *)obj);
289
+ wrapper->nestedArrayLevel = 0;
290
+ wrapper->nestedHashLevel = 0;
291
+ wrapper->objectsFound = 0;
292
+ wrapper->symbolizeKeys = symbolizeKeys;
293
+ wrapper->builderStack = rb_ary_new();
294
+ wrapper->parse_complete_callback = Qnil;
295
+ rb_obj_call_init(obj, 0, 0);
296
+ return obj;
297
+ }
298
+
299
+ /*
300
+ * Document-method: initialize
301
+ *
302
+ * call-seq: new([:symbolize_keys => true, [:allow_comments => false[, :check_utf8 => false]]])
303
+ *
304
+ * :symbolize_keys will turn hash keys into Ruby symbols, defaults to false.
305
+ *
306
+ * :allow_comments will turn on/off the check for comments inside the JSON stream, defaults to true.
307
+ *
308
+ * :check_utf8 will validate UTF8 characters found in the JSON stream, defaults to true.
309
+ */
310
+ static VALUE rb_yajl_parser_init(int argc, VALUE * argv, VALUE self) {
311
+ return self;
312
+ }
313
+
314
+ /*
315
+ * Document-method: parse
316
+ *
317
+ * call-seq:
318
+ * parse(input, buffer_size=8092)
319
+ * parse(input, buffer_size=8092) { |obj| ... }
320
+ *
321
+ * +input+ can either be a string or an IO to parse JSON from
322
+ *
323
+ * +buffer_size+ is the size of chunk that will be parsed off the input (if it's an IO) for each loop of the parsing process.
324
+ * 8092 is a good balance between the different types of streams (off disk, off a socket, etc...), but this option
325
+ * is here so the caller can better tune their parsing depending on the type of stream being passed.
326
+ * A larger read buffer will perform better for files off disk, where as a smaller size may be more efficient for
327
+ * reading off of a socket directly.
328
+ *
329
+ * If a block was passed, it's called when an object has been parsed off the stream. This is especially
330
+ * usefull when parsing a stream of multiple JSON objects.
331
+ *
332
+ * NOTE: you can optionally assign the +on_parse_complete+ callback, and it will be called the same way the optional
333
+ * block is for this method.
334
+ */
335
+ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) {
336
+ yajl_status stat;
337
+ struct yajl_parser_wrapper * wrapper;
338
+ VALUE parsed, rbufsize, input, blk;
339
+
340
+ GetParser(self, wrapper);
341
+ parsed = rb_str_new2("");
342
+
343
+ // setup our parameters
344
+ rb_scan_args(argc, argv, "11&", &input, &rbufsize, &blk);
345
+ if (NIL_P(rbufsize)) {
346
+ rbufsize = INT2FIX(READ_BUFSIZE);
347
+ } else {
348
+ Check_Type(rbufsize, T_FIXNUM);
349
+ }
350
+ if (!NIL_P(blk)) {
351
+ rb_yajl_set_complete_cb(self, blk);
352
+ }
353
+
354
+ if (TYPE(input) == T_STRING) {
355
+ yajl_parse_chunk((const unsigned char *)RSTRING_PTR(input), RSTRING_LEN(input), wrapper->parser);
356
+ } else if (rb_respond_to(input, intern_eof)) {
357
+ while (rb_funcall(input, intern_eof, 0) != Qtrue) {
358
+ rb_funcall(input, intern_io_read, 2, rbufsize, parsed);
359
+ yajl_parse_chunk((const unsigned char *)RSTRING_PTR(parsed), RSTRING_LEN(parsed), wrapper->parser);
360
+ }
361
+ } else {
362
+ rb_raise(cParseError, "input must be a string or IO");
363
+ }
364
+
365
+ // parse any remaining buffered data
366
+ stat = yajl_parse_complete(wrapper->parser);
367
+
368
+ if (wrapper->parse_complete_callback != Qnil) {
369
+ yajl_check_and_fire_callback((void *)self);
370
+ return Qnil;
371
+ }
372
+
373
+ return rb_ary_pop(wrapper->builderStack);
374
+ }
375
+
376
+ /*
377
+ * Document-method: parse_chunk
378
+ *
379
+ * call-seq: parse_chunk(string_chunk)
380
+ *
381
+ * +string_chunk+ can be a partial or full JSON string to push on the parser.
382
+ *
383
+ * This method will throw an exception if the +on_parse_complete+ callback hasn't been assigned yet.
384
+ * The +on_parse_complete+ callback assignment is required so the user can handle objects that have been
385
+ * parsed off the stream as they're found.
386
+ */
387
+ static VALUE rb_yajl_parser_parse_chunk(VALUE self, VALUE chunk) {
388
+ struct yajl_parser_wrapper * wrapper;
389
+
390
+ GetParser(self, wrapper);
391
+ if (NIL_P(chunk)) {
392
+ rb_raise(cParseError, "Can't parse a nil string.");
393
+ return Qnil;
394
+ }
395
+
396
+ if (wrapper->parse_complete_callback != Qnil) {
397
+ yajl_parse_chunk((const unsigned char *)RSTRING_PTR(chunk), RSTRING_LEN(chunk), wrapper->parser);
398
+ } else {
399
+ rb_raise(cParseError, "The on_parse_complete callback isn't setup, parsing useless.");
400
+ }
401
+
402
+ return Qnil;
403
+ }
404
+
405
+ /*
406
+ * Document-method: on_parse_complete=
407
+ *
408
+ * call-seq: on_parse_complete = Proc.new { |obj| ... }
409
+ *
410
+ * This callback setter allows you to pass a Proc/lambda or any other object that response to #call.
411
+ *
412
+ * It will pass a single parameter, the ruby object built from the last parsed JSON object
413
+ */
414
+ static VALUE rb_yajl_set_complete_cb(VALUE self, VALUE callback) {
415
+ struct yajl_parser_wrapper * wrapper;
416
+ GetParser(self, wrapper);
417
+ wrapper->parse_complete_callback = callback;
418
+ return Qnil;
419
+ }
420
+
421
+ /*
422
+ * Document-class: Yajl::Encoder
423
+ *
424
+ * This class contains methods for encoding a Ruby object into JSON, streaming it's output into an IO object.
425
+ * The IO object need only respond to #write(str)
426
+ * The JSON stream created is written to the IO in chunks, as it's being created.
427
+ */
428
+
429
+ /*
430
+ * Document-method: new
431
+ *
432
+ * call-seq: new([:pretty => false[, :indent => ' ']])
433
+ *
434
+ * :pretty will enable/disable beautifying or "pretty priting" the output string.
435
+ *
436
+ * :indent is the character(s) used to indent the output string.
437
+ */
438
+ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
439
+ yajl_gen_config cfg;
440
+ yajl_gen encoder;
441
+ VALUE opts, obj, indent;
442
+ const char * indentString = " ";
443
+ int beautify = 0;
444
+
445
+ // Scan off config vars
446
+ if (rb_scan_args(argc, argv, "01", &opts) == 1) {
447
+ Check_Type(opts, T_HASH);
448
+
449
+ if (rb_hash_aref(opts, ID2SYM(sym_pretty)) == Qtrue) {
450
+ beautify = 1;
451
+ indent = rb_hash_aref(opts, ID2SYM(sym_indent));
452
+ if (indent != Qnil) {
453
+ Check_Type(indent, T_STRING);
454
+ indentString = RSTRING_PTR(indent);
455
+ }
456
+ }
457
+ }
458
+ cfg = (yajl_gen_config){beautify, indentString};
459
+
460
+ encoder = yajl_gen_alloc(&cfg, NULL);
461
+ obj = Data_Wrap_Struct(klass, 0, yajl_gen_free, encoder);
462
+ rb_obj_call_init(obj, 0, 0);
463
+ return obj;
464
+ }
465
+
466
+ /*
467
+ * Document-method: initialize
468
+ *
469
+ * call-seq: initialize([:pretty => false[, :indent => ' ']])
470
+ *
471
+ * :pretty will enable/disable beautifying or "pretty priting" the output string.
472
+ *
473
+ * :indent is the character(s) used to indent the output string.
474
+ */
475
+ static VALUE rb_yajl_encoder_init(int argc, VALUE * argv, VALUE self) {
476
+ return self;
477
+ }
478
+
479
+ /*
480
+ * Document-method: encode
481
+ *
482
+ * call-seq: encode(obj[, io[, &block]])
483
+ *
484
+ * +obj+ is the Ruby object to encode to JSON
485
+ *
486
+ * +io+ is an optional IO used to stream the encoded JSON string to.
487
+ * If +io+ isn't specified, this method will return the resulting JSON string. If +io+ is specified, this method returns nil
488
+ *
489
+ * If an optional block is passed, it's called when encoding is complete and passed the resulting JSON string
490
+ *
491
+ * It should be noted that you can reuse an instance of this class to continue encoding multiple JSON
492
+ * to the same stream. Just continue calling this method, passing it the same IO object with new/different
493
+ * ruby objects to encode. This is how streaming is accomplished.
494
+ */
495
+ static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self) {
496
+ yajl_gen encoder;
497
+ const unsigned char * buffer;
498
+ unsigned int len;
499
+ VALUE obj, io, blk, outBuff;
500
+
501
+ GetEncoder(self, encoder);
502
+
503
+ rb_scan_args(argc, argv, "11&", &obj, &io, &blk);
504
+
505
+ // begin encode process
506
+ yajl_encode_part(encoder, obj, io);
507
+
508
+ // just make sure we output the remaining buffer
509
+ yajl_gen_get_buf(encoder, &buffer, &len);
510
+ outBuff = rb_str_new((const char *)buffer, len);
511
+ yajl_gen_clear(encoder);
512
+ if (io != Qnil) {
513
+ rb_io_write(io, outBuff);
514
+ return Qnil;
515
+ } else if (blk != Qnil) {
516
+ rb_funcall(blk, intern_call, 1, outBuff);
517
+ return Qnil;
518
+ } else {
519
+ return outBuff;
520
+ }
521
+ return Qnil;
522
+ }
523
+
524
+
525
+ // JSON Gem compatibility
526
+
527
+ /*
528
+ * Document-class: Object
529
+ */
530
+ /*
531
+ * Document-method: to_json
532
+ *
533
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
534
+ *
535
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
536
+ *
537
+ * Encodes an instance of Object to JSON
538
+ */
539
+ static VALUE rb_yajl_json_ext_object_to_json(int argc, VALUE * argv, VALUE self) {
540
+ VALUE rb_encoder;
541
+ rb_scan_args(argc, argv, "01", &rb_encoder);
542
+ if (rb_encoder == Qnil) {
543
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
544
+ }
545
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
546
+ }
547
+
548
+ /*
549
+ * Document-class: Hash
550
+ */
551
+ /*
552
+ * Document-method: to_json
553
+ *
554
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
555
+ *
556
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
557
+ *
558
+ * Encodes an instance of Hash to JSON
559
+ */
560
+ static VALUE rb_yajl_json_ext_hash_to_json(int argc, VALUE * argv, VALUE self) {
561
+ VALUE rb_encoder;
562
+ rb_scan_args(argc, argv, "01", &rb_encoder);
563
+ if (rb_encoder == Qnil) {
564
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
565
+ }
566
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
567
+ }
568
+
569
+ /*
570
+ * Document-class: Array
571
+ */
572
+ /*
573
+ * Document-method: to_json
574
+ *
575
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
576
+ *
577
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
578
+ *
579
+ * Encodes an instance of Array to JSON
580
+ */
581
+ static VALUE rb_yajl_json_ext_array_to_json(int argc, VALUE * argv, VALUE self) {
582
+ VALUE rb_encoder;
583
+ rb_scan_args(argc, argv, "01", &rb_encoder);
584
+ if (rb_encoder == Qnil) {
585
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
586
+ }
587
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
588
+ }
589
+
590
+ /*
591
+ * Document-class: Fixnum
592
+ */
593
+ /*
594
+ * Document-method: to_json
595
+ *
596
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
597
+ *
598
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
599
+ *
600
+ * Encodes an instance of Fixnum to JSON
601
+ */
602
+ static VALUE rb_yajl_json_ext_fixnum_to_json(int argc, VALUE * argv, VALUE self) {
603
+ VALUE rb_encoder;
604
+ rb_scan_args(argc, argv, "01", &rb_encoder);
605
+ if (rb_encoder == Qnil) {
606
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
607
+ }
608
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
609
+ }
610
+
611
+ /*
612
+ * Document-class: Float
613
+ */
614
+ /*
615
+ * Document-method: to_json
616
+ *
617
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
618
+ *
619
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
620
+ *
621
+ * Encodes an instance of Float to JSON
622
+ */
623
+ static VALUE rb_yajl_json_ext_float_to_json(int argc, VALUE * argv, VALUE self) {
624
+ VALUE rb_encoder;
625
+ rb_scan_args(argc, argv, "01", &rb_encoder);
626
+ if (rb_encoder == Qnil) {
627
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
628
+ }
629
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
630
+ }
631
+
632
+ /*
633
+ * Document-class: String
634
+ */
635
+ /*
636
+ * Document-method: to_json
637
+ *
638
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
639
+ *
640
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
641
+ *
642
+ * Encodes an instance of TrueClass to JSON
643
+ */
644
+ static VALUE rb_yajl_json_ext_string_to_json(int argc, VALUE * argv, VALUE self) {
645
+ VALUE rb_encoder;
646
+ rb_scan_args(argc, argv, "01", &rb_encoder);
647
+ if (rb_encoder == Qnil) {
648
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
649
+ }
650
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
651
+ }
652
+
653
+ /*
654
+ * Document-class: TrueClass
655
+ */
656
+ /*
657
+ * Document-method: to_json
658
+ *
659
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
660
+ *
661
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
662
+ *
663
+ * Encodes an instance of TrueClass to JSON
664
+ */
665
+ static VALUE rb_yajl_json_ext_true_to_json(int argc, VALUE * argv, VALUE self) {
666
+ VALUE rb_encoder;
667
+ rb_scan_args(argc, argv, "01", &rb_encoder);
668
+ if (rb_encoder == Qnil) {
669
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
670
+ }
671
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
672
+ }
673
+
674
+ /*
675
+ * Document-class: FalseClass
676
+ */
677
+ /*
678
+ * Document-method: to_json
679
+ *
680
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
681
+ *
682
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
683
+ *
684
+ * Encodes an instance of FalseClass to JSON
685
+ */
686
+ static VALUE rb_yajl_json_ext_false_to_json(int argc, VALUE * argv, VALUE self) {
687
+ VALUE rb_encoder;
688
+ rb_scan_args(argc, argv, "01", &rb_encoder);
689
+ if (rb_encoder == Qnil) {
690
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
691
+ }
692
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
693
+ }
694
+
695
+ /*
696
+ * Document-class: NilClass
697
+ */
698
+ /*
699
+ * Document-method: to_json
700
+ *
701
+ * call-seq: to_json(encoder=Yajl::Encoder.new)
702
+ *
703
+ * +encoder+ is an existing Yajl::Encoder used to encode JSON
704
+ *
705
+ * Encodes an instance of NilClass to JSON
706
+ */
707
+ static VALUE rb_yajl_json_ext_nil_to_json(int argc, VALUE * argv, VALUE self) {
708
+ VALUE rb_encoder;
709
+ rb_scan_args(argc, argv, "01", &rb_encoder);
710
+ if (rb_encoder == Qnil) {
711
+ rb_encoder = rb_yajl_encoder_new(0, NULL, cEncoder);
712
+ }
713
+ return rb_yajl_encoder_encode(1, &self, rb_encoder);
714
+ }
715
+
716
+ /*
717
+ * Document-class: Yajl::Encoder
718
+ */
719
+ /*
720
+ * Document-method: enable_json_gem_compatability
721
+ *
722
+ * call-seq: enable_json_gem_compatability
723
+ *
724
+ * Enables the JSON gem compatibility API
725
+ */
726
+ static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass) {
727
+ rb_define_method(rb_cObject, "to_json", rb_yajl_json_ext_object_to_json, -1);
728
+ rb_define_method(rb_cHash, "to_json", rb_yajl_json_ext_hash_to_json, -1);
729
+ rb_define_method(rb_cArray, "to_json", rb_yajl_json_ext_array_to_json, -1);
730
+ rb_define_method(rb_cFixnum, "to_json", rb_yajl_json_ext_fixnum_to_json, -1);
731
+ rb_define_method(rb_cFloat, "to_json", rb_yajl_json_ext_float_to_json, -1);
732
+ rb_define_method(rb_cString, "to_json", rb_yajl_json_ext_string_to_json, -1);
733
+ rb_define_method(rb_cTrueClass, "to_json", rb_yajl_json_ext_true_to_json, -1);
734
+ rb_define_method(rb_cFalseClass, "to_json", rb_yajl_json_ext_false_to_json, -1);
735
+ rb_define_method(rb_cNilClass, "to_json", rb_yajl_json_ext_nil_to_json, -1);
736
+ return Qnil;
737
+ }
738
+
739
+
740
+ // Ruby Extension initializer
741
+ void Init_yajl_ext() {
742
+ mYajl = rb_define_module("Yajl");
743
+
744
+ cParseError = rb_define_class_under(mYajl, "ParseError", rb_eStandardError);
745
+ cEncodeError = rb_define_class_under(mYajl, "EncodeError", rb_eStandardError);
746
+
747
+ cParser = rb_define_class_under(mYajl, "Parser", rb_cObject);
748
+ rb_define_singleton_method(cParser, "new", rb_yajl_parser_new, -1);
749
+ rb_define_method(cParser, "initialize", rb_yajl_parser_init, -1);
750
+ rb_define_method(cParser, "parse", rb_yajl_parser_parse, -1);
751
+ rb_define_method(cParser, "parse_chunk", rb_yajl_parser_parse_chunk, -1);
752
+ rb_define_method(cParser, "<<", rb_yajl_parser_parse_chunk, 1);
753
+ rb_define_method(cParser, "on_parse_complete=", rb_yajl_set_complete_cb, 1);
754
+
755
+ cEncoder = rb_define_class_under(mYajl, "Encoder", rb_cObject);
756
+ rb_define_singleton_method(cEncoder, "new", rb_yajl_encoder_new, -1);
757
+ rb_define_method(cEncoder, "initialize", rb_yajl_encoder_init, -1);
758
+ rb_define_method(cEncoder, "encode", rb_yajl_encoder_encode, -1);
759
+
760
+ rb_define_singleton_method(cEncoder, "enable_json_gem_compatability", rb_yajl_encoder_enable_json_gem_ext, 0);
761
+
762
+ intern_io_read = rb_intern("read");
763
+ intern_eof = rb_intern("eof?");
764
+ intern_call = rb_intern("call");
765
+ intern_keys = rb_intern("keys");
766
+ intern_to_s = rb_intern("to_s");
767
+ intern_to_json = rb_intern("to_json");
768
+
769
+ sym_allow_comments = rb_intern("allow_comments");
770
+ sym_check_utf8 = rb_intern("check_utf8");
771
+ sym_pretty = rb_intern("pretty");
772
+ sym_indent = rb_intern("indent");
773
+ sym_symbolize_keys = rb_intern("symbolize_keys");
774
+ }