benschwarz-smoke 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (245) hide show
  1. data/Rakefile +47 -0
  2. data/VERSION.yml +2 -2
  3. data/lib/core_ext/string.rb +6 -0
  4. data/lib/smoke/cache.rb +38 -23
  5. data/lib/smoke/request.rb +4 -8
  6. data/lib/smoke.rb +7 -2
  7. data/rdoc/classes/Smoke/Origin.html +340 -0
  8. data/rdoc/classes/Smoke/Source/Data.html +126 -0
  9. data/rdoc/classes/Smoke/Source/Feed.html +117 -0
  10. data/rdoc/classes/Smoke/Source/YQL.html +223 -0
  11. data/rdoc/classes/Smoke.html +260 -0
  12. data/rdoc/created.rid +1 -0
  13. data/rdoc/files/README_markdown.html +180 -0
  14. data/rdoc/files/lib/core_ext/hash_rb.html +49 -0
  15. data/rdoc/files/lib/smoke/origin_rb.html +49 -0
  16. data/rdoc/files/lib/smoke/request_rb.html +49 -0
  17. data/rdoc/files/lib/smoke/source/data_rb.html +49 -0
  18. data/rdoc/files/lib/smoke/source/feed_rb.html +49 -0
  19. data/rdoc/files/lib/smoke/source/join_rb.html +49 -0
  20. data/rdoc/files/lib/smoke/source/yql_rb.html +49 -0
  21. data/rdoc/files/lib/smoke_rb.html +65 -0
  22. data/rdoc/fr_class_index.html +21 -0
  23. data/rdoc/fr_file_index.html +28 -0
  24. data/rdoc/fr_method_index.html +4459 -0
  25. data/rdoc/index.html +15 -0
  26. data/rdoc/rdoc-style.css +319 -0
  27. data/spec/smoke/cache_spec.rb +28 -6
  28. data/vendor/crack/History +15 -0
  29. data/vendor/crack/LICENSE +20 -0
  30. data/vendor/crack/README.rdoc +42 -0
  31. data/vendor/crack/Rakefile +49 -0
  32. data/vendor/crack/VERSION.yml +4 -0
  33. data/vendor/crack/crack.gemspec +61 -0
  34. data/vendor/crack/lib/crack/core_extensions.rb +128 -0
  35. data/vendor/crack/lib/crack/json.rb +68 -0
  36. data/vendor/crack/lib/crack/xml.rb +214 -0
  37. data/vendor/crack/lib/crack.rb +7 -0
  38. data/vendor/crack/test/crack_test.rb +4 -0
  39. data/vendor/crack/test/data/twittersearch-firefox.json +1 -0
  40. data/vendor/crack/test/data/twittersearch-ie.json +1 -0
  41. data/vendor/crack/test/hash_test.rb +56 -0
  42. data/vendor/crack/test/json_test.rb +66 -0
  43. data/vendor/crack/test/string_test.rb +31 -0
  44. data/vendor/crack/test/test_helper.rb +12 -0
  45. data/vendor/crack/test/xml_test.rb +489 -0
  46. data/vendor/dependencies/README.markdown +113 -0
  47. data/vendor/dependencies/Rakefile +5 -0
  48. data/vendor/dependencies/bin/dep +83 -0
  49. data/vendor/dependencies/dependencies.gemspec +15 -0
  50. data/vendor/dependencies/dependencies.gemspec.erb +27 -0
  51. data/vendor/dependencies/lib/dependencies/dep.rb +99 -0
  52. data/vendor/dependencies/lib/dependencies.rb +5 -0
  53. data/vendor/dependencies/test/dependencies_test.rb +228 -0
  54. data/vendor/dependencies/test/foobaz-0.3.gem +0 -0
  55. data/vendor/fakeweb/CHANGELOG +163 -0
  56. data/vendor/fakeweb/LICENSE.txt +281 -0
  57. data/vendor/fakeweb/README.rdoc +193 -0
  58. data/vendor/fakeweb/Rakefile +76 -0
  59. data/vendor/fakeweb/fakeweb.gemspec +21 -0
  60. data/vendor/fakeweb/lib/fake_web/ext/net_http.rb +71 -0
  61. data/vendor/fakeweb/lib/fake_web/registry.rb +103 -0
  62. data/vendor/fakeweb/lib/fake_web/responder.rb +113 -0
  63. data/vendor/fakeweb/lib/fake_web/response.rb +10 -0
  64. data/vendor/fakeweb/lib/fake_web/stub_socket.rb +15 -0
  65. data/vendor/fakeweb/lib/fake_web/utility.rb +22 -0
  66. data/vendor/fakeweb/lib/fake_web.rb +172 -0
  67. data/vendor/fakeweb/lib/fakeweb.rb +2 -0
  68. data/vendor/fakeweb/test/fixtures/google_response_from_curl +12 -0
  69. data/vendor/fakeweb/test/fixtures/google_response_with_transfer_encoding +17 -0
  70. data/vendor/fakeweb/test/fixtures/google_response_without_transfer_encoding +11 -0
  71. data/vendor/fakeweb/test/fixtures/test_example.txt +1 -0
  72. data/vendor/fakeweb/test/fixtures/test_txt_file +3 -0
  73. data/vendor/fakeweb/test/test_allow_net_connect.rb +85 -0
  74. data/vendor/fakeweb/test/test_deprecations.rb +54 -0
  75. data/vendor/fakeweb/test/test_fake_authentication.rb +92 -0
  76. data/vendor/fakeweb/test/test_fake_web.rb +535 -0
  77. data/vendor/fakeweb/test/test_fake_web_open_uri.rb +58 -0
  78. data/vendor/fakeweb/test/test_helper.rb +74 -0
  79. data/vendor/fakeweb/test/test_missing_open_uri.rb +25 -0
  80. data/vendor/fakeweb/test/test_precedence.rb +51 -0
  81. data/vendor/fakeweb/test/test_query_string.rb +45 -0
  82. data/vendor/fakeweb/test/test_regexes.rb +103 -0
  83. data/vendor/fakeweb/test/test_response_headers.rb +73 -0
  84. data/vendor/fakeweb/test/test_trailing_slashes.rb +53 -0
  85. data/vendor/fakeweb/test/test_utility.rb +70 -0
  86. data/vendor/json-1.1.3/CHANGES +93 -0
  87. data/vendor/json-1.1.3/GPL +340 -0
  88. data/vendor/json-1.1.3/README +78 -0
  89. data/vendor/json-1.1.3/RUBY +58 -0
  90. data/vendor/json-1.1.3/Rakefile +309 -0
  91. data/vendor/json-1.1.3/TODO +1 -0
  92. data/vendor/json-1.1.3/VERSION +1 -0
  93. data/vendor/json-1.1.3/benchmarks/benchmark.txt +133 -0
  94. data/vendor/json-1.1.3/benchmarks/benchmark_generator.rb +48 -0
  95. data/vendor/json-1.1.3/benchmarks/benchmark_parser.rb +26 -0
  96. data/vendor/json-1.1.3/benchmarks/benchmark_rails.rb +26 -0
  97. data/vendor/json-1.1.3/bin/edit_json.rb +10 -0
  98. data/vendor/json-1.1.3/bin/prettify_json.rb +76 -0
  99. data/vendor/json-1.1.3/data/example.json +1 -0
  100. data/vendor/json-1.1.3/data/index.html +38 -0
  101. data/vendor/json-1.1.3/data/prototype.js +4184 -0
  102. data/vendor/json-1.1.3/ext/json/ext/generator/extconf.rb +9 -0
  103. data/vendor/json-1.1.3/ext/json/ext/generator/generator.c +875 -0
  104. data/vendor/json-1.1.3/ext/json/ext/generator/unicode.c +182 -0
  105. data/vendor/json-1.1.3/ext/json/ext/generator/unicode.h +53 -0
  106. data/vendor/json-1.1.3/ext/json/ext/parser/extconf.rb +9 -0
  107. data/vendor/json-1.1.3/ext/json/ext/parser/parser.c +1758 -0
  108. data/vendor/json-1.1.3/ext/json/ext/parser/parser.rl +638 -0
  109. data/vendor/json-1.1.3/ext/json/ext/parser/unicode.c +154 -0
  110. data/vendor/json-1.1.3/ext/json/ext/parser/unicode.h +58 -0
  111. data/vendor/json-1.1.3/install.rb +26 -0
  112. data/vendor/json-1.1.3/lib/json/Array.xpm +21 -0
  113. data/vendor/json-1.1.3/lib/json/FalseClass.xpm +21 -0
  114. data/vendor/json-1.1.3/lib/json/Hash.xpm +21 -0
  115. data/vendor/json-1.1.3/lib/json/Key.xpm +73 -0
  116. data/vendor/json-1.1.3/lib/json/NilClass.xpm +21 -0
  117. data/vendor/json-1.1.3/lib/json/Numeric.xpm +28 -0
  118. data/vendor/json-1.1.3/lib/json/String.xpm +96 -0
  119. data/vendor/json-1.1.3/lib/json/TrueClass.xpm +21 -0
  120. data/vendor/json-1.1.3/lib/json/add/core.rb +135 -0
  121. data/vendor/json-1.1.3/lib/json/add/rails.rb +58 -0
  122. data/vendor/json-1.1.3/lib/json/common.rb +354 -0
  123. data/vendor/json-1.1.3/lib/json/editor.rb +1362 -0
  124. data/vendor/json-1.1.3/lib/json/ext.rb +13 -0
  125. data/vendor/json-1.1.3/lib/json/json.xpm +1499 -0
  126. data/vendor/json-1.1.3/lib/json/pure/generator.rb +394 -0
  127. data/vendor/json-1.1.3/lib/json/pure/parser.rb +259 -0
  128. data/vendor/json-1.1.3/lib/json/pure.rb +75 -0
  129. data/vendor/json-1.1.3/lib/json/version.rb +9 -0
  130. data/vendor/json-1.1.3/lib/json.rb +235 -0
  131. data/vendor/json-1.1.3/tests/fixtures/fail1.json +1 -0
  132. data/vendor/json-1.1.3/tests/fixtures/fail10.json +1 -0
  133. data/vendor/json-1.1.3/tests/fixtures/fail11.json +1 -0
  134. data/vendor/json-1.1.3/tests/fixtures/fail12.json +1 -0
  135. data/vendor/json-1.1.3/tests/fixtures/fail13.json +1 -0
  136. data/vendor/json-1.1.3/tests/fixtures/fail14.json +1 -0
  137. data/vendor/json-1.1.3/tests/fixtures/fail18.json +1 -0
  138. data/vendor/json-1.1.3/tests/fixtures/fail19.json +1 -0
  139. data/vendor/json-1.1.3/tests/fixtures/fail2.json +1 -0
  140. data/vendor/json-1.1.3/tests/fixtures/fail20.json +1 -0
  141. data/vendor/json-1.1.3/tests/fixtures/fail21.json +1 -0
  142. data/vendor/json-1.1.3/tests/fixtures/fail22.json +1 -0
  143. data/vendor/json-1.1.3/tests/fixtures/fail23.json +1 -0
  144. data/vendor/json-1.1.3/tests/fixtures/fail24.json +1 -0
  145. data/vendor/json-1.1.3/tests/fixtures/fail25.json +1 -0
  146. data/vendor/json-1.1.3/tests/fixtures/fail27.json +2 -0
  147. data/vendor/json-1.1.3/tests/fixtures/fail28.json +2 -0
  148. data/vendor/json-1.1.3/tests/fixtures/fail3.json +1 -0
  149. data/vendor/json-1.1.3/tests/fixtures/fail4.json +1 -0
  150. data/vendor/json-1.1.3/tests/fixtures/fail5.json +1 -0
  151. data/vendor/json-1.1.3/tests/fixtures/fail6.json +1 -0
  152. data/vendor/json-1.1.3/tests/fixtures/fail7.json +1 -0
  153. data/vendor/json-1.1.3/tests/fixtures/fail8.json +1 -0
  154. data/vendor/json-1.1.3/tests/fixtures/fail9.json +1 -0
  155. data/vendor/json-1.1.3/tests/fixtures/pass1.json +56 -0
  156. data/vendor/json-1.1.3/tests/fixtures/pass15.json +1 -0
  157. data/vendor/json-1.1.3/tests/fixtures/pass16.json +1 -0
  158. data/vendor/json-1.1.3/tests/fixtures/pass17.json +1 -0
  159. data/vendor/json-1.1.3/tests/fixtures/pass2.json +1 -0
  160. data/vendor/json-1.1.3/tests/fixtures/pass26.json +1 -0
  161. data/vendor/json-1.1.3/tests/fixtures/pass3.json +6 -0
  162. data/vendor/json-1.1.3/tests/runner.rb +25 -0
  163. data/vendor/json-1.1.3/tests/test_json.rb +293 -0
  164. data/vendor/json-1.1.3/tests/test_json_addition.rb +161 -0
  165. data/vendor/json-1.1.3/tests/test_json_fixtures.rb +30 -0
  166. data/vendor/json-1.1.3/tests/test_json_generate.rb +100 -0
  167. data/vendor/json-1.1.3/tests/test_json_rails.rb +118 -0
  168. data/vendor/json-1.1.3/tests/test_json_unicode.rb +61 -0
  169. data/vendor/json-1.1.3/tools/fuzz.rb +140 -0
  170. data/vendor/json-1.1.3/tools/server.rb +62 -0
  171. data/vendor/moneta/LICENSE +20 -0
  172. data/vendor/moneta/README +51 -0
  173. data/vendor/moneta/Rakefile +60 -0
  174. data/vendor/moneta/TODO +4 -0
  175. data/vendor/moneta/benchmarks/various.rb +234 -0
  176. data/vendor/moneta/lib/moneta/basic_file.rb +111 -0
  177. data/vendor/moneta/lib/moneta/berkeley.rb +53 -0
  178. data/vendor/moneta/lib/moneta/couch.rb +63 -0
  179. data/vendor/moneta/lib/moneta/datamapper.rb +117 -0
  180. data/vendor/moneta/lib/moneta/file.rb +91 -0
  181. data/vendor/moneta/lib/moneta/lmc.rb +52 -0
  182. data/vendor/moneta/lib/moneta/memcache.rb +53 -0
  183. data/vendor/moneta/lib/moneta/memory.rb +11 -0
  184. data/vendor/moneta/lib/moneta/mongodb.rb +58 -0
  185. data/vendor/moneta/lib/moneta/redis.rb +49 -0
  186. data/vendor/moneta/lib/moneta/rufus.rb +41 -0
  187. data/vendor/moneta/lib/moneta/s3.rb +162 -0
  188. data/vendor/moneta/lib/moneta/sdbm.rb +33 -0
  189. data/vendor/moneta/lib/moneta/tyrant.rb +58 -0
  190. data/vendor/moneta/lib/moneta/xattr.rb +58 -0
  191. data/vendor/moneta/lib/moneta.rb +76 -0
  192. data/vendor/moneta/moneta.gemspec +32 -0
  193. data/vendor/moneta/script/destroy +14 -0
  194. data/vendor/moneta/script/generate +14 -0
  195. data/vendor/moneta/spec/moneta_basic_file_spec.rb +50 -0
  196. data/vendor/moneta/spec/moneta_berkeley_spec.rb +20 -0
  197. data/vendor/moneta/spec/moneta_couch_spec.rb +22 -0
  198. data/vendor/moneta/spec/moneta_datamapper_spec.rb +79 -0
  199. data/vendor/moneta/spec/moneta_file_spec.rb +21 -0
  200. data/vendor/moneta/spec/moneta_lmc_spec.rb +24 -0
  201. data/vendor/moneta/spec/moneta_memcache_spec.rb +16 -0
  202. data/vendor/moneta/spec/moneta_memory_spec.rb +12 -0
  203. data/vendor/moneta/spec/moneta_mongodb_spec.rb +16 -0
  204. data/vendor/moneta/spec/moneta_redis_spec.rb +16 -0
  205. data/vendor/moneta/spec/moneta_rufus_spec.rb +15 -0
  206. data/vendor/moneta/spec/moneta_s3_spec.rb +19 -0
  207. data/vendor/moneta/spec/moneta_sdbm_spec.rb +21 -0
  208. data/vendor/moneta/spec/moneta_tyrant_spec.rb +15 -0
  209. data/vendor/moneta/spec/moneta_xattr_spec.rb +21 -0
  210. data/vendor/moneta/spec/shared.rb +122 -0
  211. data/vendor/moneta/spec/spec_helper.rb +7 -0
  212. data/vendor/rest-client/README.rdoc +151 -0
  213. data/vendor/rest-client/Rakefile +58 -0
  214. data/vendor/rest-client/VERSION +1 -0
  215. data/vendor/rest-client/bin/restclient +87 -0
  216. data/vendor/rest-client/lib/rest_client.rb +2 -0
  217. data/vendor/rest-client/lib/restclient/exceptions.rb +88 -0
  218. data/vendor/rest-client/lib/restclient/mixin/response.rb +43 -0
  219. data/vendor/rest-client/lib/restclient/raw_response.rb +30 -0
  220. data/vendor/rest-client/lib/restclient/request.rb +238 -0
  221. data/vendor/rest-client/lib/restclient/resource.rb +146 -0
  222. data/vendor/rest-client/lib/restclient/response.rb +20 -0
  223. data/vendor/rest-client/lib/restclient.rb +99 -0
  224. data/vendor/rest-client/rest-client.gemspec +66 -0
  225. data/vendor/rest-client/spec/base.rb +4 -0
  226. data/vendor/rest-client/spec/exceptions_spec.rb +65 -0
  227. data/vendor/rest-client/spec/mixin/response_spec.rb +46 -0
  228. data/vendor/rest-client/spec/raw_response_spec.rb +17 -0
  229. data/vendor/rest-client/spec/request_spec.rb +476 -0
  230. data/vendor/rest-client/spec/resource_spec.rb +75 -0
  231. data/vendor/rest-client/spec/response_spec.rb +16 -0
  232. data/vendor/rest-client/spec/restclient_spec.rb +53 -0
  233. data/vendor/simple-rss/LICENSE +429 -0
  234. data/vendor/simple-rss/README +43 -0
  235. data/vendor/simple-rss/Rakefile +212 -0
  236. data/vendor/simple-rss/install.rb +40 -0
  237. data/vendor/simple-rss/lib/simple-rss.rb +150 -0
  238. data/vendor/simple-rss/simple-rss.gemspec +12 -0
  239. data/vendor/simple-rss/test/base/base_test.rb +51 -0
  240. data/vendor/simple-rss/test/data/atom.xml +45 -0
  241. data/vendor/simple-rss/test/data/not-rss.xml +8 -0
  242. data/vendor/simple-rss/test/data/rss09.rdf +79 -0
  243. data/vendor/simple-rss/test/data/rss20.xml +818 -0
  244. data/vendor/simple-rss/test/test_helper.rb +4 -0
  245. metadata +314 -40
