json 1.2.4 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

@@ -1,11 +0,0 @@
1
- require 'mkmf'
2
- require 'rbconfig'
3
-
4
- if CONFIG['CC'] =~ /gcc/
5
- $CFLAGS += ' -Wall'
6
- #$CFLAGS += ' -O0 -ggdb'
7
- end
8
-
9
- have_header("ruby/st.h") || have_header("st.h")
10
- have_header("ruby/encoding.h")
11
- create_makefile 'generator'
@@ -1,953 +0,0 @@
1
- #include <string.h>
2
- #include "ruby.h"
3
- #if HAVE_RUBY_ST_H
4
- #include "ruby/st.h"
5
- #endif
6
- #if HAVE_ST_H
7
- #include "st.h"
8
- #endif
9
- #include "unicode.h"
10
- #include <math.h>
11
-
12
- #ifndef RHASH_SIZE
13
- #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
14
- #endif
15
-
16
- #ifndef RFLOAT_VALUE
17
- #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
18
- #endif
19
-
20
- #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
21
-
22
- #ifdef HAVE_RUBY_ENCODING_H
23
- #include "ruby/encoding.h"
24
- #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
25
- static VALUE mEncoding_UTF_8;
26
- static ID i_encoding, i_encode;
27
- #else
28
- #define FORCE_UTF8(obj)
29
- #endif
30
-
31
- #define check_max_nesting(state, depth) do { \
32
- long current_nesting = 1 + depth; \
33
- if (state->max_nesting != 0 && current_nesting > state->max_nesting) \
34
- rb_raise(eNestingError, "nesting of %ld is too deep", current_nesting); \
35
- } while (0);
36
-
37
- static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
38
- mHash, mArray, mInteger, mFloat, mString, mString_Extend,
39
- mTrueClass, mFalseClass, mNilClass, eGeneratorError,
40
- eCircularDatastructure, eNestingError;
41
-
42
- static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
43
- i_object_nl, i_array_nl, i_check_circular, i_max_nesting,
44
- i_allow_nan, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
45
- i_aref, i_send, i_respond_to_p;
46
-
47
- typedef struct JSON_Generator_StateStruct {
48
- VALUE indent;
49
- VALUE space;
50
- VALUE space_before;
51
- VALUE object_nl;
52
- VALUE array_nl;
53
- int check_circular;
54
- VALUE seen;
55
- VALUE memo;
56
- VALUE depth;
57
- long max_nesting;
58
- int flag;
59
- int allow_nan;
60
- } JSON_Generator_State;
61
-
62
- #define GET_STATE(self) \
63
- JSON_Generator_State *state; \
64
- Data_Get_Struct(self, JSON_Generator_State, state);
65
-
66
- /*
67
- * Document-module: JSON::Ext::Generator
68
- *
69
- * This is the JSON generator implemented as a C extension. It can be
70
- * configured to be used by setting
71
- *
72
- * JSON.generator = JSON::Ext::Generator
73
- *
74
- * with the method generator= in JSON.
75
- *
76
- */
77
-
78
- static int hash_to_json_state_i(VALUE key, VALUE value, VALUE Vstate)
79
- {
80
- VALUE json, buf, Vdepth;
81
- GET_STATE(Vstate);
82
- buf = state->memo;
83
- Vdepth = state->depth;
84
-
85
- if (key == Qundef) return ST_CONTINUE;
86
- if (state->flag) {
87
- state->flag = 0;
88
- rb_str_buf_cat2(buf, ",");
89
- if (RSTRING_LEN(state->object_nl)) rb_str_buf_append(buf, state->object_nl);
90
- }
91
- if (RSTRING_LEN(state->object_nl)) {
92
- rb_str_buf_append(buf, rb_str_times(state->indent, Vdepth));
93
- }
94
- json = rb_funcall(rb_funcall(key, i_to_s, 0), i_to_json, 2, Vstate, Vdepth);
95
- Check_Type(json, T_STRING);
96
- rb_str_buf_append(buf, json);
97
- OBJ_INFECT(buf, json);
98
- if (RSTRING_LEN(state->space_before)) {
99
- rb_str_buf_append(buf, state->space_before);
100
- }
101
- rb_str_buf_cat2(buf, ":");
102
- if (RSTRING_LEN(state->space)) rb_str_buf_append(buf, state->space);
103
- json = rb_funcall(value, i_to_json, 2, Vstate, Vdepth);
104
- Check_Type(json, T_STRING);
105
- state->flag = 1;
106
- rb_str_buf_append(buf, json);
107
- OBJ_INFECT(buf, json);
108
- state->depth = Vdepth;
109
- state->memo = buf;
110
- return ST_CONTINUE;
111
- }
112
-
113
- inline static VALUE mHash_json_transfrom(VALUE self, VALUE Vstate, VALUE Vdepth) {
114
- long depth, len = RHASH_SIZE(self);
115
- VALUE result;
116
- GET_STATE(Vstate);
117
-
118
- depth = 1 + FIX2LONG(Vdepth);
119
- result = rb_str_buf_new(len);
120
- state->memo = result;
121
- state->depth = LONG2FIX(depth);
122
- state->flag = 0;
123
- rb_str_buf_cat2(result, "{");
124
- if (RSTRING_LEN(state->object_nl)) rb_str_buf_append(result, state->object_nl);
125
- rb_hash_foreach(self, hash_to_json_state_i, Vstate);
126
- if (RSTRING_LEN(state->object_nl)) rb_str_buf_append(result, state->object_nl);
127
- if (RSTRING_LEN(state->object_nl)) {
128
- rb_str_buf_append(result, rb_str_times(state->indent, Vdepth));
129
- }
130
- rb_str_buf_cat2(result, "}");
131
- return result;
132
- }
133
-
134
- static int hash_to_json_i(VALUE key, VALUE value, VALUE buf)
135
- {
136
- VALUE tmp;
137
-
138
- if (key == Qundef) return ST_CONTINUE;
139
- if (RSTRING_LEN(buf) > 1) rb_str_buf_cat2(buf, ",");
140
- tmp = rb_funcall(rb_funcall(key, i_to_s, 0), i_to_json, 0);
141
- Check_Type(tmp, T_STRING);
142
- rb_str_buf_append(buf, tmp);
143
- OBJ_INFECT(buf, tmp);
144
- rb_str_buf_cat2(buf, ":");
145
- tmp = rb_funcall(value, i_to_json, 0);
146
- Check_Type(tmp, T_STRING);
147
- rb_str_buf_append(buf, tmp);
148
- OBJ_INFECT(buf, tmp);
149
-
150
- return ST_CONTINUE;
151
- }
152
-
153
- /*
154
- * call-seq: to_json(state = nil, depth = 0)
155
- *
156
- * Returns a JSON string containing a JSON object, that is unparsed from
157
- * this Hash instance.
158
- * _state_ is a JSON::State object, that can also be used to configure the
159
- * produced JSON string output further.
160
- * _depth_ is used to find out nesting depth, to indent accordingly.
161
- */
162
- static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
163
- {
164
- VALUE Vstate, Vdepth, result;
165
- long depth;
166
-
167
- rb_scan_args(argc, argv, "02", &Vstate, &Vdepth);
168
- depth = NIL_P(Vdepth) ? 0 : FIX2LONG(Vdepth);
169
- if (NIL_P(Vstate)) {
170
- long len = RHASH_SIZE(self);
171
- result = rb_str_buf_new(len);
172
- rb_str_buf_cat2(result, "{");
173
- rb_hash_foreach(self, hash_to_json_i, result);
174
- rb_str_buf_cat2(result, "}");
175
- } else {
176
- GET_STATE(Vstate);
177
- check_max_nesting(state, depth);
178
- if (state->check_circular) {
179
- VALUE self_id = rb_obj_id(self);
180
- if (RTEST(rb_hash_aref(state->seen, self_id))) {
181
- rb_raise(eCircularDatastructure,
182
- "circular data structures not supported!");
183
- }
184
- rb_hash_aset(state->seen, self_id, Qtrue);
185
- result = mHash_json_transfrom(self, Vstate, LONG2FIX(depth));
186
- rb_hash_delete(state->seen, self_id);
187
- } else {
188
- result = mHash_json_transfrom(self, Vstate, LONG2FIX(depth));
189
- }
190
- }
191
- OBJ_INFECT(result, self);
192
- FORCE_UTF8(result);
193
- return result;
194
- }
195
-
196
- inline static VALUE mArray_json_transfrom(VALUE self, VALUE Vstate, VALUE Vdepth) {
197
- long i, len = RARRAY_LEN(self);
198
- VALUE shift, result;
199
- long depth = NIL_P(Vdepth) ? 0 : FIX2LONG(Vdepth);
200
- VALUE delim = rb_str_new2(",");
201
- GET_STATE(Vstate);
202
-
203
- check_max_nesting(state, depth);
204
- if (state->check_circular) {
205
- VALUE self_id = rb_obj_id(self);
206
- rb_hash_aset(state->seen, self_id, Qtrue);
207
- result = rb_str_buf_new(len);
208
- if (RSTRING_LEN(state->array_nl)) rb_str_append(delim, state->array_nl);
209
- shift = rb_str_times(state->indent, LONG2FIX(depth + 1));
210
-
211
- rb_str_buf_cat2(result, "[");
212
- OBJ_INFECT(result, self);
213
- rb_str_buf_append(result, state->array_nl);
214
- for (i = 0; i < len; i++) {
215
- VALUE element = RARRAY_PTR(self)[i];
216
- if (RTEST(rb_hash_aref(state->seen, rb_obj_id(element)))) {
217
- rb_raise(eCircularDatastructure,
218
- "circular data structures not supported!");
219
- }
220
- OBJ_INFECT(result, element);
221
- if (i > 0) rb_str_buf_append(result, delim);
222
- rb_str_buf_append(result, shift);
223
- element = rb_funcall(element, i_to_json, 2, Vstate, LONG2FIX(depth + 1));
224
- Check_Type(element, T_STRING);
225
- rb_str_buf_append(result, element);
226
- }
227
- if (RSTRING_LEN(state->array_nl)) {
228
- rb_str_buf_append(result, state->array_nl);
229
- rb_str_buf_append(result, rb_str_times(state->indent, LONG2FIX(depth)));
230
- }
231
- rb_str_buf_cat2(result, "]");
232
- rb_hash_delete(state->seen, self_id);
233
- } else {
234
- result = rb_str_buf_new(len);
235
- OBJ_INFECT(result, self);
236
- if (RSTRING_LEN(state->array_nl)) rb_str_append(delim, state->array_nl);
237
- shift = rb_str_times(state->indent, LONG2FIX(depth + 1));
238
-
239
- rb_str_buf_cat2(result, "[");
240
- rb_str_buf_append(result, state->array_nl);
241
- for (i = 0; i < len; i++) {
242
- VALUE element = RARRAY_PTR(self)[i];
243
- OBJ_INFECT(result, element);
244
- if (i > 0) rb_str_buf_append(result, delim);
245
- rb_str_buf_append(result, shift);
246
- element = rb_funcall(element, i_to_json, 2, Vstate, LONG2FIX(depth + 1));
247
- Check_Type(element, T_STRING);
248
- rb_str_buf_append(result, element);
249
- }
250
- rb_str_buf_append(result, state->array_nl);
251
- if (RSTRING_LEN(state->array_nl)) {
252
- rb_str_buf_append(result, rb_str_times(state->indent, LONG2FIX(depth)));
253
- }
254
- rb_str_buf_cat2(result, "]");
255
- }
256
- return result;
257
- }
258
-
259
- /*
260
- * call-seq: to_json(state = nil, depth = 0)
261
- *
262
- * Returns a JSON string containing a JSON array, that is unparsed from
263
- * this Array instance.
264
- * _state_ is a JSON::State object, that can also be used to configure the
265
- * produced JSON string output further.
266
- * _depth_ is used to find out nesting depth, to indent accordingly.
267
- */
268
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
269
- VALUE Vstate, Vdepth, result;
270
-
271
- rb_scan_args(argc, argv, "02", &Vstate, &Vdepth);
272
- if (NIL_P(Vstate)) {
273
- long i, len = RARRAY_LEN(self);
274
- result = rb_str_buf_new(2 + 2 * len);
275
- rb_str_buf_cat2(result, "[");
276
- OBJ_INFECT(result, self);
277
- for (i = 0; i < len; i++) {
278
- VALUE element = RARRAY_PTR(self)[i];
279
- OBJ_INFECT(result, element);
280
- if (i > 0) rb_str_buf_cat2(result, ",");
281
- element = rb_funcall(element, i_to_json, 0);
282
- Check_Type(element, T_STRING);
283
- rb_str_buf_append(result, element);
284
- }
285
- rb_str_buf_cat2(result, "]");
286
- } else {
287
- result = mArray_json_transfrom(self, Vstate, Vdepth);
288
- }
289
- OBJ_INFECT(result, self);
290
- FORCE_UTF8(result);
291
- return result;
292
- }
293
-
294
- /*
295
- * call-seq: to_json(*)
296
- *
297
- * Returns a JSON string representation for this Integer number.
298
- */
299
- static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
300
- {
301
- VALUE result = rb_funcall(self, i_to_s, 0);
302
- FORCE_UTF8(result);
303
- return result;
304
- }
305
-
306
- /*
307
- * call-seq: to_json(*)
308
- *
309
- * Returns a JSON string representation for this Float number.
310
- */
311
- static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
312
- {
313
- JSON_Generator_State *state = NULL;
314
- VALUE Vstate, rest, tmp, result;
315
- double value = RFLOAT_VALUE(self);
316
- rb_scan_args(argc, argv, "01*", &Vstate, &rest);
317
- if (!NIL_P(Vstate)) Data_Get_Struct(Vstate, JSON_Generator_State, state);
318
- if (isinf(value)) {
319
- if (state && state->allow_nan) {
320
- result = rb_funcall(self, i_to_s, 0);
321
- } else {
322
- tmp = rb_funcall(self, i_to_s, 0);
323
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
324
- }
325
- } else if (isnan(value)) {
326
- if (state && state->allow_nan) {
327
- result = rb_funcall(self, i_to_s, 0);
328
- } else {
329
- tmp = rb_funcall(self, i_to_s, 0);
330
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
331
- }
332
- } else {
333
- result = rb_funcall(self, i_to_s, 0);
334
- }
335
- FORCE_UTF8(result);
336
- return result;
337
- }
338
-
339
- /*
340
- * call-seq: String.included(modul)
341
- *
342
- * Extends _modul_ with the String::Extend module.
343
- */
344
- static VALUE mString_included_s(VALUE self, VALUE modul) {
345
- VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
346
- FORCE_UTF8(result);
347
- return result;
348
- }
349
-
350
- /*
351
- * call-seq: to_json(*)
352
- *
353
- * This string should be encoded with UTF-8 A call to this method
354
- * returns a JSON string encoded with UTF16 big endian characters as
355
- * \u????.
356
- */
357
- static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
358
- {
359
- VALUE result = rb_str_buf_new(RSTRING_LEN(self));
360
- rb_str_buf_cat2(result, "\"");
361
- #ifdef HAVE_RUBY_ENCODING_H
362
- if (rb_funcall(self, i_encoding, 0) == mEncoding_UTF_8) {
363
- JSON_convert_UTF8_to_JSON(result, self, strictConversion);
364
- } else {
365
- VALUE string = rb_funcall(self, i_encode, 1, mEncoding_UTF_8);
366
- JSON_convert_UTF8_to_JSON(result, string, strictConversion);
367
- }
368
- #else
369
- JSON_convert_UTF8_to_JSON(result, self, strictConversion);
370
- #endif
371
- rb_str_buf_cat2(result, "\"");
372
- FORCE_UTF8(result);
373
- return result;
374
- }
375
-
376
- /*
377
- * call-seq: to_json_raw_object()
378
- *
379
- * This method creates a raw object hash, that can be nested into
380
- * other data structures and will be unparsed as a raw string. This
381
- * method should be used, if you want to convert raw strings to JSON
382
- * instead of UTF-8 strings, e. g. binary data.
383
- */
384
- static VALUE mString_to_json_raw_object(VALUE self) {
385
- VALUE ary;
386
- VALUE result = rb_hash_new();
387
- rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
388
- ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
389
- rb_hash_aset(result, rb_str_new2("raw"), ary);
390
- FORCE_UTF8(result);
391
- return result;
392
- }
393
-
394
- /*
395
- * call-seq: to_json_raw(*args)
396
- *
397
- * This method creates a JSON text from the result of a call to
398
- * to_json_raw_object of this String.
399
- */
400
- static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self) {
401
- VALUE result, obj = mString_to_json_raw_object(self);
402
- Check_Type(obj, T_HASH);
403
- result = mHash_to_json(argc, argv, obj);
404
- FORCE_UTF8(result);
405
- return result;
406
- }
407
-
408
- /*
409
- * call-seq: json_create(o)
410
- *
411
- * Raw Strings are JSON Objects (the raw bytes are stored in an array for the
412
- * key "raw"). The Ruby String can be created by this module method.
413
- */
414
- static VALUE mString_Extend_json_create(VALUE self, VALUE o) {
415
- VALUE ary;
416
- Check_Type(o, T_HASH);
417
- ary = rb_hash_aref(o, rb_str_new2("raw"));
418
- return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
419
- }
420
-
421
- /*
422
- * call-seq: to_json(*)
423
- *
424
- * Returns a JSON string for true: 'true'.
425
- */
426
- static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
427
- {
428
- VALUE result = rb_str_new2("true");
429
- FORCE_UTF8(result);
430
- return result;
431
- }
432
-
433
- /*
434
- * call-seq: to_json(*)
435
- *
436
- * Returns a JSON string for false: 'false'.
437
- */
438
- static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
439
- {
440
- VALUE result = rb_str_new2("false");
441
- FORCE_UTF8(result);
442
- return result;
443
- }
444
-
445
- /*
446
- * call-seq: to_json(*)
447
- *
448
- */
449
- static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
450
- {
451
- VALUE result = rb_str_new2("null");
452
- FORCE_UTF8(result);
453
- return result;
454
- }
455
-
456
- /*
457
- * call-seq: to_json(*)
458
- *
459
- * Converts this object to a string (calling #to_s), converts
460
- * it to a JSON string, and returns the result. This is a fallback, if no
461
- * special method #to_json was defined for some object.
462
- */
463
- static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
464
- {
465
- VALUE result, string = rb_funcall(self, i_to_s, 0);
466
- Check_Type(string, T_STRING);
467
- result = mString_to_json(argc, argv, string);
468
- FORCE_UTF8(result);
469
- return result;
470
- }
471
-
472
- /*
473
- * Document-class: JSON::Ext::Generator::State
474
- *
475
- * This class is used to create State instances, that are use to hold data
476
- * while generating a JSON text from a a Ruby data structure.
477
- */
478
-
479
- static void State_mark(JSON_Generator_State *state)
480
- {
481
- rb_gc_mark_maybe(state->indent);
482
- rb_gc_mark_maybe(state->space);
483
- rb_gc_mark_maybe(state->space_before);
484
- rb_gc_mark_maybe(state->object_nl);
485
- rb_gc_mark_maybe(state->array_nl);
486
- rb_gc_mark_maybe(state->seen);
487
- rb_gc_mark_maybe(state->memo);
488
- rb_gc_mark_maybe(state->depth);
489
- }
490
-
491
- static JSON_Generator_State *State_allocate()
492
- {
493
- JSON_Generator_State *state = ALLOC(JSON_Generator_State);
494
- return state;
495
- }
496
-
497
- static VALUE cState_s_allocate(VALUE klass)
498
- {
499
- JSON_Generator_State *state = State_allocate();
500
- return Data_Wrap_Struct(klass, State_mark, -1, state);
501
- }
502
-
503
- /*
504
- * call-seq: configure(opts)
505
- *
506
- * Configure this State instance with the Hash _opts_, and return
507
- * itself.
508
- */
509
- static VALUE cState_configure(VALUE self, VALUE opts)
510
- {
511
- VALUE tmp;
512
- GET_STATE(self);
513
- tmp = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
514
- if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
515
- if (NIL_P(tmp)) {
516
- rb_raise(rb_eArgError, "opts has to be hash like or convertable into a hash");
517
- }
518
- opts = tmp;
519
- tmp = rb_hash_aref(opts, ID2SYM(i_indent));
520
- if (RTEST(tmp)) {
521
- Check_Type(tmp, T_STRING);
522
- state->indent = tmp;
523
- }
524
- tmp = rb_hash_aref(opts, ID2SYM(i_space));
525
- if (RTEST(tmp)) {
526
- Check_Type(tmp, T_STRING);
527
- state->space = tmp;
528
- }
529
- tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
530
- if (RTEST(tmp)) {
531
- Check_Type(tmp, T_STRING);
532
- state->space_before = tmp;
533
- }
534
- tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
535
- if (RTEST(tmp)) {
536
- Check_Type(tmp, T_STRING);
537
- state->array_nl = tmp;
538
- }
539
- tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
540
- if (RTEST(tmp)) {
541
- Check_Type(tmp, T_STRING);
542
- state->object_nl = tmp;
543
- }
544
- tmp = ID2SYM(i_check_circular);
545
- if (option_given_p(opts, tmp)) {
546
- tmp = rb_hash_aref(opts, ID2SYM(i_check_circular));
547
- state->check_circular = RTEST(tmp);
548
- } else {
549
- state->check_circular = 1;
550
- }
551
- tmp = ID2SYM(i_max_nesting);
552
- state->max_nesting = 19;
553
- if (option_given_p(opts, tmp)) {
554
- VALUE max_nesting = rb_hash_aref(opts, tmp);
555
- if (RTEST(max_nesting)) {
556
- Check_Type(max_nesting, T_FIXNUM);
557
- state->max_nesting = FIX2LONG(max_nesting);
558
- } else {
559
- state->max_nesting = 0;
560
- }
561
- }
562
- tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
563
- state->allow_nan = RTEST(tmp);
564
- return self;
565
- }
566
-
567
- /*
568
- * call-seq: to_h
569
- *
570
- * Returns the configuration instance variables as a hash, that can be
571
- * passed to the configure method.
572
- */
573
- static VALUE cState_to_h(VALUE self)
574
- {
575
- VALUE result = rb_hash_new();
576
- GET_STATE(self);
577
- rb_hash_aset(result, ID2SYM(i_indent), state->indent);
578
- rb_hash_aset(result, ID2SYM(i_space), state->space);
579
- rb_hash_aset(result, ID2SYM(i_space_before), state->space_before);
580
- rb_hash_aset(result, ID2SYM(i_object_nl), state->object_nl);
581
- rb_hash_aset(result, ID2SYM(i_array_nl), state->array_nl);
582
- rb_hash_aset(result, ID2SYM(i_check_circular), state->check_circular ? Qtrue : Qfalse);
583
- rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
584
- rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
585
- return result;
586
- }
587
-
588
- /*
589
- * call-seq: [](name)
590
- *
591
- * Return the value returned by method +name+.
592
- */
593
- static VALUE cState_aref(VALUE self, VALUE name)
594
- {
595
- GET_STATE(self);
596
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
597
- return rb_funcall(self, i_send, 1, name);
598
- } else {
599
- return Qnil;
600
- }
601
- }
602
-
603
- /*
604
- * call-seq: new(opts = {})
605
- *
606
- * Instantiates a new State object, configured by _opts_.
607
- *
608
- * _opts_ can have the following keys:
609
- *
610
- * * *indent*: a string used to indent levels (default: ''),
611
- * * *space*: a string that is put after, a : or , delimiter (default: ''),
612
- * * *space_before*: a string that is put before a : pair delimiter (default: ''),
613
- * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
614
- * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
615
- * * *check_circular*: true if checking for circular data structures
616
- * should be done, false (the default) otherwise.
617
- * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
618
- * generated, otherwise an exception is thrown, if these values are
619
- * encountered. This options defaults to false.
620
- */
621
- static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
622
- {
623
- VALUE opts;
624
- GET_STATE(self);
625
-
626
- rb_scan_args(argc, argv, "01", &opts);
627
- state->indent = rb_str_new2("");
628
- state->space = rb_str_new2("");
629
- state->space_before = rb_str_new2("");
630
- state->array_nl = rb_str_new2("");
631
- state->object_nl = rb_str_new2("");
632
- if (NIL_P(opts)) {
633
- state->check_circular = 1;
634
- state->allow_nan = 0;
635
- state->max_nesting = 19;
636
- } else {
637
- cState_configure(self, opts);
638
- }
639
- state->seen = rb_hash_new();
640
- state->memo = Qnil;
641
- state->depth = INT2FIX(0);
642
- return self;
643
- }
644
-
645
- /*
646
- * call-seq: from_state(opts)
647
- *
648
- * Creates a State object from _opts_, which ought to be Hash to create a
649
- * new State instance configured by _opts_, something else to create an
650
- * unconfigured instance. If _opts_ is a State object, it is just returned.
651
- */
652
- static VALUE cState_from_state_s(VALUE self, VALUE opts)
653
- {
654
- if (rb_obj_is_kind_of(opts, self)) {
655
- return opts;
656
- } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
657
- return rb_funcall(self, i_new, 1, opts);
658
- } else {
659
- return rb_funcall(self, i_new, 0);
660
- }
661
- }
662
-
663
- /*
664
- * call-seq: indent()
665
- *
666
- * This string is used to indent levels in the JSON text.
667
- */
668
- static VALUE cState_indent(VALUE self)
669
- {
670
- GET_STATE(self);
671
- return state->indent;
672
- }
673
-
674
- /*
675
- * call-seq: indent=(indent)
676
- *
677
- * This string is used to indent levels in the JSON text.
678
- */
679
- static VALUE cState_indent_set(VALUE self, VALUE indent)
680
- {
681
- GET_STATE(self);
682
- Check_Type(indent, T_STRING);
683
- return state->indent = indent;
684
- }
685
-
686
- /*
687
- * call-seq: space()
688
- *
689
- * This string is used to insert a space between the tokens in a JSON
690
- * string.
691
- */
692
- static VALUE cState_space(VALUE self)
693
- {
694
- GET_STATE(self);
695
- return state->space;
696
- }
697
-
698
- /*
699
- * call-seq: space=(space)
700
- *
701
- * This string is used to insert a space between the tokens in a JSON
702
- * string.
703
- */
704
- static VALUE cState_space_set(VALUE self, VALUE space)
705
- {
706
- GET_STATE(self);
707
- Check_Type(space, T_STRING);
708
- return state->space = space;
709
- }
710
-
711
- /*
712
- * call-seq: space_before()
713
- *
714
- * This string is used to insert a space before the ':' in JSON objects.
715
- */
716
- static VALUE cState_space_before(VALUE self)
717
- {
718
- GET_STATE(self);
719
- return state->space_before;
720
- }
721
-
722
- /*
723
- * call-seq: space_before=(space_before)
724
- *
725
- * This string is used to insert a space before the ':' in JSON objects.
726
- */
727
- static VALUE cState_space_before_set(VALUE self, VALUE space_before)
728
- {
729
- GET_STATE(self);
730
- Check_Type(space_before, T_STRING);
731
- return state->space_before = space_before;
732
- }
733
-
734
- /*
735
- * call-seq: object_nl()
736
- *
737
- * This string is put at the end of a line that holds a JSON object (or
738
- * Hash).
739
- */
740
- static VALUE cState_object_nl(VALUE self)
741
- {
742
- GET_STATE(self);
743
- return state->object_nl;
744
- }
745
-
746
- /*
747
- * call-seq: object_nl=(object_nl)
748
- *
749
- * This string is put at the end of a line that holds a JSON object (or
750
- * Hash).
751
- */
752
- static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
753
- {
754
- GET_STATE(self);
755
- Check_Type(object_nl, T_STRING);
756
- return state->object_nl = object_nl;
757
- }
758
-
759
- /*
760
- * call-seq: array_nl()
761
- *
762
- * This string is put at the end of a line that holds a JSON array.
763
- */
764
- static VALUE cState_array_nl(VALUE self)
765
- {
766
- GET_STATE(self);
767
- return state->array_nl;
768
- }
769
-
770
- /*
771
- * call-seq: array_nl=(array_nl)
772
- *
773
- * This string is put at the end of a line that holds a JSON array.
774
- */
775
- static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
776
- {
777
- GET_STATE(self);
778
- Check_Type(array_nl, T_STRING);
779
- return state->array_nl = array_nl;
780
- }
781
-
782
- /*
783
- * call-seq: check_circular?
784
- *
785
- * Returns true, if circular data structures should be checked,
786
- * otherwise returns false.
787
- */
788
- static VALUE cState_check_circular_p(VALUE self)
789
- {
790
- GET_STATE(self);
791
- return state->check_circular ? Qtrue : Qfalse;
792
- }
793
-
794
- /*
795
- * call-seq: max_nesting
796
- *
797
- * This integer returns the maximum level of data structure nesting in
798
- * the generated JSON, max_nesting = 0 if no maximum is checked.
799
- */
800
- static VALUE cState_max_nesting(VALUE self)
801
- {
802
- GET_STATE(self);
803
- return LONG2FIX(state->max_nesting);
804
- }
805
-
806
- /*
807
- * call-seq: max_nesting=(depth)
808
- *
809
- * This sets the maximum level of data structure nesting in the generated JSON
810
- * to the integer depth, max_nesting = 0 if no maximum should be checked.
811
- */
812
- static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
813
- {
814
- GET_STATE(self);
815
- Check_Type(depth, T_FIXNUM);
816
- state->max_nesting = FIX2LONG(depth);
817
- return Qnil;
818
- }
819
-
820
- /*
821
- * call-seq: allow_nan?
822
- *
823
- * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
824
- * returns false.
825
- */
826
- static VALUE cState_allow_nan_p(VALUE self)
827
- {
828
- GET_STATE(self);
829
- return state->allow_nan ? Qtrue : Qfalse;
830
- }
831
-
832
- /*
833
- * call-seq: seen?(object)
834
- *
835
- * Returns _true_, if _object_ was already seen during this generating run.
836
- */
837
- static VALUE cState_seen_p(VALUE self, VALUE object)
838
- {
839
- GET_STATE(self);
840
- return rb_hash_aref(state->seen, rb_obj_id(object));
841
- }
842
-
843
- /*
844
- * call-seq: remember(object)
845
- *
846
- * Remember _object_, to find out if it was already encountered (if a cyclic
847
- * data structure is rendered).
848
- */
849
- static VALUE cState_remember(VALUE self, VALUE object)
850
- {
851
- GET_STATE(self);
852
- return rb_hash_aset(state->seen, rb_obj_id(object), Qtrue);
853
- }
854
-
855
- /*
856
- * call-seq: forget(object)
857
- *
858
- * Forget _object_ for this generating run.
859
- */
860
- static VALUE cState_forget(VALUE self, VALUE object)
861
- {
862
- GET_STATE(self);
863
- return rb_hash_delete(state->seen, rb_obj_id(object));
864
- }
865
-
866
- /*
867
- *
868
- */
869
- void Init_generator()
870
- {
871
- rb_require("json/common");
872
- mJSON = rb_define_module("JSON");
873
- mExt = rb_define_module_under(mJSON, "Ext");
874
- mGenerator = rb_define_module_under(mExt, "Generator");
875
- eGeneratorError = rb_path2class("JSON::GeneratorError");
876
- eCircularDatastructure = rb_path2class("JSON::CircularDatastructure");
877
- eNestingError = rb_path2class("JSON::NestingError");
878
- cState = rb_define_class_under(mGenerator, "State", rb_cObject);
879
- rb_define_alloc_func(cState, cState_s_allocate);
880
- rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
881
- rb_define_method(cState, "initialize", cState_initialize, -1);
882
-
883
- rb_define_method(cState, "indent", cState_indent, 0);
884
- rb_define_method(cState, "indent=", cState_indent_set, 1);
885
- rb_define_method(cState, "space", cState_space, 0);
886
- rb_define_method(cState, "space=", cState_space_set, 1);
887
- rb_define_method(cState, "space_before", cState_space_before, 0);
888
- rb_define_method(cState, "space_before=", cState_space_before_set, 1);
889
- rb_define_method(cState, "object_nl", cState_object_nl, 0);
890
- rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
891
- rb_define_method(cState, "array_nl", cState_array_nl, 0);
892
- rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
893
- rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
894
- rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
895
- rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
896
- rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
897
- rb_define_method(cState, "seen?", cState_seen_p, 1);
898
- rb_define_method(cState, "remember", cState_remember, 1);
899
- rb_define_method(cState, "forget", cState_forget, 1);
900
- rb_define_method(cState, "configure", cState_configure, 1);
901
- rb_define_method(cState, "to_h", cState_to_h, 0);
902
- rb_define_method(cState, "[]", cState_aref, 1);
903
-
904
- mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
905
- mObject = rb_define_module_under(mGeneratorMethods, "Object");
906
- rb_define_method(mObject, "to_json", mObject_to_json, -1);
907
- mHash = rb_define_module_under(mGeneratorMethods, "Hash");
908
- rb_define_method(mHash, "to_json", mHash_to_json, -1);
909
- mArray = rb_define_module_under(mGeneratorMethods, "Array");
910
- rb_define_method(mArray, "to_json", mArray_to_json, -1);
911
- mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
912
- rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
913
- mFloat = rb_define_module_under(mGeneratorMethods, "Float");
914
- rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
915
- mString = rb_define_module_under(mGeneratorMethods, "String");
916
- rb_define_singleton_method(mString, "included", mString_included_s, 1);
917
- rb_define_method(mString, "to_json", mString_to_json, -1);
918
- rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
919
- rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
920
- mString_Extend = rb_define_module_under(mString, "Extend");
921
- rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
922
- mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
923
- rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
924
- mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
925
- rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
926
- mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
927
- rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
928
-
929
- i_to_s = rb_intern("to_s");
930
- i_to_json = rb_intern("to_json");
931
- i_new = rb_intern("new");
932
- i_indent = rb_intern("indent");
933
- i_space = rb_intern("space");
934
- i_space_before = rb_intern("space_before");
935
- i_object_nl = rb_intern("object_nl");
936
- i_array_nl = rb_intern("array_nl");
937
- i_check_circular = rb_intern("check_circular");
938
- i_max_nesting = rb_intern("max_nesting");
939
- i_allow_nan = rb_intern("allow_nan");
940
- i_pack = rb_intern("pack");
941
- i_unpack = rb_intern("unpack");
942
- i_create_id = rb_intern("create_id");
943
- i_extend = rb_intern("extend");
944
- i_key_p = rb_intern("key?");
945
- i_aref = rb_intern("[]");
946
- i_send = rb_intern("__send__");
947
- i_respond_to_p = rb_intern("respond_to?");
948
- #ifdef HAVE_RUBY_ENCODING_H
949
- mEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
950
- i_encoding = rb_intern("encoding");
951
- i_encode = rb_intern("encode");
952
- #endif
953
- }