genki-json 1.1.4.1

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 (86) hide show
  1. data/CHANGES +97 -0
  2. data/GPL +340 -0
  3. data/README +78 -0
  4. data/RUBY +58 -0
  5. data/Rakefile +297 -0
  6. data/TODO +1 -0
  7. data/VERSION +1 -0
  8. data/benchmarks/generator_benchmark.rb +141 -0
  9. data/benchmarks/parser_benchmark.rb +129 -0
  10. data/bin/edit_json.rb +10 -0
  11. data/bin/prettify_json.rb +76 -0
  12. data/data/example.json +1 -0
  13. data/data/index.html +38 -0
  14. data/data/prototype.js +4184 -0
  15. data/doc-templates/main.txt +219 -0
  16. data/ext/json/ext/generator/extconf.rb +9 -0
  17. data/ext/json/ext/generator/generator.c +875 -0
  18. data/ext/json/ext/generator/unicode.c +182 -0
  19. data/ext/json/ext/generator/unicode.h +53 -0
  20. data/ext/json/ext/parser/extconf.rb +9 -0
  21. data/ext/json/ext/parser/parser.c +1758 -0
  22. data/ext/json/ext/parser/parser.rl +638 -0
  23. data/ext/json/ext/parser/unicode.c +154 -0
  24. data/ext/json/ext/parser/unicode.h +58 -0
  25. data/install.rb +26 -0
  26. data/json.gemspec +34 -0
  27. data/lib/json.rb +14 -0
  28. data/lib/json/Array.xpm +21 -0
  29. data/lib/json/FalseClass.xpm +21 -0
  30. data/lib/json/Hash.xpm +21 -0
  31. data/lib/json/Key.xpm +73 -0
  32. data/lib/json/NilClass.xpm +21 -0
  33. data/lib/json/Numeric.xpm +28 -0
  34. data/lib/json/String.xpm +96 -0
  35. data/lib/json/TrueClass.xpm +21 -0
  36. data/lib/json/add/core.rb +135 -0
  37. data/lib/json/add/rails.rb +58 -0
  38. data/lib/json/common.rb +354 -0
  39. data/lib/json/editor.rb +1362 -0
  40. data/lib/json/ext.rb +15 -0
  41. data/lib/json/json.xpm +1499 -0
  42. data/lib/json/pure.rb +77 -0
  43. data/lib/json/pure/generator.rb +404 -0
  44. data/lib/json/pure/parser.rb +259 -0
  45. data/lib/json/version.rb +9 -0
  46. data/tests/fixtures/fail1.json +1 -0
  47. data/tests/fixtures/fail10.json +1 -0
  48. data/tests/fixtures/fail11.json +1 -0
  49. data/tests/fixtures/fail12.json +1 -0
  50. data/tests/fixtures/fail13.json +1 -0
  51. data/tests/fixtures/fail14.json +1 -0
  52. data/tests/fixtures/fail18.json +1 -0
  53. data/tests/fixtures/fail19.json +1 -0
  54. data/tests/fixtures/fail2.json +1 -0
  55. data/tests/fixtures/fail20.json +1 -0
  56. data/tests/fixtures/fail21.json +1 -0
  57. data/tests/fixtures/fail22.json +1 -0
  58. data/tests/fixtures/fail23.json +1 -0
  59. data/tests/fixtures/fail24.json +1 -0
  60. data/tests/fixtures/fail25.json +1 -0
  61. data/tests/fixtures/fail27.json +2 -0
  62. data/tests/fixtures/fail28.json +2 -0
  63. data/tests/fixtures/fail3.json +1 -0
  64. data/tests/fixtures/fail4.json +1 -0
  65. data/tests/fixtures/fail5.json +1 -0
  66. data/tests/fixtures/fail6.json +1 -0
  67. data/tests/fixtures/fail7.json +1 -0
  68. data/tests/fixtures/fail8.json +1 -0
  69. data/tests/fixtures/fail9.json +1 -0
  70. data/tests/fixtures/pass1.json +56 -0
  71. data/tests/fixtures/pass15.json +1 -0
  72. data/tests/fixtures/pass16.json +1 -0
  73. data/tests/fixtures/pass17.json +1 -0
  74. data/tests/fixtures/pass2.json +1 -0
  75. data/tests/fixtures/pass26.json +1 -0
  76. data/tests/fixtures/pass3.json +6 -0
  77. data/tests/runner.rb +25 -0
  78. data/tests/test_json.rb +293 -0
  79. data/tests/test_json_addition.rb +161 -0
  80. data/tests/test_json_fixtures.rb +30 -0
  81. data/tests/test_json_generate.rb +100 -0
  82. data/tests/test_json_rails.rb +143 -0
  83. data/tests/test_json_unicode.rb +61 -0
  84. data/tools/fuzz.rb +140 -0
  85. data/tools/server.rb +62 -0
  86. metadata +161 -0