@@ -0,0 +1,875 @@
1
+ #include <string.h>
2
+ #include "ruby.h"
3
+ #include "st.h"
4
+ #include "unicode.h"
5
+ #include <math.h>
6
+
7
+ #define check_max_nesting(state, depth) do { \
8
+ long current_nesting = 1 + depth; \
9
+ if (state->max_nesting != 0 && current_nesting > state->max_nesting) \
10
+ rb_raise(eNestingError, "nesting of %ld is too deep", current_nesting); \
11
+ } while (0);
12
+
13
+ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
14
+ mHash, mArray, mInteger, mFloat, mString, mString_Extend,
15
+ mTrueClass, mFalseClass, mNilClass, eGeneratorError,
16
+ eCircularDatastructure, eNestingError;
17
+
18
+ static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
19
+ i_object_nl, i_array_nl, i_check_circular, i_max_nesting,
20
+ i_allow_nan, i_pack, i_unpack, i_create_id, i_extend;
21
+
22
+ typedef struct JSON_Generator_StateStruct {
23
+ VALUE indent;
24
+ VALUE space;
25
+ VALUE space_before;
26
+ VALUE object_nl;
27
+ VALUE array_nl;
28
+ int check_circular;
29
+ VALUE seen;
30
+ VALUE memo;
31
+ VALUE depth;
32
+ long max_nesting;
33
+ int flag;
34
+ int allow_nan;
35
+ } JSON_Generator_State;
36
+
37
+ #define GET_STATE(self) \
38
+ JSON_Generator_State *state; \
39
+ Data_Get_Struct(self, JSON_Generator_State, state);
40
+
41
+ /*
42
+ * Document-module: JSON::Ext::Generator
43
+ *
44
+ * This is the JSON generator implemented as a C extension. It can be
45
+ * configured to be used by setting
46
+ *
47
+ * JSON.generator = JSON::Ext::Generator
48
+ *
49
+ * with the method generator= in JSON.
50
+ *
51
+ */
52
+
53
+ static int hash_to_json_state_i(VALUE key, VALUE value, VALUE Vstate)
54
+ {
55
+ VALUE json, buf, Vdepth;
56
+ GET_STATE(Vstate);
57
+ buf = state->memo;
58
+ Vdepth = state->depth;
59
+
60
+ if (key == Qundef) return ST_CONTINUE;
61
+ if (state->flag) {
62
+ state->flag = 0;
63
+ rb_str_buf_cat2(buf, ",");
64
+ if (RSTRING_LEN(state->object_nl)) rb_str_buf_append(buf, state->object_nl);
65
+ }
66
+ if (RSTRING_LEN(state->object_nl)) {
67
+ rb_str_buf_append(buf, rb_str_times(state->indent, Vdepth));
68
+ }
69
+ json = rb_funcall(rb_funcall(key, i_to_s, 0), i_to_json, 2, Vstate, Vdepth);
70
+ Check_Type(json, T_STRING);
71
+ rb_str_buf_append(buf, json);
72
+ OBJ_INFECT(buf, json);
73
+ if (RSTRING_LEN(state->space_before)) {
74
+ rb_str_buf_append(buf, state->space_before);
75
+ }
76
+ rb_str_buf_cat2(buf, ":");
77
+ if (RSTRING_LEN(state->space)) rb_str_buf_append(buf, state->space);
78
+ json = rb_funcall(value, i_to_json, 2, Vstate, Vdepth);
79
+ Check_Type(json, T_STRING);
80
+ state->flag = 1;
81
+ rb_str_buf_append(buf, json);
82
+ OBJ_INFECT(buf, json);
83
+ state->depth = Vdepth;
84
+ state->memo = buf;
85
+ return ST_CONTINUE;
86
+ }
87
+
88
+ inline static VALUE mHash_json_transfrom(VALUE self, VALUE Vstate, VALUE Vdepth) {
89
+ long depth, len = RHASH(self)->tbl->num_entries;
90
+ VALUE result;
91
+ GET_STATE(Vstate);
92
+
93
+ depth = 1 + FIX2LONG(Vdepth);
94
+ result = rb_str_buf_new(len);
95
+ state->memo = result;
96
+ state->depth = LONG2FIX(depth);
97
+ state->flag = 0;
98
+ rb_str_buf_cat2(result, "{");
99
+ if (RSTRING_LEN(state->object_nl)) rb_str_buf_append(result, state->object_nl);
100
+ rb_hash_foreach(self, hash_to_json_state_i, Vstate);
101
+ if (RSTRING_LEN(state->object_nl)) rb_str_buf_append(result, state->object_nl);
102
+ if (RSTRING_LEN(state->object_nl)) {
103
+ rb_str_buf_append(result, rb_str_times(state->indent, Vdepth));
104
+ }
105
+ rb_str_buf_cat2(result, "}");
106
+ return result;
107
+ }
108
+
109
+ static int hash_to_json_i(VALUE key, VALUE value, VALUE buf)
110
+ {
111
+ VALUE tmp;
112
+
113
+ if (key == Qundef) return ST_CONTINUE;
114
+ if (RSTRING_LEN(buf) > 1) rb_str_buf_cat2(buf, ",");
115
+ tmp = rb_funcall(rb_funcall(key, i_to_s, 0), i_to_json, 0);
116
+ Check_Type(tmp, T_STRING);
117
+ rb_str_buf_append(buf, tmp);
118
+ OBJ_INFECT(buf, tmp);
119
+ rb_str_buf_cat2(buf, ":");
120
+ tmp = rb_funcall(value, i_to_json, 0);
121
+ Check_Type(tmp, T_STRING);
122
+ rb_str_buf_append(buf, tmp);
123
+ OBJ_INFECT(buf, tmp);
124
+
125
+ return ST_CONTINUE;
126
+ }
127
+
128
+ /*
129
+ * call-seq: to_json(state = nil, depth = 0)
130
+ *
131
+ * Returns a JSON string containing a JSON object, that is unparsed from
132
+ * this Hash instance.
133
+ * _state_ is a JSON::State object, that can also be used to configure the
134
+ * produced JSON string output further.
135
+ * _depth_ is used to find out nesting depth, to indent accordingly.
136
+ */
137
+ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
138
+ {
139
+ VALUE Vstate, Vdepth, result;
140
+ long depth;
141
+
142
+ rb_scan_args(argc, argv, "02", &Vstate, &Vdepth);
143
+ depth = NIL_P(Vdepth) ? 0 : FIX2LONG(Vdepth);
144
+ if (NIL_P(Vstate)) {
145
+ long len = RHASH(self)->tbl->num_entries;
146
+ result = rb_str_buf_new(len);
147
+ rb_str_buf_cat2(result, "{");
148
+ rb_hash_foreach(self, hash_to_json_i, result);
149
+ rb_str_buf_cat2(result, "}");
150
+ } else {
151
+ GET_STATE(Vstate);
152
+ check_max_nesting(state, depth);
153
+ if (state->check_circular) {
154
+ VALUE self_id = rb_obj_id(self);
155
+ if (RTEST(rb_hash_aref(state->seen, self_id))) {
156
+ rb_raise(eCircularDatastructure,
157
+ "circular data structures not supported!");
158
+ }
159
+ rb_hash_aset(state->seen, self_id, Qtrue);
160
+ result = mHash_json_transfrom(self, Vstate, LONG2FIX(depth));
161
+ rb_hash_delete(state->seen, self_id);
162
+ } else {
163
+ result = mHash_json_transfrom(self, Vstate, LONG2FIX(depth));
164
+ }
165
+ }
166
+ OBJ_INFECT(result, self);
167
+ return result;
168
+ }
169
+
170
+ inline static VALUE mArray_json_transfrom(VALUE self, VALUE Vstate, VALUE Vdepth) {
171
+ long i, len = RARRAY_LEN(self);
172
+ VALUE shift, result;
173
+ long depth = NIL_P(Vdepth) ? 0 : FIX2LONG(Vdepth);
174
+ VALUE delim = rb_str_new2(",");
175
+ GET_STATE(Vstate);
176
+
177
+ check_max_nesting(state, depth);
178
+ if (state->check_circular) {
179
+ VALUE self_id = rb_obj_id(self);
180
+ rb_hash_aset(state->seen, self_id, Qtrue);
181
+ result = rb_str_buf_new(len);
182
+ if (RSTRING_LEN(state->array_nl)) rb_str_append(delim, state->array_nl);
183
+ shift = rb_str_times(state->indent, LONG2FIX(depth + 1));
184
+
185
+ rb_str_buf_cat2(result, "[");
186
+ OBJ_INFECT(result, self);
187
+ rb_str_buf_append(result, state->array_nl);
188
+ for (i = 0; i < len; i++) {
189
+ VALUE element = RARRAY_PTR(self)[i];
190
+ if (RTEST(rb_hash_aref(state->seen, rb_obj_id(element)))) {
191
+ rb_raise(eCircularDatastructure,
192
+ "circular data structures not supported!");
193
+ }
194
+ OBJ_INFECT(result, element);
195
+ if (i > 0) rb_str_buf_append(result, delim);
196
+ rb_str_buf_append(result, shift);
197
+ element = rb_funcall(element, i_to_json, 2, Vstate, LONG2FIX(depth + 1));
198
+ Check_Type(element, T_STRING);
199
+ rb_str_buf_append(result, element);
200
+ }
201
+ if (RSTRING_LEN(state->array_nl)) {
202
+ rb_str_buf_append(result, state->array_nl);
203
+ rb_str_buf_append(result, rb_str_times(state->indent, LONG2FIX(depth)));
204
+ }
205
+ rb_str_buf_cat2(result, "]");
206
+ rb_hash_delete(state->seen, self_id);
207
+ } else {
208
+ result = rb_str_buf_new(len);
209
+ OBJ_INFECT(result, self);
210
+ if (RSTRING_LEN(state->array_nl)) rb_str_append(delim, state->array_nl);
211
+ shift = rb_str_times(state->indent, LONG2FIX(depth + 1));
212
+
213
+ rb_str_buf_cat2(result, "[");
214
+ rb_str_buf_append(result, state->array_nl);
215
+ for (i = 0; i < len; i++) {
216
+ VALUE element = RARRAY_PTR(self)[i];
217
+ OBJ_INFECT(result, element);
218
+ if (i > 0) rb_str_buf_append(result, delim);
219
+ rb_str_buf_append(result, shift);
220
+ element = rb_funcall(element, i_to_json, 2, Vstate, LONG2FIX(depth + 1));
221
+ Check_Type(element, T_STRING);
222
+ rb_str_buf_append(result, element);
223
+ }
224
+ rb_str_buf_append(result, state->array_nl);
225
+ if (RSTRING_LEN(state->array_nl)) {
226
+ rb_str_buf_append(result, rb_str_times(state->indent, LONG2FIX(depth)));
227
+ }
228
+ rb_str_buf_cat2(result, "]");
229
+ }
230
+ return result;
231
+ }
232
+
233
+ /*
234
+ * call-seq: to_json(state = nil, depth = 0)
235
+ *
236
+ * Returns a JSON string containing a JSON array, that is unparsed from
237
+ * this Array instance.
238
+ * _state_ is a JSON::State object, that can also be used to configure the
239
+ * produced JSON string output further.
240
+ * _depth_ is used to find out nesting depth, to indent accordingly.
241
+ */
242
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
243
+ VALUE Vstate, Vdepth, result;
244
+
245
+ rb_scan_args(argc, argv, "02", &Vstate, &Vdepth);
246
+ if (NIL_P(Vstate)) {
247
+ long i, len = RARRAY_LEN(self);
248
+ result = rb_str_buf_new(2 + 2 * len);
249
+ rb_str_buf_cat2(result, "[");
250
+ OBJ_INFECT(result, self);
251
+ for (i = 0; i < len; i++) {
252
+ VALUE element = RARRAY_PTR(self)[i];
253
+ OBJ_INFECT(result, element);
254
+ if (i > 0) rb_str_buf_cat2(result, ",");
255
+ element = rb_funcall(element, i_to_json, 0);
256
+ Check_Type(element, T_STRING);
257
+ rb_str_buf_append(result, element);
258
+ }
259
+ rb_str_buf_cat2(result, "]");
260
+ } else {
261
+ result = mArray_json_transfrom(self, Vstate, Vdepth);
262
+ }
263
+ OBJ_INFECT(result, self);
264
+ return result;
265
+ }
266
+
267
+ /*
268
+ * call-seq: to_json(*)
269
+ *
270
+ * Returns a JSON string representation for this Integer number.
271
+ */
272
+ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
273
+ {
274
+ return rb_funcall(self, i_to_s, 0);
275
+ }
276
+
277
+ /*
278
+ * call-seq: to_json(*)
279
+ *
280
+ * Returns a JSON string representation for this Float number.
281
+ */
282
+ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
283
+ {
284
+ JSON_Generator_State *state = NULL;
285
+ VALUE Vstate, rest, tmp;
286
+ double value = RFLOAT(self)->value;
287
+ rb_scan_args(argc, argv, "01*", &Vstate, &rest);
288
+ if (!NIL_P(Vstate)) Data_Get_Struct(Vstate, JSON_Generator_State, state);
289
+ if (isinf(value)) {
290
+ if (!state || state->allow_nan) {
291
+ return rb_funcall(self, i_to_s, 0);
292
+ } else {
293
+ tmp = rb_funcall(self, i_to_s, 0);
294
+ rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
295
+ }
296
+ } else if (isnan(value)) {
297
+ if (!state || state->allow_nan) {
298
+ return rb_funcall(self, i_to_s, 0);
299
+ } else {
300
+ tmp = rb_funcall(self, i_to_s, 0);
301
+ rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
302
+ }
303
+ } else {
304
+ return rb_funcall(self, i_to_s, 0);
305
+ }
306
+ }
307
+
308
+ /*
309
+ * call-seq: String.included(modul)
310
+ *
311
+ * Extends _modul_ with the String::Extend module.
312
+ */
313
+ static VALUE mString_included_s(VALUE self, VALUE modul) {
314
+ return rb_funcall(modul, i_extend, 1, mString_Extend);
315
+ }
316
+
317
+ /*
318
+ * call-seq: to_json(*)
319
+ *
320
+ * This string should be encoded with UTF-8 A call to this method
321
+ * returns a JSON string encoded with UTF16 big endian characters as
322
+ * \u????.
323
+ */
324
+ static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
325
+ {
326
+ VALUE result = rb_str_buf_new(RSTRING_LEN(self));
327
+ rb_str_buf_cat2(result, "\"");
328
+ JSON_convert_UTF8_to_JSON(result, self, strictConversion);
329
+ rb_str_buf_cat2(result, "\"");
330
+ return result;
331
+ }
332
+
333
+ /*
334
+ * call-seq: to_json_raw_object()
335
+ *
336
+ * This method creates a raw object hash, that can be nested into
337
+ * other data structures and will be unparsed as a raw string. This
338
+ * method should be used, if you want to convert raw strings to JSON
339
+ * instead of UTF-8 strings, e. g. binary data.
340
+ */
341
+ static VALUE mString_to_json_raw_object(VALUE self) {
342
+ VALUE ary;
343
+ VALUE result = rb_hash_new();
344
+ rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
345
+ ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
346
+ rb_hash_aset(result, rb_str_new2("raw"), ary);
347
+ return result;
348
+ }
349
+
350
+ /*
351
+ * call-seq: to_json_raw(*args)
352
+ *
353
+ * This method creates a JSON text from the result of a call to
354
+ * to_json_raw_object of this String.
355
+ */
356
+ static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self) {
357
+ VALUE obj = mString_to_json_raw_object(self);
358
+ Check_Type(obj, T_HASH);
359
+ return mHash_to_json(argc, argv, obj);
360
+ }
361
+
362
+ /*
363
+ * call-seq: json_create(o)
364
+ *
365
+ * Raw Strings are JSON Objects (the raw bytes are stored in an array for the
366
+ * key "raw"). The Ruby String can be created by this module method.
367
+ */
368
+ static VALUE mString_Extend_json_create(VALUE self, VALUE o) {
369
+ VALUE ary;
370
+ Check_Type(o, T_HASH);
371
+ ary = rb_hash_aref(o, rb_str_new2("raw"));
372
+ return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
373
+ }
374
+
375
+ /*
376
+ * call-seq: to_json(state = nil, depth = 0)
377
+ *
378
+ * Returns a JSON string for true: 'true'.
379
+ */
380
+ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
381
+ {
382
+ return rb_str_new2("true");
383
+ }
384
+
385
+ /*
386
+ * call-seq: to_json(state = nil, depth = 0)
387
+ *
388
+ * Returns a JSON string for false: 'false'.
389
+ */
390
+ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
391
+ {
392
+ return rb_str_new2("false");
393
+ }
394
+
395
+ /*
396
+ * call-seq: to_json(state = nil, depth = 0)
397
+ *
398
+ */
399
+ static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
400
+ {
401
+ return rb_str_new2("null");
402
+ }
403
+
404
+ /*
405
+ * call-seq: to_json(*)
406
+ *
407
+ * Converts this object to a string (calling #to_s), converts
408
+ * it to a JSON string, and returns the result. This is a fallback, if no
409
+ * special method #to_json was defined for some object.
410
+ */
411
+ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
412
+ {
413
+ VALUE string = rb_funcall(self, i_to_s, 0);
414
+ Check_Type(string, T_STRING);
415
+ return mString_to_json(argc, argv, string);
416
+ }
417
+
418
+ /*
419
+ * Document-class: JSON::Ext::Generator::State
420
+ *
421
+ * This class is used to create State instances, that are use to hold data
422
+ * while generating a JSON text from a a Ruby data structure.
423
+ */
424
+
425
+ static void State_mark(JSON_Generator_State *state)
426
+ {
427
+ rb_gc_mark_maybe(state->indent);
428
+ rb_gc_mark_maybe(state->space);
429
+ rb_gc_mark_maybe(state->space_before);
430
+ rb_gc_mark_maybe(state->object_nl);
431
+ rb_gc_mark_maybe(state->array_nl);
432
+ rb_gc_mark_maybe(state->seen);
433
+ rb_gc_mark_maybe(state->memo);
434
+ rb_gc_mark_maybe(state->depth);
435
+ }
436
+
437
+ static JSON_Generator_State *State_allocate()
438
+ {
439
+ JSON_Generator_State *state = ALLOC(JSON_Generator_State);
440
+ return state;
441
+ }
442
+
443
+ static VALUE cState_s_allocate(VALUE klass)
444
+ {
445
+ JSON_Generator_State *state = State_allocate();
446
+ return Data_Wrap_Struct(klass, State_mark, -1, state);
447
+ }
448
+
449
+ /*
450
+ * call-seq: configure(opts)
451
+ *
452
+ * Configure this State instance with the Hash _opts_, and return
453
+ * itself.
454
+ */
455
+ static inline VALUE cState_configure(VALUE self, VALUE opts)
456
+ {
457
+ VALUE tmp;
458
+ GET_STATE(self);
459
+ tmp = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
460
+ if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
461
+ if (NIL_P(tmp)) {
462
+ rb_raise(rb_eArgError, "opts has to be hash like or convertable into a hash");
463
+ }
464
+ opts = tmp;
465
+ tmp = rb_hash_aref(opts, ID2SYM(i_indent));
466
+ if (RTEST(tmp)) {
467
+ Check_Type(tmp, T_STRING);
468
+ state->indent = tmp;
469
+ }
470
+ tmp = rb_hash_aref(opts, ID2SYM(i_space));
471
+ if (RTEST(tmp)) {
472
+ Check_Type(tmp, T_STRING);
473
+ state->space = tmp;
474
+ }
475
+ tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
476
+ if (RTEST(tmp)) {
477
+ Check_Type(tmp, T_STRING);
478
+ state->space_before = tmp;
479
+ }
480
+ tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
481
+ if (RTEST(tmp)) {
482
+ Check_Type(tmp, T_STRING);
483
+ state->array_nl = tmp;
484
+ }
485
+ tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
486
+ if (RTEST(tmp)) {
487
+ Check_Type(tmp, T_STRING);
488
+ state->object_nl = tmp;
489
+ }
490
+ tmp = ID2SYM(i_check_circular);
491
+ if (st_lookup(RHASH(opts)->tbl, tmp, 0)) {
492
+ tmp = rb_hash_aref(opts, ID2SYM(i_check_circular));
493
+ state->check_circular = RTEST(tmp);
494
+ } else {
495
+ state->check_circular = 1;
496
+ }
497
+ tmp = ID2SYM(i_max_nesting);
498
+ state->max_nesting = 19;
499
+ if (st_lookup(RHASH(opts)->tbl, tmp, 0)) {
500
+ VALUE max_nesting = rb_hash_aref(opts, tmp);
501
+ if (RTEST(max_nesting)) {
502
+ Check_Type(max_nesting, T_FIXNUM);
503
+ state->max_nesting = FIX2LONG(max_nesting);
504
+ } else {
505
+ state->max_nesting = 0;
506
+ }
507
+ }
508
+ tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
509
+ state->allow_nan = RTEST(tmp);
510
+ return self;
511
+ }
512
+
513
+ /*
514
+ * call-seq: to_h
515
+ *
516
+ * Returns the configuration instance variables as a hash, that can be
517
+ * passed to the configure method.
518
+ */
519
+ static VALUE cState_to_h(VALUE self)
520
+ {
521
+ VALUE result = rb_hash_new();
522
+ GET_STATE(self);
523
+ rb_hash_aset(result, ID2SYM(i_indent), state->indent);
524
+ rb_hash_aset(result, ID2SYM(i_space), state->space);
525
+ rb_hash_aset(result, ID2SYM(i_space_before), state->space_before);
526
+ rb_hash_aset(result, ID2SYM(i_object_nl), state->object_nl);
527
+ rb_hash_aset(result, ID2SYM(i_array_nl), state->array_nl);
528
+ rb_hash_aset(result, ID2SYM(i_check_circular), state->check_circular ? Qtrue : Qfalse);
529
+ rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
530
+ rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
531
+ return result;
532
+ }
533
+
534
+
535
+ /*
536
+ * call-seq: new(opts = {})
537
+ *
538
+ * Instantiates a new State object, configured by _opts_.
539
+ *
540
+ * _opts_ can have the following keys:
541
+ *
542
+ * * *indent*: a string used to indent levels (default: ''),
543
+ * * *space*: a string that is put after, a : or , delimiter (default: ''),
544
+ * * *space_before*: a string that is put before a : pair delimiter (default: ''),
545
+ * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
546
+ * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
547
+ * * *check_circular*: true if checking for circular data structures
548
+ * should be done, false (the default) otherwise.
549
+ * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
550
+ * generated, otherwise an exception is thrown, if these values are
551
+ * encountered. This options defaults to false.
552
+ */
553
+ static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
554
+ {
555
+ VALUE opts;
556
+ GET_STATE(self);
557
+
558
+ rb_scan_args(argc, argv, "01", &opts);
559
+ state->indent = rb_str_new2("");
560
+ state->space = rb_str_new2("");
561
+ state->space_before = rb_str_new2("");
562
+ state->array_nl = rb_str_new2("");
563
+ state->object_nl = rb_str_new2("");
564
+ if (NIL_P(opts)) {
565
+ state->check_circular = 1;
566
+ state->allow_nan = 0;
567
+ state->max_nesting = 19;
568
+ } else {
569
+ cState_configure(self, opts);
570
+ }
571
+ state->seen = rb_hash_new();
572
+ state->memo = Qnil;
573
+ state->depth = INT2FIX(0);
574
+ return self;
575
+ }
576
+
577
+ /*
578
+ * call-seq: from_state(opts)
579
+ *
580
+ * Creates a State object from _opts_, which ought to be Hash to create a
581
+ * new State instance configured by _opts_, something else to create an
582
+ * unconfigured instance. If _opts_ is a State object, it is just returned.
583
+ */
584
+ static VALUE cState_from_state_s(VALUE self, VALUE opts)
585
+ {
586
+ if (rb_obj_is_kind_of(opts, self)) {
587
+ return opts;
588
+ } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
589
+ return rb_funcall(self, i_new, 1, opts);
590
+ } else {
591
+ return rb_funcall(self, i_new, 0);
592
+ }
593
+ }
594
+
595
+ /*
596
+ * call-seq: indent()
597
+ *
598
+ * This string is used to indent levels in the JSON text.
599
+ */
600
+ static VALUE cState_indent(VALUE self)
601
+ {
602
+ GET_STATE(self);
603
+ return state->indent;
604
+ }
605
+
606
+ /*
607
+ * call-seq: indent=(indent)
608
+ *
609
+ * This string is used to indent levels in the JSON text.
610
+ */
611
+ static VALUE cState_indent_set(VALUE self, VALUE indent)
612
+ {
613
+ GET_STATE(self);
614
+ Check_Type(indent, T_STRING);
615
+ return state->indent = indent;
616
+ }
617
+
618
+ /*
619
+ * call-seq: space()
620
+ *
621
+ * This string is used to insert a space between the tokens in a JSON
622
+ * string.
623
+ */
624
+ static VALUE cState_space(VALUE self)
625
+ {
626
+ GET_STATE(self);
627
+ return state->space;
628
+ }
629
+
630
+ /*
631
+ * call-seq: space=(space)
632
+ *
633
+ * This string is used to insert a space between the tokens in a JSON
634
+ * string.
635
+ */
636
+ static VALUE cState_space_set(VALUE self, VALUE space)
637
+ {
638
+ GET_STATE(self);
639
+ Check_Type(space, T_STRING);
640
+ return state->space = space;
641
+ }
642
+
643
+ /*
644
+ * call-seq: space_before()
645
+ *
646
+ * This string is used to insert a space before the ':' in JSON objects.
647
+ */
648
+ static VALUE cState_space_before(VALUE self)
649
+ {
650
+ GET_STATE(self);
651
+ return state->space_before;
652
+ }
653
+
654
+ /*
655
+ * call-seq: space_before=(space_before)
656
+ *
657
+ * This string is used to insert a space before the ':' in JSON objects.
658
+ */
659
+ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
660
+ {
661
+ GET_STATE(self);
662
+ Check_Type(space_before, T_STRING);
663
+ return state->space_before = space_before;
664
+ }
665
+
666
+ /*
667
+ * call-seq: object_nl()
668
+ *
669
+ * This string is put at the end of a line that holds a JSON object (or
670
+ * Hash).
671
+ */
672
+ static VALUE cState_object_nl(VALUE self)
673
+ {
674
+ GET_STATE(self);
675
+ return state->object_nl;
676
+ }
677
+
678
+ /*
679
+ * call-seq: object_nl=(object_nl)
680
+ *
681
+ * This string is put at the end of a line that holds a JSON object (or
682
+ * Hash).
683
+ */
684
+ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
685
+ {
686
+ GET_STATE(self);
687
+ Check_Type(object_nl, T_STRING);
688
+ return state->object_nl = object_nl;
689
+ }
690
+
691
+ /*
692
+ * call-seq: array_nl()
693
+ *
694
+ * This string is put at the end of a line that holds a JSON array.
695
+ */
696
+ static VALUE cState_array_nl(VALUE self)
697
+ {
698
+ GET_STATE(self);
699
+ return state->array_nl;
700
+ }
701
+
702
+ /*
703
+ * call-seq: array_nl=(array_nl)
704
+ *
705
+ * This string is put at the end of a line that holds a JSON array.
706
+ */
707
+ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
708
+ {
709
+ GET_STATE(self);
710
+ Check_Type(array_nl, T_STRING);
711
+ return state->array_nl = array_nl;
712
+ }
713
+
714
+ /*
715
+ * call-seq: check_circular?
716
+ *
717
+ * Returns true, if circular data structures should be checked,
718
+ * otherwise returns false.
719
+ */
720
+ static VALUE cState_check_circular_p(VALUE self)
721
+ {
722
+ GET_STATE(self);
723
+ return state->check_circular ? Qtrue : Qfalse;
724
+ }
725
+
726
+ /*
727
+ * call-seq: max_nesting
728
+ *
729
+ * This integer returns the maximum level of data structure nesting in
730
+ * the generated JSON, max_nesting = 0 if no maximum is checked.
731
+ */
732
+ static VALUE cState_max_nesting(VALUE self)
733
+ {
734
+ GET_STATE(self);
735
+ return LONG2FIX(state->max_nesting);
736
+ }
737
+
738
+ /*
739
+ * call-seq: max_nesting=(depth)
740
+ *
741
+ * This sets the maximum level of data structure nesting in the generated JSON
742
+ * to the integer depth, max_nesting = 0 if no maximum should be checked.
743
+ */
744
+ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
745
+ {
746
+ GET_STATE(self);
747
+ Check_Type(depth, T_FIXNUM);
748
+ state->max_nesting = FIX2LONG(depth);
749
+ return Qnil;
750
+ }
751
+
752
+ /*
753
+ * call-seq: allow_nan?
754
+ *
755
+ * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
756
+ * returns false.
757
+ */
758
+ static VALUE cState_allow_nan_p(VALUE self)
759
+ {
760
+ GET_STATE(self);
761
+ return state->allow_nan ? Qtrue : Qfalse;
762
+ }
763
+
764
+ /*
765
+ * call-seq: seen?(object)
766
+ *
767
+ * Returns _true_, if _object_ was already seen during this generating run.
768
+ */
769
+ static VALUE cState_seen_p(VALUE self, VALUE object)
770
+ {
771
+ GET_STATE(self);
772
+ return rb_hash_aref(state->seen, rb_obj_id(object));
773
+ }
774
+
775
+ /*
776
+ * call-seq: remember(object)
777
+ *
778
+ * Remember _object_, to find out if it was already encountered (if a cyclic
779
+ * data structure is rendered).
780
+ */
781
+ static VALUE cState_remember(VALUE self, VALUE object)
782
+ {
783
+ GET_STATE(self);
784
+ return rb_hash_aset(state->seen, rb_obj_id(object), Qtrue);
785
+ }
786
+
787
+ /*
788
+ * call-seq: forget(object)
789
+ *
790
+ * Forget _object_ for this generating run.
791
+ */
792
+ static VALUE cState_forget(VALUE self, VALUE object)
793
+ {
794
+ GET_STATE(self);
795
+ return rb_hash_delete(state->seen, rb_obj_id(object));
796
+ }
797
+
798
+ /*
799
+ *
800
+ */
801
+ void Init_generator()
802
+ {
803
+ rb_require("json/common");
804
+ mJSON = rb_define_module("JSON");
805
+ mExt = rb_define_module_under(mJSON, "Ext");
806
+ mGenerator = rb_define_module_under(mExt, "Generator");
807
+ eGeneratorError = rb_path2class("JSON::GeneratorError");
808
+ eCircularDatastructure = rb_path2class("JSON::CircularDatastructure");
809
+ eNestingError = rb_path2class("JSON::NestingError");
810
+ cState = rb_define_class_under(mGenerator, "State", rb_cObject);
811
+ rb_define_alloc_func(cState, cState_s_allocate);
812
+ rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
813
+ rb_define_method(cState, "initialize", cState_initialize, -1);
814
+
815
+ rb_define_method(cState, "indent", cState_indent, 0);
816
+ rb_define_method(cState, "indent=", cState_indent_set, 1);
817
+ rb_define_method(cState, "space", cState_space, 0);
818
+ rb_define_method(cState, "space=", cState_space_set, 1);
819
+ rb_define_method(cState, "space_before", cState_space_before, 0);
820
+ rb_define_method(cState, "space_before=", cState_space_before_set, 1);
821
+ rb_define_method(cState, "object_nl", cState_object_nl, 0);
822
+ rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
823
+ rb_define_method(cState, "array_nl", cState_array_nl, 0);
824
+ rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
825
+ rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
826
+ rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
827
+ rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
828
+ rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
829
+ rb_define_method(cState, "seen?", cState_seen_p, 1);
830
+ rb_define_method(cState, "remember", cState_remember, 1);
831
+ rb_define_method(cState, "forget", cState_forget, 1);
832
+ rb_define_method(cState, "configure", cState_configure, 1);
833
+ rb_define_method(cState, "to_h", cState_to_h, 0);
834
+
835
+ mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
836
+ mObject = rb_define_module_under(mGeneratorMethods, "Object");
837
+ rb_define_method(mObject, "to_json", mObject_to_json, -1);
838
+ mHash = rb_define_module_under(mGeneratorMethods, "Hash");
839
+ rb_define_method(mHash, "to_json", mHash_to_json, -1);
840
+ mArray = rb_define_module_under(mGeneratorMethods, "Array");
841
+ rb_define_method(mArray, "to_json", mArray_to_json, -1);
842
+ mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
843
+ rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
844
+ mFloat = rb_define_module_under(mGeneratorMethods, "Float");
845
+ rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
846
+ mString = rb_define_module_under(mGeneratorMethods, "String");
847
+ rb_define_singleton_method(mString, "included", mString_included_s, 1);
848
+ rb_define_method(mString, "to_json", mString_to_json, -1);
849
+ rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
850
+ rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
851
+ mString_Extend = rb_define_module_under(mString, "Extend");
852
+ rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
853
+ mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
854
+ rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
855
+ mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
856
+ rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
857
+ mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
858
+ rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
859
+
860
+ i_to_s = rb_intern("to_s");
861
+ i_to_json = rb_intern("to_json");
862
+ i_new = rb_intern("new");
863
+ i_indent = rb_intern("indent");
864
+ i_space = rb_intern("space");
865
+ i_space_before = rb_intern("space_before");
866
+ i_object_nl = rb_intern("object_nl");
867
+ i_array_nl = rb_intern("array_nl");
868
+ i_check_circular = rb_intern("check_circular");
869
+ i_max_nesting = rb_intern("max_nesting");
870
+ i_allow_nan = rb_intern("allow_nan");
871
+ i_pack = rb_intern("pack");
872
+ i_unpack = rb_intern("unpack");
873
+ i_create_id = rb_intern("create_id");
874
+ i_extend = rb_intern("extend");
875
+ }