benofsky-yajl-ruby 0.7.6

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 (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_encode.c ADDED
@@ -0,0 +1,188 @@
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 "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
+ yajl_string_encode2((const yajl_print_t) &yajl_buf_append, buf, str, len);
52
+ }
53
+
54
+ void
55
+ yajl_string_encode2(const yajl_print_t print,
56
+ void * ctx,
57
+ const unsigned char * str,
58
+ unsigned int len)
59
+ {
60
+ unsigned int beg = 0;
61
+ unsigned int end = 0;
62
+ char hexBuf[7];
63
+ hexBuf[0] = '\\'; hexBuf[1] = 'u'; hexBuf[2] = '0'; hexBuf[3] = '0';
64
+ hexBuf[6] = 0;
65
+
66
+ while (end < len) {
67
+ const char * escaped = NULL;
68
+ switch (str[end]) {
69
+ case '\r': escaped = "\\r"; break;
70
+ case '\n': escaped = "\\n"; break;
71
+ case '\\': escaped = "\\\\"; break;
72
+ /* case '/': escaped = "\\/"; break; */
73
+ case '"': escaped = "\\\""; break;
74
+ case '\f': escaped = "\\f"; break;
75
+ case '\b': escaped = "\\b"; break;
76
+ case '\t': escaped = "\\t"; break;
77
+ default:
78
+ if ((unsigned char) str[end] < 32) {
79
+ CharToHex(str[end], hexBuf + 4);
80
+ escaped = hexBuf;
81
+ }
82
+ break;
83
+ }
84
+ if (escaped != NULL) {
85
+ print(ctx, (const char *) (str + beg), end - beg);
86
+ print(ctx, escaped, strlen(escaped));
87
+ beg = ++end;
88
+ } else {
89
+ ++end;
90
+ }
91
+ }
92
+ print(ctx, (const char *) (str + beg), end - beg);
93
+ }
94
+
95
+ static void hexToDigit(unsigned int * val, const unsigned char * hex)
96
+ {
97
+ unsigned int i;
98
+ for (i=0;i<4;i++) {
99
+ unsigned char c = hex[i];
100
+ if (c >= 'A') c = (c & ~0x20) - 7;
101
+ c -= '0';
102
+ assert(!(c & 0xF0));
103
+ *val = (*val << 4) | c;
104
+ }
105
+ }
106
+
107
+ static void Utf32toUtf8(unsigned int codepoint, char * utf8Buf)
108
+ {
109
+ if (codepoint < 0x80) {
110
+ utf8Buf[0] = (char) codepoint;
111
+ utf8Buf[1] = 0;
112
+ } else if (codepoint < 0x0800) {
113
+ utf8Buf[0] = (char) ((codepoint >> 6) | 0xC0);
114
+ utf8Buf[1] = (char) ((codepoint & 0x3F) | 0x80);
115
+ utf8Buf[2] = 0;
116
+ } else if (codepoint < 0x10000) {
117
+ utf8Buf[0] = (char) ((codepoint >> 12) | 0xE0);
118
+ utf8Buf[1] = (char) (((codepoint >> 6) & 0x3F) | 0x80);
119
+ utf8Buf[2] = (char) ((codepoint & 0x3F) | 0x80);
120
+ utf8Buf[3] = 0;
121
+ } else if (codepoint < 0x200000) {
122
+ utf8Buf[0] =(char)((codepoint >> 18) | 0xF0);
123
+ utf8Buf[1] =(char)(((codepoint >> 12) & 0x3F) | 0x80);
124
+ utf8Buf[2] =(char)(((codepoint >> 6) & 0x3F) | 0x80);
125
+ utf8Buf[3] =(char)((codepoint & 0x3F) | 0x80);
126
+ utf8Buf[4] = 0;
127
+ } else {
128
+ utf8Buf[0] = '?';
129
+ utf8Buf[1] = 0;
130
+ }
131
+ }
132
+
133
+ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
134
+ unsigned int len)
135
+ {
136
+ unsigned int beg = 0;
137
+ unsigned int end = 0;
138
+
139
+ while (end < len) {
140
+ if (str[end] == '\\') {
141
+ char utf8Buf[5];
142
+ const char * unescaped = "?";
143
+ yajl_buf_append(buf, str + beg, end - beg);
144
+ switch (str[++end]) {
145
+ case 'r': unescaped = "\r"; break;
146
+ case 'n': unescaped = "\n"; break;
147
+ case '\\': unescaped = "\\"; break;
148
+ case '/': unescaped = "/"; break;
149
+ case '"': unescaped = "\""; break;
150
+ case 'f': unescaped = "\f"; break;
151
+ case 'b': unescaped = "\b"; break;
152
+ case 't': unescaped = "\t"; break;
153
+ case 'u': {
154
+ unsigned int codepoint = 0;
155
+ hexToDigit(&codepoint, str + ++end);
156
+ end+=3;
157
+ /* check if this is a surrogate */
158
+ if ((codepoint & 0xFC00) == 0xD800) {
159
+ end++;
160
+ if (str[end] == '\\' && str[end + 1] == 'u') {
161
+ unsigned int surrogate = 0;
162
+ hexToDigit(&surrogate, str + end + 2);
163
+ codepoint =
164
+ (((codepoint & 0x3F) << 10) |
165
+ ((((codepoint >> 6) & 0xF) + 1) << 16) |
166
+ (surrogate & 0x3FF));
167
+ end += 5;
168
+ } else {
169
+ unescaped = "?";
170
+ break;
171
+ }
172
+ }
173
+
174
+ Utf32toUtf8(codepoint, utf8Buf);
175
+ unescaped = utf8Buf;
176
+ break;
177
+ }
178
+ default:
179
+ assert("this should never happen" == NULL);
180
+ }
181
+ yajl_buf_append(buf, unescaped, strlen(unescaped));
182
+ beg = ++end;
183
+ } else {
184
+ end++;
185
+ }
186
+ }
187
+ yajl_buf_append(buf, str + beg, end - beg);
188
+ }
data/ext/yajl_encode.h ADDED
@@ -0,0 +1,50 @@
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_ENCODE_H__
34
+ #define __YAJL_ENCODE_H__
35
+
36
+ #include "yajl_buf.h"
37
+ #include "api/yajl_gen.h"
38
+
39
+ void yajl_string_encode2(const yajl_print_t printer,
40
+ void * ctx,
41
+ const unsigned char * str,
42
+ unsigned int length);
43
+
44
+ void yajl_string_encode(yajl_buf buf, const unsigned char * str,
45
+ unsigned int length);
46
+
47
+ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
48
+ unsigned int length);
49
+
50
+ #endif