@@ -0,0 +1,219 @@
1
+ # = json - JSON for Ruby
2
+ #
3
+ # == Description
4
+ #
5
+ # This is a implementation of the JSON specification according to RFC 4627
6
+ # (http://www.ietf.org/rfc/rfc4627.txt). Starting from version 1.0.0 on there
7
+ # will be two variants available:
8
+ #
9
+ # * A pure ruby variant, that relies on the iconv and the stringscan
10
+ # extensions, which are both part of the ruby standard library.
11
+ # * The quite a bit faster C extension variant, which is in parts implemented
12
+ # in C and comes with its own unicode conversion functions and a parser
13
+ # generated by the ragel state machine compiler
14
+ # (http://www.cs.queensu.ca/~thurston/ragel).
15
+ #
16
+ # Both variants of the JSON generator escape all non-ASCII an control
17
+ # characters with \uXXXX escape sequences, and support UTF-16 surrogate pairs
18
+ # in order to be able to generate the whole range of unicode code points. This
19
+ # means that generated JSON text is encoded as UTF-8 (because ASCII is a subset
20
+ # of UTF-8) and at the same time avoids decoding problems for receiving
21
+ # endpoints, that don't expect UTF-8 encoded texts. On the negative side this
22
+ # may lead to a bit longer strings than necessarry.
23
+ #
24
+ # All strings, that are to be encoded as JSON strings, should be UTF-8 byte
25
+ # sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
26
+ # encoded, please use the to_json_raw_object method of String (which produces
27
+ # an object, that contains a byte array) and decode the result on the receiving
28
+ # endpoint.
29
+ #
30
+ # == Author
31
+ #
32
+ # Florian Frank <mailto:flori@ping.de>
33
+ #
34
+ # == License
35
+ #
36
+ # This software is distributed under the same license as Ruby itself, see
37
+ # http://www.ruby-lang.org/en/LICENSE.txt.
38
+ #
39
+ # == Download
40
+ #
41
+ # The latest version of this library can be downloaded at
42
+ #
43
+ # * http://rubyforge.org/frs?group_id=953
44
+ #
45
+ # Online Documentation should be located at
46
+ #
47
+ # * http://json.rubyforge.org
48
+ #
49
+ # == Usage
50
+ #
51
+ # To use JSON you can
52
+ # require 'json'
53
+ # to load the installed variant (either the extension 'json' or the pure
54
+ # variant 'json_pure'). If you have installed the extension variant, you can
55
+ # pick either the extension variant or the pure variant by typing
56
+ # require 'json/ext'
57
+ # or
58
+ # require 'json/pure'
59
+ #
60
+ # You can choose to load a set of common additions to ruby core's objects if
61
+ # you
62
+ # require 'json/add/core'
63
+ #
64
+ # After requiring this you can, e. g., serialise/deserialise Ruby ranges:
65
+ #
66
+ # JSON JSON(1..10) # => 1..10
67
+ #
68
+ # To find out how to add JSON support to other or your own classes, read the
69
+ # Examples section below.
70
+ #
71
+ # To get the best compatibility to rails' JSON implementation, you can
72
+ # require 'json/add/rails'
73
+ #
74
+ # Both of the additions attempt to require 'json' (like above) first, if it has
75
+ # not been required yet.
76
+ #
77
+ # == Speed Comparisons
78
+ #
79
+ # I have created some benchmark results (see the benchmarks subdir of the
80
+ # package) for the JSON-Parser to estimate the speed up in the C extension:
81
+ #
82
+ # JSON::Pure::Parser:: 28.90 calls/second
83
+ # JSON::Ext::Parser:: 505.50 calls/second
84
+ #
85
+ # This is ca. <b>17.5</b> times the speed of the pure Ruby implementation.
86
+ #
87
+ # I have benchmarked the JSON-Generator as well. This generates a few more
88
+ # values, because there are different modes, that also influence the achieved
89
+ # speed:
90
+ #
91
+ # * JSON::Pure::Generator:
92
+ # generate:: 35.06 calls/second
93
+ # pretty_generate:: 34.00 calls/second
94
+ # fast_generate:: 41.06 calls/second
95
+ #
96
+ # * JSON::Ext::Generator:
97
+ # generate:: 492.11 calls/second
98
+ # pretty_generate:: 348.85 calls/second
99
+ # fast_generate:: 541.60 calls/second
100
+ #
101
+ # * Speedup Ext/Pure:
102
+ # generate safe:: 14.0 times
103
+ # generate pretty:: 10.3 times
104
+ # generate fast:: 13.2 times
105
+ #
106
+ # The rails framework includes a generator as well, also it seems to be rather
107
+ # slow: I measured only 23.87 calls/second which is slower than any of my pure
108
+ # generator results. Here a comparison of the different speedups with the Rails
109
+ # measurement as the divisor:
110
+ #
111
+ # * Speedup Pure/Rails:
112
+ # generate safe:: 1.5 times
113
+ # generate pretty:: 1.4 times
114
+ # generate fast:: 1.7 times
115
+ #
116
+ # * Speedup Ext/Rails:
117
+ # generate safe:: 20.6 times
118
+ # generate pretty:: 14.6 times
119
+ # generate fast:: 22.7 times
120
+ #
121
+ # To achieve the fastest JSON text output, you can use the
122
+ # fast_generate/fast_unparse methods. Beware, that this will disable the
123
+ # checking for circular Ruby data structures, which may cause JSON to go into
124
+ # an infinite loop.
125
+ #
126
+ # == Examples
127
+ #
128
+ # To create a JSON text from a ruby data structure, you
129
+ # can call JSON.generate (or JSON.unparse) like that:
130
+ #
131
+ # json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
132
+ # # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
133
+ #
134
+ # To create a valid JSON text you have to make sure, that the output is
135
+ # embedded in either a JSON array [] or a JSON object {}. The easiest way to do
136
+ # this, is by putting your values in a Ruby Array or Hash instance.
137
+ #
138
+ # To get back a ruby data structure from a JSON text, you have to call
139
+ # JSON.parse on it:
140
+ #
141
+ # JSON.parse json
142
+ # # => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
143
+ #
144
+ # Note, that the range from the original data structure is a simple
145
+ # string now. The reason for this is, that JSON doesn't support ranges
146
+ # or arbitrary classes. In this case the json library falls back to call
147
+ # Object#to_json, which is the same as #to_s.to_json.
148
+ #
149
+ # It's possible to add JSON support serialization to arbitrary classes by
150
+ # simply implementing a more specialized version of the #to_json method, that
151
+ # should return a JSON object (a hash converted to JSON with #to_json) like
152
+ # this (don't forget the *a for all the arguments):
153
+ #
154
+ # class Range
155
+ # def to_json(*a)
156
+ # {
157
+ # 'json_class' => self.class.name, # = 'Range'
158
+ # 'data' => [ first, last, exclude_end? ]
159
+ # }.to_json(*a)
160
+ # end
161
+ # end
162
+ #
163
+ # The hash key 'json_class' is the class, that will be asked to deserialise the
164
+ # JSON representation later. In this case it's 'Range', but any namespace of
165
+ # the form 'A::B' or '::A::B' will do. All other keys are arbitrary and can be
166
+ # used to store the necessary data to configure the object to be deserialised.
167
+ #
168
+ # If a the key 'json_class' is found in a JSON object, the JSON parser checks
169
+ # if the given class responds to the json_create class method. If so, it is
170
+ # called with the JSON object converted to a Ruby hash. So a range can
171
+ # be deserialised by implementing Range.json_create like this:
172
+ #
173
+ # class Range
174
+ # def self.json_create(o)
175
+ # new(*o['data'])
176
+ # end
177
+ # end
178
+ #
179
+ # Now it possible to serialise/deserialise ranges as well:
180
+ #
181
+ # json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
182
+ # # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
183
+ # JSON.parse json
184
+ # # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
185
+ #
186
+ # JSON.generate always creates the shortest possible string representation of a
187
+ # ruby data structure in one line. This good for data storage or network
188
+ # protocols, but not so good for humans to read. Fortunately there's also
189
+ # JSON.pretty_generate (or JSON.pretty_generate) that creates a more
190
+ # readable output:
191
+ #
192
+ # puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
193
+ # [
194
+ # 1,
195
+ # 2,
196
+ # {
197
+ # "a": 3.141
198
+ # },
199
+ # false,
200
+ # true,
201
+ # null,
202
+ # {
203
+ # "json_class": "Range",
204
+ # "data": [
205
+ # 4,
206
+ # 10,
207
+ # false
208
+ # ]
209
+ # }
210
+ # ]
211
+ #
212
+ # There are also the methods Kernel#j for unparse, and Kernel#jj for
213
+ # pretty_unparse output to the console, that work analogous to Core Ruby's p
214
+ # and the pp library's pp methods.
215
+ #
216
+ # The script tools/server.rb contains a small example if you want to test, how
217
+ # receiving a JSON object from a webrick server in your browser with the
218
+ # javasript prototype library (http://www.prototypejs.org) works.
219
+ #
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+ require 'rbconfig'
3
+
4
+ if CONFIG['CC'] =~ /gcc/
5
+ $CFLAGS += ' -Wall'
6
+ #$CFLAGS += ' -O0 -ggdb'
7
+ end
8
+
9
+ create_makefile 'generator'
@@ -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_TBL(self)->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_TBL(self)->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_VALUE(self);
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_TBL(opts), 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_TBL(opts), 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
+ }