3
#ifdef HAVE_RUBY_ENCODING_H
4
static VALUE CEncoding_UTF_8;
5
static ID i_encoding, i_encode;
8
static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
9
mHash, mArray, mInteger, mFloat, mString, mString_Extend,
10
mTrueClass, mFalseClass, mNilClass, eGeneratorError,
11
eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE;
13
static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
14
i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
15
i_pack, i_unpack, i_create_id, i_extend, i_key_p, i_aref, i_send,
16
i_respond_to_p, i_match;
19
* Copyright 2001-2004 Unicode, Inc.
23
* This source code is provided as is by Unicode, Inc. No claims are
24
* made as to fitness for any particular purpose. No warranties of any
25
* kind are expressed or implied. The recipient agrees to determine
26
* applicability of information provided. If this file has been
27
* purchased on magnetic or optical media from Unicode, Inc., the
28
* sole remedy for any claim will be exchange of defective media
29
* within 90 days of receipt.
31
* Limitations on Rights to Redistribute This Code
33
* Unicode, Inc. hereby grants the right to freely use the information
34
* supplied in this file in the creation of products supporting the
35
* Unicode Standard, and to make copies of this file in any form
36
* for internal or external distribution as long as this notice
41
* Index into the table below with the first byte of a UTF-8 sequence to
42
* get the number of trailing bytes that are supposed to follow it.
43
* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
44
* left as-is for anyone who may want to do such conversion, which was
45
* allowed in earlier algorithms.
47
static const char trailingBytesForUTF8[256] = {
48
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
49
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
50
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
51
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
55
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
59
* Magic values subtracted from a buffer value during UTF8 conversion.
60
* This table contains as many values as there might be trailing bytes
61
* in a UTF-8 sequence.
63
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
64
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
67
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
68
* This must be called with the length pre-determined by the first byte.
69
* If not calling this from ConvertUTF8to*, then the length can be set by:
70
* length = trailingBytesForUTF8[*source]+1;
71
* and the sequence is illegal right away if there aren't that many bytes
73
* If presented with a length > 4, this returns 0. The Unicode
74
* definition of UTF-8 goes up to 4-byte sequences.
76
static unsigned char isLegalUTF8(const UTF8 *source, int length)
79
const UTF8 *srcptr = source+length;
82
/* Everything else falls through when "1"... */
83
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
84
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
85
case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
88
/* no fall-through in this inner switch */
89
case 0xE0: if (a < 0xA0) return 0; break;
90
case 0xED: if (a > 0x9F) return 0; break;
91
case 0xF0: if (a < 0x90) return 0; break;
92
case 0xF4: if (a > 0x8F) return 0; break;
93
default: if (a < 0x80) return 0;
96
case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
98
if (*source > 0xF4) return 0;
102
/* Escapes the UTF16 character and stores the result in the buffer buf. */
103
static void unicode_escape(char *buf, UTF16 character)
105
const char *digits = "0123456789abcdef";
107
buf[2] = digits[character >> 12];
108
buf[3] = digits[(character >> 8) & 0xf];
109
buf[4] = digits[(character >> 4) & 0xf];
110
buf[5] = digits[character & 0xf];
113
/* Escapes the UTF16 character and stores the result in the buffer buf, then
114
* the buffer buf іs appended to the FBuffer buffer. */
115
static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
118
unicode_escape(buf, character);
119
fbuffer_append(buffer, buf, 6);
122
/* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
123
* and control characters are JSON escaped. */
124
static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
126
const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
127
const UTF8 *sourceEnd = source + RSTRING_LEN(string);
128
char buf[6] = { '\\', 'u' };
130
while (source < sourceEnd) {
132
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
133
if (source + extraBytesToRead >= sourceEnd) {
134
rb_raise(rb_path2class("JSON::GeneratorError"),
135
"partial character in source, but hit end");
137
if (!isLegalUTF8(source, extraBytesToRead+1)) {
138
rb_raise(rb_path2class("JSON::GeneratorError"),
139
"source sequence is illegal/malformed utf-8");
142
* The cases all fall through. See "Note A" below.
144
switch (extraBytesToRead) {
145
case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
146
case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
147
case 3: ch += *source++; ch <<= 6;
148
case 2: ch += *source++; ch <<= 6;
149
case 1: ch += *source++; ch <<= 6;
150
case 0: ch += *source++;
152
ch -= offsetsFromUTF8[extraBytesToRead];
154
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
155
/* UTF-16 surrogate values are illegal in UTF-32 */
156
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
157
#if UNI_STRICT_CONVERSION
158
source -= (extraBytesToRead+1); /* return to the illegal value itself */
159
rb_raise(rb_path2class("JSON::GeneratorError"),
160
"source sequence is illegal/malformed utf-8");
162
unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
166
if (ch >= 0x20 && ch <= 0x7f) {
169
fbuffer_append(buffer, "\\\\", 2);
172
fbuffer_append(buffer, "\\\"", 2);
175
fbuffer_append_char(buffer, (char)ch);
181
fbuffer_append(buffer, "\\n", 2);
184
fbuffer_append(buffer, "\\r", 2);
187
fbuffer_append(buffer, "\\t", 2);
190
fbuffer_append(buffer, "\\f", 2);
193
fbuffer_append(buffer, "\\b", 2);
196
unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
201
} else if (ch > UNI_MAX_UTF16) {
202
#if UNI_STRICT_CONVERSION
203
source -= (extraBytesToRead+1); /* return to the start */
204
rb_raise(rb_path2class("JSON::GeneratorError"),
205
"source sequence is illegal/malformed utf8");
207
unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
210
/* target is a character in range 0xFFFF - 0x10FFFF. */
212
unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
213
unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
218
/* Converts string to a JSON string in FBuffer buffer, where only the
219
* characters required by the JSON standard are JSON escaped. The remaining
220
* characters (should be UTF8) are just passed through and appended to the
222
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
224
const char *ptr = RSTRING_PTR(string), *p;
225
int len = RSTRING_LEN(string), start = 0, end = 0;
226
const char *escape = NULL;
229
char buf[6] = { '\\', 'u' };
231
for (start = 0, end = 0; end < len;) {
233
c = (unsigned char) *p;
257
unicode_escape(buf, (UTF16) *p);
278
fbuffer_append(buffer, ptr + start, end - start);
279
fbuffer_append(buffer, escape, escape_len);
283
fbuffer_append(buffer, ptr + start, end - start);
286
static char *fstrndup(const char *ptr, int len) {
288
if (len <= 0) return NULL;
289
result = ALLOC_N(char, len);
290
memccpy(result, ptr, 0, len);
294
/* fbuffer implementation */
296
static FBuffer *fbuffer_alloc()
298
FBuffer *fb = ALLOC(FBuffer);
299
memset((void *) fb, 0, sizeof(FBuffer));
300
fb->initial_length = FBUFFER_INITIAL_LENGTH;
304
static FBuffer *fbuffer_alloc_with_length(unsigned int initial_length)
307
assert(initial_length > 0);
309
memset((void *) fb, 0, sizeof(FBuffer));
310
fb->initial_length = initial_length;
314
static void fbuffer_free(FBuffer *fb)
316
if (fb->ptr) ruby_xfree(fb->ptr);
320
static void fbuffer_free_only_buffer(FBuffer *fb)
325
static void fbuffer_clear(FBuffer *fb)
330
static void fbuffer_inc_capa(FBuffer *fb, unsigned int requested)
332
unsigned int required;
335
fb->ptr = ALLOC_N(char, fb->initial_length);
336
fb->capa = fb->initial_length;
339
for (required = fb->capa; requested > required - fb->len; required <<= 1);
341
if (required > fb->capa) {
342
REALLOC_N(fb->ptr, char, required);
347
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned int len)
350
fbuffer_inc_capa(fb, len);
351
MEMCPY(fb->ptr + fb->len, newstr, char, len);
356
static void fbuffer_append_char(FBuffer *fb, char newchr)
358
fbuffer_inc_capa(fb, 1);
359
*(fb->ptr + fb->len) = newchr;
363
static void freverse(char *start, char *end)
367
while (end > start) {
368
c = *end, *end-- = *start, *start++ = c;
372
static int fltoa(long number, char *buf)
374
static char digits[] = "0123456789";
378
if (sign < 0) number = -number;
379
do *tmp++ = digits[number % 10]; while (number /= 10);
380
if (sign < 0) *tmp++ = '-';
381
freverse(buf, tmp - 1);
385
static void fbuffer_append_long(FBuffer *fb, long number)
388
int len = fltoa(number, buf);
389
fbuffer_append(fb, buf, len);
392
static FBuffer *fbuffer_dup(FBuffer *fb)
398
result = fbuffer_alloc_with_length(len);
399
fbuffer_append(result, FBUFFER_PAIR(fb));
401
result = fbuffer_alloc();
407
* Document-module: JSON::Ext::Generator
409
* This is the JSON generator implemented as a C extension. It can be
410
* configured to be used by setting
412
* JSON.generator = JSON::Ext::Generator
414
* with the method generator= in JSON.
419
* call-seq: to_json(state = nil, depth = 0)
421
* Returns a JSON string containing a JSON object, that is generated from
422
* this Hash instance.
423
* _state_ is a JSON::State object, that can also be used to configure the
424
* produced JSON string output further.
425
* _depth_ is used to find out nesting depth, to indent accordingly.
427
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
431
rb_scan_args(argc, argv, "02", &state, &depth);
432
state = cState_from_state_s(cState, state);
433
return cState_partial_generate(state, self, depth);
437
* call-seq: to_json(state = nil, depth = 0)
439
* Returns a JSON string containing a JSON array, that is generated from
440
* this Array instance.
441
* _state_ is a JSON::State object, that can also be used to configure the
442
* produced JSON string output further.
443
* _depth_ is used to find out nesting depth, to indent accordingly.
445
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
447
rb_scan_args(argc, argv, "02", &state, &depth);
448
state = cState_from_state_s(cState, state);
449
return cState_partial_generate(state, self, depth);
453
* call-seq: to_json(*)
455
* Returns a JSON string representation for this Integer number.
457
static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
460
rb_scan_args(argc, argv, "02", &state, &depth);
461
state = cState_from_state_s(cState, state);
462
return cState_partial_generate(state, self, depth);
466
* call-seq: to_json(*)
468
* Returns a JSON string representation for this Float number.
470
static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
473
rb_scan_args(argc, argv, "02", &state, &depth);
474
state = cState_from_state_s(cState, state);
475
return cState_partial_generate(state, self, depth);
479
* call-seq: String.included(modul)
481
* Extends _modul_ with the String::Extend module.
483
static VALUE mString_included_s(VALUE self, VALUE modul) {
484
VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
489
* call-seq: to_json(*)
491
* This string should be encoded with UTF-8 A call to this method
492
* returns a JSON string encoded with UTF16 big endian characters as
495
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
498
rb_scan_args(argc, argv, "02", &state, &depth);
499
state = cState_from_state_s(cState, state);
500
return cState_partial_generate(state, self, depth);
504
* call-seq: to_json_raw_object()
506
* This method creates a raw object hash, that can be nested into
507
* other data structures and will be generated as a raw string. This
508
* method should be used, if you want to convert raw strings to JSON
509
* instead of UTF-8 strings, e. g. binary data.
511
static VALUE mString_to_json_raw_object(VALUE self)
514
VALUE result = rb_hash_new();
515
rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
516
ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
517
rb_hash_aset(result, rb_str_new2("raw"), ary);
522
* call-seq: to_json_raw(*args)
524
* This method creates a JSON text from the result of a call to
525
* to_json_raw_object of this String.
527
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
529
VALUE obj = mString_to_json_raw_object(self);
530
Check_Type(obj, T_HASH);
531
return mHash_to_json(argc, argv, obj);
535
* call-seq: json_create(o)
537
* Raw Strings are JSON Objects (the raw bytes are stored in an array for the
538
* key "raw"). The Ruby String can be created by this module method.
540
static VALUE mString_Extend_json_create(VALUE self, VALUE o)
543
Check_Type(o, T_HASH);
544
ary = rb_hash_aref(o, rb_str_new2("raw"));
545
return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
549
* call-seq: to_json(*)
551
* Returns a JSON string for true: 'true'.
553
static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
556
rb_scan_args(argc, argv, "02", &state, &depth);
557
state = cState_from_state_s(cState, state);
558
return cState_partial_generate(state, self, depth);
562
* call-seq: to_json(*)
564
* Returns a JSON string for false: 'false'.
566
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
569
rb_scan_args(argc, argv, "02", &state, &depth);
570
state = cState_from_state_s(cState, state);
571
return cState_partial_generate(state, self, depth);
575
* call-seq: to_json(*)
578
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
581
rb_scan_args(argc, argv, "02", &state, &depth);
582
state = cState_from_state_s(cState, state);
583
return cState_partial_generate(state, self, depth);
587
* call-seq: to_json(*)
589
* Converts this object to a string (calling #to_s), converts
590
* it to a JSON string, and returns the result. This is a fallback, if no
591
* special method #to_json was defined for some object.
593
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
596
VALUE string = rb_funcall(self, i_to_s, 0);
597
rb_scan_args(argc, argv, "02", &state, &depth);
598
Check_Type(string, T_STRING);
599
state = cState_from_state_s(cState, state);
600
return cState_partial_generate(state, string, depth);
603
static void State_free(JSON_Generator_State *state)
605
if (state->indent) ruby_xfree(state->indent);
606
if (state->space) ruby_xfree(state->space);
607
if (state->space_before) ruby_xfree(state->space_before);
608
if (state->object_nl) ruby_xfree(state->object_nl);
609
if (state->array_nl) ruby_xfree(state->array_nl);
610
if (state->array_delim) fbuffer_free(state->array_delim);
611
if (state->object_delim) fbuffer_free(state->object_delim);
612
if (state->object_delim2) fbuffer_free(state->object_delim2);
616
static JSON_Generator_State *State_allocate()
618
JSON_Generator_State *state = ALLOC(JSON_Generator_State);
622
static VALUE cState_s_allocate(VALUE klass)
624
JSON_Generator_State *state = State_allocate();
625
return Data_Wrap_Struct(klass, NULL, State_free, state);
629
* call-seq: configure(opts)
631
* Configure this State instance with the Hash _opts_, and return
634
static VALUE cState_configure(VALUE self, VALUE opts)
638
tmp = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
639
if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
641
rb_raise(rb_eArgError, "opts has to be hash like or convertable into a hash");
644
tmp = rb_hash_aref(opts, ID2SYM(i_indent));
647
Check_Type(tmp, T_STRING);
648
len = RSTRING_LEN(tmp);
649
state->indent = fstrndup(RSTRING_PTR(tmp), len);
650
state->indent_len = len;
652
tmp = rb_hash_aref(opts, ID2SYM(i_space));
655
Check_Type(tmp, T_STRING);
656
len = RSTRING_LEN(tmp);
657
state->space = fstrndup(RSTRING_PTR(tmp), len);
658
state->space_len = len;
660
tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
663
Check_Type(tmp, T_STRING);
664
len = RSTRING_LEN(tmp);
665
state->space_before = fstrndup(RSTRING_PTR(tmp), len);
666
state->space_before_len = len;
668
tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
671
Check_Type(tmp, T_STRING);
672
len = RSTRING_LEN(tmp);
673
state->array_nl = fstrndup(RSTRING_PTR(tmp), len);
674
state->array_nl_len = len;
676
tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
679
Check_Type(tmp, T_STRING);
680
len = RSTRING_LEN(tmp);
681
state->object_nl = fstrndup(RSTRING_PTR(tmp), len);
682
state->object_nl_len = len;
684
tmp = ID2SYM(i_max_nesting);
685
state->max_nesting = 19;
686
if (option_given_p(opts, tmp)) {
687
VALUE max_nesting = rb_hash_aref(opts, tmp);
688
if (RTEST(max_nesting)) {
689
Check_Type(max_nesting, T_FIXNUM);
690
state->max_nesting = FIX2LONG(max_nesting);
692
state->max_nesting = 0;
695
tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
696
state->allow_nan = RTEST(tmp);
697
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
698
state->ascii_only = RTEST(tmp);
705
* Returns the configuration instance variables as a hash, that can be
706
* passed to the configure method.
708
static VALUE cState_to_h(VALUE self)
710
VALUE result = rb_hash_new();
712
rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
713
rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
714
rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
715
rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
716
rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
717
rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
718
rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
719
rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
726
* Return the value returned by method +name+.
728
static VALUE cState_aref(VALUE self, VALUE name)
731
if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
732
return rb_funcall(self, i_send, 1, name);
738
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj, long depth)
744
char *object_nl = state->object_nl;
745
long object_nl_len = state->object_nl_len;
746
char *indent = state->indent;
747
long indent_len = state->indent_len;
748
long max_nesting = state->max_nesting;
749
char *delim = FBUFFER_PTR(state->object_delim);
750
long delim_len = FBUFFER_LEN(state->object_delim);
751
char *delim2 = FBUFFER_PTR(state->object_delim2);
752
long delim2_len = FBUFFER_LEN(state->object_delim2);
754
VALUE key, key_to_s, keys;
756
if (max_nesting != 0 && depth > max_nesting) {
757
fbuffer_free(buffer);
758
rb_raise(eNestingError, "nesting of %ld is too deep", depth);
760
fbuffer_append_char(buffer, '{');
761
keys = rb_funcall(obj, rb_intern("keys"), 0);
762
for(i = 0; i < RARRAY_LEN(keys); i++) {
763
if (i > 0) fbuffer_append(buffer, delim, delim_len);
765
fbuffer_append(buffer, object_nl, object_nl_len);
768
for (j = 0; j < depth; j++) {
769
fbuffer_append(buffer, indent, indent_len);
772
key = rb_ary_entry(keys, i);
773
key_to_s = rb_funcall(key, i_to_s, 0);
774
Check_Type(key_to_s, T_STRING);
775
generate_json(buffer, Vstate, state, key_to_s, depth);
776
fbuffer_append(buffer, delim2, delim2_len);
777
generate_json(buffer, Vstate, state, rb_hash_aref(obj, key), depth);
781
fbuffer_append(buffer, object_nl, object_nl_len);
783
for (j = 0; j < depth; j++) {
784
fbuffer_append(buffer, indent, indent_len);
788
fbuffer_append_char(buffer, '}');
793
char *array_nl = state->array_nl;
794
long array_nl_len = state->array_nl_len;
795
char *indent = state->indent;
796
long indent_len = state->indent_len;
797
long max_nesting = state->max_nesting;
798
char *delim = FBUFFER_PTR(state->array_delim);
799
long delim_len = FBUFFER_LEN(state->array_delim);
802
if (max_nesting != 0 && depth > max_nesting) {
803
fbuffer_free(buffer);
804
rb_raise(eNestingError, "nesting of %ld is too deep", depth);
806
fbuffer_append_char(buffer, '[');
807
if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
808
for(i = 0; i < RARRAY_LEN(obj); i++) {
809
if (i > 0) fbuffer_append(buffer, delim, delim_len);
811
for (j = 0; j < depth; j++) {
812
fbuffer_append(buffer, indent, indent_len);
815
generate_json(buffer, Vstate, state, rb_ary_entry(obj, i), depth);
819
fbuffer_append(buffer, array_nl, array_nl_len);
821
for (j = 0; j < depth; j++) {
822
fbuffer_append(buffer, indent, indent_len);
826
fbuffer_append_char(buffer, ']');
830
fbuffer_append_char(buffer, '"');
831
#ifdef HAVE_RUBY_ENCODING_H
832
obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
834
if (state->ascii_only) {
835
convert_UTF8_to_JSON_ASCII(buffer, obj);
837
convert_UTF8_to_JSON(buffer, obj);
839
fbuffer_append_char(buffer, '"');
842
fbuffer_append(buffer, "null", 4);
845
fbuffer_append(buffer, "false", 5);
848
fbuffer_append(buffer, "true", 4);
851
fbuffer_append_long(buffer, FIX2LONG(obj));
854
tmp = rb_funcall(obj, i_to_s, 0);
855
fbuffer_append(buffer, RSTRING_PAIR(tmp));
859
double value = RFLOAT_VALUE(obj);
860
char allow_nan = state->allow_nan;
861
tmp = rb_funcall(obj, i_to_s, 0);
864
fbuffer_free(buffer);
865
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
866
} else if (isnan(value)) {
867
fbuffer_free(buffer);
868
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
871
fbuffer_append(buffer, RSTRING_PAIR(tmp));
875
if (rb_respond_to(obj, i_to_json)) {
876
tmp = rb_funcall(obj, i_to_json, 2, Vstate, INT2FIX(depth + 1));
877
Check_Type(tmp, T_STRING);
878
fbuffer_append(buffer, RSTRING_PAIR(tmp));
880
tmp = rb_funcall(obj, i_to_s, 0);
881
Check_Type(tmp, T_STRING);
882
generate_json(buffer, Vstate, state, tmp, depth + 1);
889
* call-seq: partial_generate(obj)
891
* Generates a part of a JSON document from object +obj+ and returns the
894
static VALUE cState_partial_generate(VALUE self, VALUE obj, VALUE depth)
897
FBuffer *buffer = fbuffer_alloc();
900
if (state->object_delim) {
901
fbuffer_clear(state->object_delim);
903
state->object_delim = fbuffer_alloc_with_length(16);
905
fbuffer_append_char(state->object_delim, ',');
906
if (state->object_delim2) {
907
fbuffer_clear(state->object_delim2);
909
state->object_delim2 = fbuffer_alloc_with_length(16);
911
fbuffer_append_char(state->object_delim2, ':');
912
if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
914
if (state->array_delim) {
915
fbuffer_clear(state->array_delim);
917
state->array_delim = fbuffer_alloc_with_length(16);
919
fbuffer_append_char(state->array_delim, ',');
920
if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
922
generate_json(buffer, self, state, obj, NIL_P(depth) ? 0 : FIX2INT(depth));
923
result = rb_str_new(FBUFFER_PAIR(buffer));
924
fbuffer_free(buffer);
930
* call-seq: generate(obj)
932
* Generates a valid JSON document from object +obj+ and returns the
933
* result. If no valid JSON document can be created this method raises a
934
* GeneratorError exception.
936
static VALUE cState_generate(VALUE self, VALUE obj)
938
VALUE result = cState_partial_generate(self, obj, Qnil);
940
args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
941
args[1] = CRegexp_MULTILINE;
942
re = rb_class_new_instance(2, args, rb_cRegexp);
943
if (NIL_P(rb_funcall(re, i_match, 1, result))) {
944
rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
950
* call-seq: new(opts = {})
952
* Instantiates a new State object, configured by _opts_.
954
* _opts_ can have the following keys:
956
* * *indent*: a string used to indent levels (default: ''),
957
* * *space*: a string that is put after, a : or , delimiter (default: ''),
958
* * *space_before*: a string that is put before a : pair delimiter (default: ''),
959
* * *object_nl*: a string that is put at the end of a JSON object (default: ''),
960
* * *array_nl*: a string that is put at the end of a JSON array (default: ''),
961
* * *allow_nan*: true if NaN, Infinity, and -Infinity should be
962
* generated, otherwise an exception is thrown, if these values are
963
* encountered. This options defaults to false.
965
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
969
MEMZERO(state, JSON_Generator_State, 1);
970
state->max_nesting = 19;
971
rb_scan_args(argc, argv, "01", &opts);
972
if (!NIL_P(opts)) cState_configure(self, opts);
977
* call-seq: initialize_copy(orig)
979
* Initializes this object from orig if it to be duplicated/cloned and returns
982
static VALUE cState_init_copy(VALUE obj, VALUE orig)
984
JSON_Generator_State *objState, *origState;
986
Data_Get_Struct(obj, JSON_Generator_State, objState);
987
Data_Get_Struct(orig, JSON_Generator_State, origState);
988
if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
990
MEMCPY(objState, origState, JSON_Generator_State, 1);
991
objState->indent = fstrndup(origState->indent, origState->indent_len);
992
objState->space = fstrndup(origState->space, origState->space_len);
993
objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
994
objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
995
objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
996
if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
997
if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
998
if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
1003
* call-seq: from_state(opts)
1005
* Creates a State object from _opts_, which ought to be Hash to create a
1006
* new State instance configured by _opts_, something else to create an
1007
* unconfigured instance. If _opts_ is a State object, it is just returned.
1009
static VALUE cState_from_state_s(VALUE self, VALUE opts)
1011
if (rb_obj_is_kind_of(opts, self)) {
1013
} else if (rb_obj_is_kind_of(opts, rb_cHash)) {
1014
return rb_funcall(self, i_new, 1, opts);
1016
if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
1017
CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, rb_intern("SAFE_STATE_PROTOTYPE"));
1019
return CJSON_SAFE_STATE_PROTOTYPE;
1024
* call-seq: indent()
1026
* This string is used to indent levels in the JSON text.
1028
static VALUE cState_indent(VALUE self)
1031
return state->indent ? rb_str_new2(state->indent) : rb_str_new2("");
1035
* call-seq: indent=(indent)
1037
* This string is used to indent levels in the JSON text.
1039
static VALUE cState_indent_set(VALUE self, VALUE indent)
1042
Check_Type(indent, T_STRING);
1043
if (RSTRING_LEN(indent) == 0) {
1044
if (state->indent) {
1045
ruby_xfree(state->indent);
1046
state->indent = NULL;
1049
if (state->indent) ruby_xfree(state->indent);
1050
state->indent = strdup(RSTRING_PTR(indent));
1058
* This string is used to insert a space between the tokens in a JSON
1061
static VALUE cState_space(VALUE self)
1064
return state->space ? rb_str_new2(state->space) : rb_str_new2("");
1068
* call-seq: space=(space)
1070
* This string is used to insert a space between the tokens in a JSON
1073
static VALUE cState_space_set(VALUE self, VALUE space)
1076
Check_Type(space, T_STRING);
1077
if (RSTRING_LEN(space) == 0) {
1079
ruby_xfree(state->space);
1080
state->space = NULL;
1083
if (state->space) ruby_xfree(state->space);
1084
state->space = strdup(RSTRING_PTR(space));
1090
* call-seq: space_before()
1092
* This string is used to insert a space before the ':' in JSON objects.
1094
static VALUE cState_space_before(VALUE self)
1097
return state->space_before ? rb_str_new2(state->space_before) : rb_str_new2("");
1101
* call-seq: space_before=(space_before)
1103
* This string is used to insert a space before the ':' in JSON objects.
1105
static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1108
Check_Type(space_before, T_STRING);
1109
if (RSTRING_LEN(space_before) == 0) {
1110
if (state->space_before) {
1111
ruby_xfree(state->space_before);
1112
state->space_before = NULL;
1115
if (state->space_before) ruby_xfree(state->space_before);
1116
state->space_before = strdup(RSTRING_PTR(space_before));
1122
* call-seq: object_nl()
1124
* This string is put at the end of a line that holds a JSON object (or
1127
static VALUE cState_object_nl(VALUE self)
1130
return state->object_nl ? rb_str_new2(state->object_nl) : rb_str_new2("");
1134
* call-seq: object_nl=(object_nl)
1136
* This string is put at the end of a line that holds a JSON object (or
1139
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1142
Check_Type(object_nl, T_STRING);
1143
if (RSTRING_LEN(object_nl) == 0) {
1144
if (state->object_nl) {
1145
ruby_xfree(state->object_nl);
1146
state->object_nl = NULL;
1149
if (state->object_nl) ruby_xfree(state->object_nl);
1150
state->object_nl = strdup(RSTRING_PTR(object_nl));
1156
* call-seq: array_nl()
1158
* This string is put at the end of a line that holds a JSON array.
1160
static VALUE cState_array_nl(VALUE self)
1163
return state->array_nl ? rb_str_new2(state->array_nl) : rb_str_new2("");
1167
* call-seq: array_nl=(array_nl)
1169
* This string is put at the end of a line that holds a JSON array.
1171
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1174
Check_Type(array_nl, T_STRING);
1175
if (RSTRING_LEN(array_nl) == 0) {
1176
if (state->array_nl) {
1177
ruby_xfree(state->array_nl);
1178
state->array_nl = NULL;
1181
if (state->array_nl) ruby_xfree(state->array_nl);
1182
state->array_nl = strdup(RSTRING_PTR(array_nl));
1189
* call-seq: check_circular?
1191
* Returns true, if circular data structures should be checked,
1192
* otherwise returns false.
1194
static VALUE cState_check_circular_p(VALUE self)
1197
return state->max_nesting ? Qtrue : Qfalse;
1201
* call-seq: max_nesting
1203
* This integer returns the maximum level of data structure nesting in
1204
* the generated JSON, max_nesting = 0 if no maximum is checked.
1206
static VALUE cState_max_nesting(VALUE self)
1209
return LONG2FIX(state->max_nesting);
1213
* call-seq: max_nesting=(depth)
1215
* This sets the maximum level of data structure nesting in the generated JSON
1216
* to the integer depth, max_nesting = 0 if no maximum should be checked.
1218
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1221
Check_Type(depth, T_FIXNUM);
1222
return state->max_nesting = FIX2LONG(depth);
1226
* call-seq: allow_nan?
1228
* Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1231
static VALUE cState_allow_nan_p(VALUE self)
1234
return state->allow_nan ? Qtrue : Qfalse;
1238
* call-seq: ascii_only?
1240
* Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1243
static VALUE cState_ascii_only_p(VALUE self)
1246
return state->ascii_only ? Qtrue : Qfalse;
1252
void Init_generator()
1254
rb_require("json/common");
1256
mJSON = rb_define_module("JSON");
1257
mExt = rb_define_module_under(mJSON, "Ext");
1258
mGenerator = rb_define_module_under(mExt, "Generator");
1260
eGeneratorError = rb_path2class("JSON::GeneratorError");
1261
eNestingError = rb_path2class("JSON::NestingError");
1263
cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1264
rb_define_alloc_func(cState, cState_s_allocate);
1265
rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1266
rb_define_method(cState, "initialize", cState_initialize, -1);
1267
rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
1268
rb_define_method(cState, "indent", cState_indent, 0);
1269
rb_define_method(cState, "indent=", cState_indent_set, 1);
1270
rb_define_method(cState, "space", cState_space, 0);
1271
rb_define_method(cState, "space=", cState_space_set, 1);
1272
rb_define_method(cState, "space_before", cState_space_before, 0);
1273
rb_define_method(cState, "space_before=", cState_space_before_set, 1);
1274
rb_define_method(cState, "object_nl", cState_object_nl, 0);
1275
rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
1276
rb_define_method(cState, "array_nl", cState_array_nl, 0);
1277
rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1278
rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1279
rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1280
rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1281
rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1282
rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
1283
rb_define_method(cState, "configure", cState_configure, 1);
1284
rb_define_method(cState, "to_h", cState_to_h, 0);
1285
rb_define_method(cState, "[]", cState_aref, 1);
1286
rb_define_method(cState, "generate", cState_generate, 1);
1287
rb_define_method(cState, "partial_generate", cState_partial_generate, 1);
1289
mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1290
mObject = rb_define_module_under(mGeneratorMethods, "Object");
1291
rb_define_method(mObject, "to_json", mObject_to_json, -1);
1292
mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1293
rb_define_method(mHash, "to_json", mHash_to_json, -1);
1294
mArray = rb_define_module_under(mGeneratorMethods, "Array");
1295
rb_define_method(mArray, "to_json", mArray_to_json, -1);
1296
mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1297
rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1298
mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1299
rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1300
mString = rb_define_module_under(mGeneratorMethods, "String");
1301
rb_define_singleton_method(mString, "included", mString_included_s, 1);
1302
rb_define_method(mString, "to_json", mString_to_json, -1);
1303
rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
1304
rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
1305
mString_Extend = rb_define_module_under(mString, "Extend");
1306
rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
1307
mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1308
rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
1309
mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1310
rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
1311
mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1312
rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1314
CRegexp_MULTILINE = rb_const_get(rb_cRegexp, rb_intern("MULTILINE"));
1315
i_to_s = rb_intern("to_s");
1316
i_to_json = rb_intern("to_json");
1317
i_new = rb_intern("new");
1318
i_indent = rb_intern("indent");
1319
i_space = rb_intern("space");
1320
i_space_before = rb_intern("space_before");
1321
i_object_nl = rb_intern("object_nl");
1322
i_array_nl = rb_intern("array_nl");
1323
i_max_nesting = rb_intern("max_nesting");
1324
i_allow_nan = rb_intern("allow_nan");
1325
i_ascii_only = rb_intern("ascii_only");
1326
i_pack = rb_intern("pack");
1327
i_unpack = rb_intern("unpack");
1328
i_create_id = rb_intern("create_id");
1329
i_extend = rb_intern("extend");
1330
i_key_p = rb_intern("key?");
1331
i_aref = rb_intern("[]");
1332
i_send = rb_intern("__send__");
1333
i_respond_to_p = rb_intern("respond_to?");
1334
i_match = rb_intern("match");
1335
#ifdef HAVE_RUBY_ENCODING_H
1336
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1337
i_encoding = rb_intern("encoding");
1338
i_encode = rb_intern("encode");
1340
CJSON_SAFE_STATE_PROTOTYPE = Qnil;