Ruby  2.5.0dev(2017-10-22revision60238)
generator.c
Go to the documentation of this file.
1 #include "../fbuffer/fbuffer.h"
2 #include "generator.h"
3 
4 #ifdef HAVE_RUBY_ENCODING_H
5 static VALUE CEncoding_UTF_8;
6 static ID i_encoding, i_encode;
7 #endif
8 
9 static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
10  mHash, mArray,
11 #ifdef RUBY_INTEGER_UNIFICATION
12  mInteger,
13 #else
14  mFixnum, mBignum,
15 #endif
16  mFloat, mString, mString_Extend,
17  mTrueClass, mFalseClass, mNilClass, eGeneratorError,
18  eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE,
19  i_SAFE_STATE_PROTOTYPE;
20 
21 static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
22  i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
23  i_pack, i_unpack, i_create_id, i_extend, i_key_p,
24  i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
25  i_buffer_initial_length, i_dup;
26 
27 /*
28  * Copyright 2001-2004 Unicode, Inc.
29  *
30  * Disclaimer
31  *
32  * This source code is provided as is by Unicode, Inc. No claims are
33  * made as to fitness for any particular purpose. No warranties of any
34  * kind are expressed or implied. The recipient agrees to determine
35  * applicability of information provided. If this file has been
36  * purchased on magnetic or optical media from Unicode, Inc., the
37  * sole remedy for any claim will be exchange of defective media
38  * within 90 days of receipt.
39  *
40  * Limitations on Rights to Redistribute This Code
41  *
42  * Unicode, Inc. hereby grants the right to freely use the information
43  * supplied in this file in the creation of products supporting the
44  * Unicode Standard, and to make copies of this file in any form
45  * for internal or external distribution as long as this notice
46  * remains attached.
47  */
48 
49 /*
50  * Index into the table below with the first byte of a UTF-8 sequence to
51  * get the number of trailing bytes that are supposed to follow it.
52  * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
53  * left as-is for anyone who may want to do such conversion, which was
54  * allowed in earlier algorithms.
55  */
56 static const char trailingBytesForUTF8[256] = {
57  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,
58  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,
59  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,
60  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,
61  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,
62  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,
63  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,
64  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
65 };
66 
67 /*
68  * Magic values subtracted from a buffer value during UTF8 conversion.
69  * This table contains as many values as there might be trailing bytes
70  * in a UTF-8 sequence.
71  */
72 static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
73  0x03C82080UL, 0xFA082080UL, 0x82082080UL };
74 
75 /*
76  * Utility routine to tell whether a sequence of bytes is legal UTF-8.
77  * This must be called with the length pre-determined by the first byte.
78  * If not calling this from ConvertUTF8to*, then the length can be set by:
79  * length = trailingBytesForUTF8[*source]+1;
80  * and the sequence is illegal right away if there aren't that many bytes
81  * available.
82  * If presented with a length > 4, this returns 0. The Unicode
83  * definition of UTF-8 goes up to 4-byte sequences.
84  */
85 static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length)
86 {
87  UTF8 a;
88  const UTF8 *srcptr = source+length;
89  switch (length) {
90  default: return 0;
91  /* Everything else falls through when "1"... */
92  case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
93  case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
94  case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
95 
96  switch (*source) {
97  /* no fall-through in this inner switch */
98  case 0xE0: if (a < 0xA0) return 0; break;
99  case 0xED: if (a > 0x9F) return 0; break;
100  case 0xF0: if (a < 0x90) return 0; break;
101  case 0xF4: if (a > 0x8F) return 0; break;
102  default: if (a < 0x80) return 0;
103  }
104 
105  case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
106  }
107  if (*source > 0xF4) return 0;
108  return 1;
109 }
110 
111 /* Escapes the UTF16 character and stores the result in the buffer buf. */
112 static void unicode_escape(char *buf, UTF16 character)
113 {
114  const char *digits = "0123456789abcdef";
115 
116  buf[2] = digits[character >> 12];
117  buf[3] = digits[(character >> 8) & 0xf];
118  buf[4] = digits[(character >> 4) & 0xf];
119  buf[5] = digits[character & 0xf];
120 }
121 
122 /* Escapes the UTF16 character and stores the result in the buffer buf, then
123  * the buffer buf is appended to the FBuffer buffer. */
124 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
125  character)
126 {
127  unicode_escape(buf, character);
128  fbuffer_append(buffer, buf, 6);
129 }
130 
131 /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
132  * and control characters are JSON escaped. */
133 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
134 {
135  const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
136  const UTF8 *sourceEnd = source + RSTRING_LEN(string);
137  char buf[6] = { '\\', 'u' };
138 
139  while (source < sourceEnd) {
140  UTF32 ch = 0;
141  unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
142  if (source + extraBytesToRead >= sourceEnd) {
143  rb_raise(rb_path2class("JSON::GeneratorError"),
144  "partial character in source, but hit end");
145  }
146  if (!isLegalUTF8(source, extraBytesToRead+1)) {
147  rb_raise(rb_path2class("JSON::GeneratorError"),
148  "source sequence is illegal/malformed utf-8");
149  }
150  /*
151  * The cases all fall through. See "Note A" below.
152  */
153  switch (extraBytesToRead) {
154  case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
155  case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
156  case 3: ch += *source++; ch <<= 6;
157  case 2: ch += *source++; ch <<= 6;
158  case 1: ch += *source++; ch <<= 6;
159  case 0: ch += *source++;
160  }
161  ch -= offsetsFromUTF8[extraBytesToRead];
162 
163  if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
164  /* UTF-16 surrogate values are illegal in UTF-32 */
165  if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
166 #if UNI_STRICT_CONVERSION
167  source -= (extraBytesToRead+1); /* return to the illegal value itself */
168  rb_raise(rb_path2class("JSON::GeneratorError"),
169  "source sequence is illegal/malformed utf-8");
170 #else
171  unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
172 #endif
173  } else {
174  /* normal case */
175  if (ch >= 0x20 && ch <= 0x7f) {
176  switch (ch) {
177  case '\\':
178  fbuffer_append(buffer, "\\\\", 2);
179  break;
180  case '"':
181  fbuffer_append(buffer, "\\\"", 2);
182  break;
183  default:
184  fbuffer_append_char(buffer, (char)ch);
185  break;
186  }
187  } else {
188  switch (ch) {
189  case '\n':
190  fbuffer_append(buffer, "\\n", 2);
191  break;
192  case '\r':
193  fbuffer_append(buffer, "\\r", 2);
194  break;
195  case '\t':
196  fbuffer_append(buffer, "\\t", 2);
197  break;
198  case '\f':
199  fbuffer_append(buffer, "\\f", 2);
200  break;
201  case '\b':
202  fbuffer_append(buffer, "\\b", 2);
203  break;
204  default:
205  unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
206  break;
207  }
208  }
209  }
210  } else if (ch > UNI_MAX_UTF16) {
211 #if UNI_STRICT_CONVERSION
212  source -= (extraBytesToRead+1); /* return to the start */
213  rb_raise(rb_path2class("JSON::GeneratorError"),
214  "source sequence is illegal/malformed utf8");
215 #else
216  unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
217 #endif
218  } else {
219  /* target is a character in range 0xFFFF - 0x10FFFF. */
220  ch -= halfBase;
221  unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
222  unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
223  }
224  }
225  RB_GC_GUARD(string);
226 }
227 
228 /* Converts string to a JSON string in FBuffer buffer, where only the
229  * characters required by the JSON standard are JSON escaped. The remaining
230  * characters (should be UTF8) are just passed through and appended to the
231  * result. */
232 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
233 {
234  const char *ptr = RSTRING_PTR(string), *p;
235  unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
236  const char *escape = NULL;
237  int escape_len;
238  unsigned char c;
239  char buf[6] = { '\\', 'u' };
240 
241  for (start = 0, end = 0; end < len;) {
242  p = ptr + end;
243  c = (unsigned char) *p;
244  if (c < 0x20) {
245  switch (c) {
246  case '\n':
247  escape = "\\n";
248  escape_len = 2;
249  break;
250  case '\r':
251  escape = "\\r";
252  escape_len = 2;
253  break;
254  case '\t':
255  escape = "\\t";
256  escape_len = 2;
257  break;
258  case '\f':
259  escape = "\\f";
260  escape_len = 2;
261  break;
262  case '\b':
263  escape = "\\b";
264  escape_len = 2;
265  break;
266  default:
267  unicode_escape(buf, (UTF16) *p);
268  escape = buf;
269  escape_len = 6;
270  break;
271  }
272  } else {
273  switch (c) {
274  case '\\':
275  escape = "\\\\";
276  escape_len = 2;
277  break;
278  case '"':
279  escape = "\\\"";
280  escape_len = 2;
281  break;
282  default:
283  {
284  unsigned short clen = trailingBytesForUTF8[c] + 1;
285  if (end + clen > len) {
286  rb_raise(rb_path2class("JSON::GeneratorError"),
287  "partial character in source, but hit end");
288  }
289  if (!isLegalUTF8((UTF8 *) p, clen)) {
290  rb_raise(rb_path2class("JSON::GeneratorError"),
291  "source sequence is illegal/malformed utf-8");
292  }
293  end += clen;
294  }
295  continue;
296  break;
297  }
298  }
299  fbuffer_append(buffer, ptr + start, end - start);
300  fbuffer_append(buffer, escape, escape_len);
301  start = ++end;
302  escape = NULL;
303  }
304  fbuffer_append(buffer, ptr + start, end - start);
305 }
306 
307 static char *fstrndup(const char *ptr, unsigned long len) {
308  char *result;
309  if (len <= 0) return NULL;
310  result = ALLOC_N(char, len);
311  memcpy(result, ptr, len);
312  return result;
313 }
314 
315 /*
316  * Document-module: JSON::Ext::Generator
317  *
318  * This is the JSON generator implemented as a C extension. It can be
319  * configured to be used by setting
320  *
321  * JSON.generator = JSON::Ext::Generator
322  *
323  * with the method generator= in JSON.
324  *
325  */
326 
327 /*
328  * call-seq: to_json(state = nil)
329  *
330  * Returns a JSON string containing a JSON object, that is generated from
331  * this Hash instance.
332  * _state_ is a JSON::State object, that can also be used to configure the
333  * produced JSON string output further.
334  */
335 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
336 {
337  GENERATE_JSON(object);
338 }
339 
340 /*
341  * call-seq: to_json(state = nil)
342  *
343  * Returns a JSON string containing a JSON array, that is generated from
344  * this Array instance.
345  * _state_ is a JSON::State object, that can also be used to configure the
346  * produced JSON string output further.
347  */
348 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
349  GENERATE_JSON(array);
350 }
351 
352 #ifdef RUBY_INTEGER_UNIFICATION
353 /*
354  * call-seq: to_json(*)
355  *
356  * Returns a JSON string representation for this Integer number.
357  */
358 static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
359 {
360  GENERATE_JSON(integer);
361 }
362 
363 #else
364 /*
365  * call-seq: to_json(*)
366  *
367  * Returns a JSON string representation for this Integer number.
368  */
369 static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
370 {
371  GENERATE_JSON(fixnum);
372 }
373 
374 /*
375  * call-seq: to_json(*)
376  *
377  * Returns a JSON string representation for this Integer number.
378  */
379 static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
380 {
381  GENERATE_JSON(bignum);
382 }
383 #endif
384 
385 /*
386  * call-seq: to_json(*)
387  *
388  * Returns a JSON string representation for this Float number.
389  */
390 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
391 {
392  GENERATE_JSON(float);
393 }
394 
395 /*
396  * call-seq: String.included(modul)
397  *
398  * Extends _modul_ with the String::Extend module.
399  */
400 static VALUE mString_included_s(VALUE self, VALUE modul) {
401  VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
402  return result;
403 }
404 
405 /*
406  * call-seq: to_json(*)
407  *
408  * This string should be encoded with UTF-8 A call to this method
409  * returns a JSON string encoded with UTF16 big endian characters as
410  * \u????.
411  */
412 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
413 {
414  GENERATE_JSON(string);
415 }
416 
417 /*
418  * call-seq: to_json_raw_object()
419  *
420  * This method creates a raw object hash, that can be nested into
421  * other data structures and will be generated as a raw string. This
422  * method should be used, if you want to convert raw strings to JSON
423  * instead of UTF-8 strings, e. g. binary data.
424  */
425 static VALUE mString_to_json_raw_object(VALUE self)
426 {
427  VALUE ary;
428  VALUE result = rb_hash_new();
429  rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
430  ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
431  rb_hash_aset(result, rb_str_new2("raw"), ary);
432  return result;
433 }
434 
435 /*
436  * call-seq: to_json_raw(*args)
437  *
438  * This method creates a JSON text from the result of a call to
439  * to_json_raw_object of this String.
440  */
441 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
442 {
443  VALUE obj = mString_to_json_raw_object(self);
444  Check_Type(obj, T_HASH);
445  return mHash_to_json(argc, argv, obj);
446 }
447 
448 /*
449  * call-seq: json_create(o)
450  *
451  * Raw Strings are JSON Objects (the raw bytes are stored in an array for the
452  * key "raw"). The Ruby String can be created by this module method.
453  */
454 static VALUE mString_Extend_json_create(VALUE self, VALUE o)
455 {
456  VALUE ary;
457  Check_Type(o, T_HASH);
458  ary = rb_hash_aref(o, rb_str_new2("raw"));
459  return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
460 }
461 
462 /*
463  * call-seq: to_json(*)
464  *
465  * Returns a JSON string for true: 'true'.
466  */
467 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
468 {
469  GENERATE_JSON(true);
470 }
471 
472 /*
473  * call-seq: to_json(*)
474  *
475  * Returns a JSON string for false: 'false'.
476  */
477 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
478 {
479  GENERATE_JSON(false);
480 }
481 
482 /*
483  * call-seq: to_json(*)
484  *
485  * Returns a JSON string for nil: 'null'.
486  */
487 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
488 {
489  GENERATE_JSON(null);
490 }
491 
492 /*
493  * call-seq: to_json(*)
494  *
495  * Converts this object to a string (calling #to_s), converts
496  * it to a JSON string, and returns the result. This is a fallback, if no
497  * special method #to_json was defined for some object.
498  */
499 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
500 {
501  VALUE state;
502  VALUE string = rb_funcall(self, i_to_s, 0);
503  rb_scan_args(argc, argv, "01", &state);
504  Check_Type(string, T_STRING);
505  state = cState_from_state_s(cState, state);
506  return cState_partial_generate(state, string);
507 }
508 
509 static void State_free(void *ptr)
510 {
511  JSON_Generator_State *state = ptr;
512  if (state->indent) ruby_xfree(state->indent);
513  if (state->space) ruby_xfree(state->space);
514  if (state->space_before) ruby_xfree(state->space_before);
515  if (state->object_nl) ruby_xfree(state->object_nl);
516  if (state->array_nl) ruby_xfree(state->array_nl);
517  if (state->array_delim) fbuffer_free(state->array_delim);
518  if (state->object_delim) fbuffer_free(state->object_delim);
519  if (state->object_delim2) fbuffer_free(state->object_delim2);
520  ruby_xfree(state);
521 }
522 
523 static size_t State_memsize(const void *ptr)
524 {
525  const JSON_Generator_State *state = ptr;
526  size_t size = sizeof(*state);
527  if (state->indent) size += state->indent_len + 1;
528  if (state->space) size += state->space_len + 1;
529  if (state->space_before) size += state->space_before_len + 1;
530  if (state->object_nl) size += state->object_nl_len + 1;
531  if (state->array_nl) size += state->array_nl_len + 1;
532  if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
533  if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
534  if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
535  return size;
536 }
537 
538 #ifdef NEW_TYPEDDATA_WRAPPER
539 static const rb_data_type_t JSON_Generator_State_type = {
540  "JSON/Generator/State",
541  {NULL, State_free, State_memsize,},
542 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
543  0, 0,
545 #endif
546 };
547 #endif
548 
549 static VALUE cState_s_allocate(VALUE klass)
550 {
551  JSON_Generator_State *state;
553  &JSON_Generator_State_type, state);
554 }
555 
556 /*
557  * call-seq: configure(opts)
558  *
559  * Configure this State instance with the Hash _opts_, and return
560  * itself.
561  */
562 static VALUE cState_configure(VALUE self, VALUE opts)
563 {
564  VALUE tmp;
565  GET_STATE(self);
566  tmp = rb_check_convert_type(opts, T_HASH, "Hash", "to_hash");
567  if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
568  opts = tmp;
569  tmp = rb_hash_aref(opts, ID2SYM(i_indent));
570  if (RTEST(tmp)) {
571  unsigned long len;
572  Check_Type(tmp, T_STRING);
573  len = RSTRING_LEN(tmp);
574  state->indent = fstrndup(RSTRING_PTR(tmp), len + 1);
575  state->indent_len = len;
576  }
577  tmp = rb_hash_aref(opts, ID2SYM(i_space));
578  if (RTEST(tmp)) {
579  unsigned long len;
580  Check_Type(tmp, T_STRING);
581  len = RSTRING_LEN(tmp);
582  state->space = fstrndup(RSTRING_PTR(tmp), len + 1);
583  state->space_len = len;
584  }
585  tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
586  if (RTEST(tmp)) {
587  unsigned long len;
588  Check_Type(tmp, T_STRING);
589  len = RSTRING_LEN(tmp);
590  state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1);
591  state->space_before_len = len;
592  }
593  tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
594  if (RTEST(tmp)) {
595  unsigned long len;
596  Check_Type(tmp, T_STRING);
597  len = RSTRING_LEN(tmp);
598  state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
599  state->array_nl_len = len;
600  }
601  tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
602  if (RTEST(tmp)) {
603  unsigned long len;
604  Check_Type(tmp, T_STRING);
605  len = RSTRING_LEN(tmp);
606  state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
607  state->object_nl_len = len;
608  }
609  tmp = ID2SYM(i_max_nesting);
610  state->max_nesting = 100;
611  if (option_given_p(opts, tmp)) {
612  VALUE max_nesting = rb_hash_aref(opts, tmp);
613  if (RTEST(max_nesting)) {
614  Check_Type(max_nesting, T_FIXNUM);
615  state->max_nesting = FIX2LONG(max_nesting);
616  } else {
617  state->max_nesting = 0;
618  }
619  }
620  tmp = ID2SYM(i_depth);
621  state->depth = 0;
622  if (option_given_p(opts, tmp)) {
623  VALUE depth = rb_hash_aref(opts, tmp);
624  if (RTEST(depth)) {
625  Check_Type(depth, T_FIXNUM);
626  state->depth = FIX2LONG(depth);
627  } else {
628  state->depth = 0;
629  }
630  }
631  tmp = ID2SYM(i_buffer_initial_length);
632  if (option_given_p(opts, tmp)) {
633  VALUE buffer_initial_length = rb_hash_aref(opts, tmp);
634  if (RTEST(buffer_initial_length)) {
635  long initial_length;
636  Check_Type(buffer_initial_length, T_FIXNUM);
637  initial_length = FIX2LONG(buffer_initial_length);
638  if (initial_length > 0) state->buffer_initial_length = initial_length;
639  }
640  }
641  tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
642  state->allow_nan = RTEST(tmp);
643  tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
644  state->ascii_only = RTEST(tmp);
645  return self;
646 }
647 
648 static void set_state_ivars(VALUE hash, VALUE state)
649 {
650  VALUE ivars = rb_obj_instance_variables(state);
651  int i = 0;
652  for (i = 0; i < RARRAY_LEN(ivars); i++) {
653  VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0);
654  long key_len = RSTRING_LEN(key);
655  VALUE value = rb_iv_get(state, StringValueCStr(key));
656  rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value);
657  }
658 }
659 
660 /*
661  * call-seq: to_h
662  *
663  * Returns the configuration instance variables as a hash, that can be
664  * passed to the configure method.
665  */
666 static VALUE cState_to_h(VALUE self)
667 {
668  VALUE result = rb_hash_new();
669  GET_STATE(self);
670  set_state_ivars(result, self);
671  rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
672  rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
673  rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
674  rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
675  rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
676  rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
677  rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
678  rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
679  rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
680  rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
681  return result;
682 }
683 
684 /*
685 * call-seq: [](name)
686 *
687 * Returns the value returned by method +name+.
688 */
689 static VALUE cState_aref(VALUE self, VALUE name)
690 {
691  name = rb_funcall(name, i_to_s, 0);
692  if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
693  return rb_funcall(self, i_send, 1, name);
694  } else {
695  return rb_ivar_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
696  }
697 }
698 
699 /*
700 * call-seq: []=(name, value)
701 *
702 * Sets the attribute name to value.
703 */
704 static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
705 {
706  VALUE name_writer;
707 
708  name = rb_funcall(name, i_to_s, 0);
709  name_writer = rb_str_cat2(rb_str_dup(name), "=");
710  if (RTEST(rb_funcall(self, i_respond_to_p, 1, name_writer))) {
711  return rb_funcall(self, i_send, 2, name_writer, value);
712  } else {
713  rb_ivar_set(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)), value);
714  }
715  return Qnil;
716 }
717 
718 static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
719 {
720  char *object_nl = state->object_nl;
721  long object_nl_len = state->object_nl_len;
722  char *indent = state->indent;
723  long indent_len = state->indent_len;
724  long max_nesting = state->max_nesting;
725  char *delim = FBUFFER_PTR(state->object_delim);
726  long delim_len = FBUFFER_LEN(state->object_delim);
727  char *delim2 = FBUFFER_PTR(state->object_delim2);
728  long delim2_len = FBUFFER_LEN(state->object_delim2);
729  long depth = ++state->depth;
730  int i, j;
731  VALUE key, key_to_s, keys;
732  if (max_nesting != 0 && depth > max_nesting) {
733  fbuffer_free(buffer);
734  rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
735  }
736  fbuffer_append_char(buffer, '{');
737  keys = rb_funcall(obj, i_keys, 0);
738  for(i = 0; i < RARRAY_LEN(keys); i++) {
739  if (i > 0) fbuffer_append(buffer, delim, delim_len);
740  if (object_nl) {
741  fbuffer_append(buffer, object_nl, object_nl_len);
742  }
743  if (indent) {
744  for (j = 0; j < depth; j++) {
745  fbuffer_append(buffer, indent, indent_len);
746  }
747  }
748  key = rb_ary_entry(keys, i);
749  key_to_s = rb_funcall(key, i_to_s, 0);
750  Check_Type(key_to_s, T_STRING);
751  generate_json(buffer, Vstate, state, key_to_s);
752  fbuffer_append(buffer, delim2, delim2_len);
753  generate_json(buffer, Vstate, state, rb_hash_aref(obj, key));
754  }
755  depth = --state->depth;
756  if (object_nl) {
757  fbuffer_append(buffer, object_nl, object_nl_len);
758  if (indent) {
759  for (j = 0; j < depth; j++) {
760  fbuffer_append(buffer, indent, indent_len);
761  }
762  }
763  }
764  fbuffer_append_char(buffer, '}');
765 }
766 
767 static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
768 {
769  char *array_nl = state->array_nl;
770  long array_nl_len = state->array_nl_len;
771  char *indent = state->indent;
772  long indent_len = state->indent_len;
773  long max_nesting = state->max_nesting;
774  char *delim = FBUFFER_PTR(state->array_delim);
775  long delim_len = FBUFFER_LEN(state->array_delim);
776  long depth = ++state->depth;
777  int i, j;
778  if (max_nesting != 0 && depth > max_nesting) {
779  fbuffer_free(buffer);
780  rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
781  }
782  fbuffer_append_char(buffer, '[');
783  if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
784  for(i = 0; i < RARRAY_LEN(obj); i++) {
785  if (i > 0) fbuffer_append(buffer, delim, delim_len);
786  if (indent) {
787  for (j = 0; j < depth; j++) {
788  fbuffer_append(buffer, indent, indent_len);
789  }
790  }
791  generate_json(buffer, Vstate, state, rb_ary_entry(obj, i));
792  }
793  state->depth = --depth;
794  if (array_nl) {
795  fbuffer_append(buffer, array_nl, array_nl_len);
796  if (indent) {
797  for (j = 0; j < depth; j++) {
798  fbuffer_append(buffer, indent, indent_len);
799  }
800  }
801  }
802  fbuffer_append_char(buffer, ']');
803 }
804 
805 static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
806 {
807  fbuffer_append_char(buffer, '"');
808 #ifdef HAVE_RUBY_ENCODING_H
809  obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
810 #endif
811  if (state->ascii_only) {
812  convert_UTF8_to_JSON_ASCII(buffer, obj);
813  } else {
814  convert_UTF8_to_JSON(buffer, obj);
815  }
816  fbuffer_append_char(buffer, '"');
817 }
818 
819 static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
820 {
821  fbuffer_append(buffer, "null", 4);
822 }
823 
824 static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
825 {
826  fbuffer_append(buffer, "false", 5);
827 }
828 
829 static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
830 {
831  fbuffer_append(buffer, "true", 4);
832 }
833 
834 static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
835 {
836  fbuffer_append_long(buffer, FIX2LONG(obj));
837 }
838 
839 static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
840 {
841  VALUE tmp = rb_funcall(obj, i_to_s, 0);
842  fbuffer_append_str(buffer, tmp);
843 }
844 
845 #ifdef RUBY_INTEGER_UNIFICATION
846 static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
847 {
848  if (FIXNUM_P(obj))
849  generate_json_fixnum(buffer, Vstate, state, obj);
850  else
851  generate_json_bignum(buffer, Vstate, state, obj);
852 }
853 #endif
854 static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
855 {
856  double value = RFLOAT_VALUE(obj);
857  char allow_nan = state->allow_nan;
858  VALUE tmp = rb_funcall(obj, i_to_s, 0);
859  if (!allow_nan) {
860  if (isinf(value)) {
861  fbuffer_free(buffer);
862  rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
863  } else if (isnan(value)) {
864  fbuffer_free(buffer);
865  rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
866  }
867  }
868  fbuffer_append_str(buffer, tmp);
869 }
870 
871 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
872 {
873  VALUE tmp;
874  VALUE klass = CLASS_OF(obj);
875  if (klass == rb_cHash) {
876  generate_json_object(buffer, Vstate, state, obj);
877  } else if (klass == rb_cArray) {
878  generate_json_array(buffer, Vstate, state, obj);
879  } else if (klass == rb_cString) {
880  generate_json_string(buffer, Vstate, state, obj);
881  } else if (obj == Qnil) {
882  generate_json_null(buffer, Vstate, state, obj);
883  } else if (obj == Qfalse) {
884  generate_json_false(buffer, Vstate, state, obj);
885  } else if (obj == Qtrue) {
886  generate_json_true(buffer, Vstate, state, obj);
887  } else if (FIXNUM_P(obj)) {
888  generate_json_fixnum(buffer, Vstate, state, obj);
889  } else if (RB_TYPE_P(obj, T_BIGNUM)) {
890  generate_json_bignum(buffer, Vstate, state, obj);
891  } else if (klass == rb_cFloat) {
892  generate_json_float(buffer, Vstate, state, obj);
893  } else if (rb_respond_to(obj, i_to_json)) {
894  tmp = rb_funcall(obj, i_to_json, 1, Vstate);
895  Check_Type(tmp, T_STRING);
896  fbuffer_append_str(buffer, tmp);
897  } else {
898  tmp = rb_funcall(obj, i_to_s, 0);
899  Check_Type(tmp, T_STRING);
900  generate_json_string(buffer, Vstate, state, tmp);
901  }
902 }
903 
904 static FBuffer *cState_prepare_buffer(VALUE self)
905 {
906  FBuffer *buffer;
907  GET_STATE(self);
908  buffer = fbuffer_alloc(state->buffer_initial_length);
909 
910  if (state->object_delim) {
911  fbuffer_clear(state->object_delim);
912  } else {
913  state->object_delim = fbuffer_alloc(16);
914  }
915  fbuffer_append_char(state->object_delim, ',');
916  if (state->object_delim2) {
917  fbuffer_clear(state->object_delim2);
918  } else {
919  state->object_delim2 = fbuffer_alloc(16);
920  }
921  if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
922  fbuffer_append_char(state->object_delim2, ':');
923  if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
924 
925  if (state->array_delim) {
926  fbuffer_clear(state->array_delim);
927  } else {
928  state->array_delim = fbuffer_alloc(16);
929  }
930  fbuffer_append_char(state->array_delim, ',');
931  if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
932  return buffer;
933 }
934 
935 static VALUE cState_partial_generate(VALUE self, VALUE obj)
936 {
937  FBuffer *buffer = cState_prepare_buffer(self);
938  GET_STATE(self);
939  generate_json(buffer, self, state, obj);
940  return fbuffer_to_s(buffer);
941 }
942 
943 /*
944  * call-seq: generate(obj)
945  *
946  * Generates a valid JSON document from object +obj+ and returns the
947  * result. If no valid JSON document can be created this method raises a
948  * GeneratorError exception.
949  */
950 static VALUE cState_generate(VALUE self, VALUE obj)
951 {
952  VALUE result = cState_partial_generate(self, obj);
953  GET_STATE(self);
954  (void)state;
955  return result;
956 }
957 
958 /*
959  * call-seq: new(opts = {})
960  *
961  * Instantiates a new State object, configured by _opts_.
962  *
963  * _opts_ can have the following keys:
964  *
965  * * *indent*: a string used to indent levels (default: ''),
966  * * *space*: a string that is put after, a : or , delimiter (default: ''),
967  * * *space_before*: a string that is put before a : pair delimiter (default: ''),
968  * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
969  * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
970  * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
971  * generated, otherwise an exception is thrown, if these values are
972  * encountered. This options defaults to false.
973  * * *buffer_initial_length*: sets the initial length of the generator's
974  * internal buffer.
975  */
976 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
977 {
978  VALUE opts;
979  GET_STATE(self);
980  state->max_nesting = 100;
982  rb_scan_args(argc, argv, "01", &opts);
983  if (!NIL_P(opts)) cState_configure(self, opts);
984  return self;
985 }
986 
987 /*
988  * call-seq: initialize_copy(orig)
989  *
990  * Initializes this object from orig if it can be duplicated/cloned and returns
991  * it.
992 */
993 static VALUE cState_init_copy(VALUE obj, VALUE orig)
994 {
995  JSON_Generator_State *objState, *origState;
996 
997  if (obj == orig) return obj;
998  GET_STATE_TO(obj, objState);
999  GET_STATE_TO(orig, origState);
1000  if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
1001 
1002  MEMCPY(objState, origState, JSON_Generator_State, 1);
1003  objState->indent = fstrndup(origState->indent, origState->indent_len);
1004  objState->space = fstrndup(origState->space, origState->space_len);
1005  objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
1006  objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
1007  objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
1008  if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
1009  if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
1010  if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
1011  return obj;
1012 }
1013 
1014 /*
1015  * call-seq: from_state(opts)
1016  *
1017  * Creates a State object from _opts_, which ought to be Hash to create a
1018  * new State instance configured by _opts_, something else to create an
1019  * unconfigured instance. If _opts_ is a State object, it is just returned.
1020  */
1021 static VALUE cState_from_state_s(VALUE self, VALUE opts)
1022 {
1023  if (rb_obj_is_kind_of(opts, self)) {
1024  return opts;
1025  } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
1026  return rb_funcall(self, i_new, 1, opts);
1027  } else {
1028  if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
1029  CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
1030  }
1031  return rb_funcall(CJSON_SAFE_STATE_PROTOTYPE, i_dup, 0);
1032  }
1033 }
1034 
1035 /*
1036  * call-seq: indent()
1037  *
1038  * Returns the string that is used to indent levels in the JSON text.
1039  */
1040 static VALUE cState_indent(VALUE self)
1041 {
1042  GET_STATE(self);
1043  return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
1044 }
1045 
1046 /*
1047  * call-seq: indent=(indent)
1048  *
1049  * Sets the string that is used to indent levels in the JSON text.
1050  */
1051 static VALUE cState_indent_set(VALUE self, VALUE indent)
1052 {
1053  unsigned long len;
1054  GET_STATE(self);
1055  Check_Type(indent, T_STRING);
1056  len = RSTRING_LEN(indent);
1057  if (len == 0) {
1058  if (state->indent) {
1059  ruby_xfree(state->indent);
1060  state->indent = NULL;
1061  state->indent_len = 0;
1062  }
1063  } else {
1064  if (state->indent) ruby_xfree(state->indent);
1065  state->indent = fstrndup(RSTRING_PTR(indent), len);
1066  state->indent_len = len;
1067  }
1068  return Qnil;
1069 }
1070 
1071 /*
1072  * call-seq: space()
1073  *
1074  * Returns the string that is used to insert a space between the tokens in a JSON
1075  * string.
1076  */
1077 static VALUE cState_space(VALUE self)
1078 {
1079  GET_STATE(self);
1080  return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
1081 }
1082 
1083 /*
1084  * call-seq: space=(space)
1085  *
1086  * Sets _space_ to the string that is used to insert a space between the tokens in a JSON
1087  * string.
1088  */
1089 static VALUE cState_space_set(VALUE self, VALUE space)
1090 {
1091  unsigned long len;
1092  GET_STATE(self);
1093  Check_Type(space, T_STRING);
1094  len = RSTRING_LEN(space);
1095  if (len == 0) {
1096  if (state->space) {
1097  ruby_xfree(state->space);
1098  state->space = NULL;
1099  state->space_len = 0;
1100  }
1101  } else {
1102  if (state->space) ruby_xfree(state->space);
1103  state->space = fstrndup(RSTRING_PTR(space), len);
1104  state->space_len = len;
1105  }
1106  return Qnil;
1107 }
1108 
1109 /*
1110  * call-seq: space_before()
1111  *
1112  * Returns the string that is used to insert a space before the ':' in JSON objects.
1113  */
1114 static VALUE cState_space_before(VALUE self)
1115 {
1116  GET_STATE(self);
1117  return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
1118 }
1119 
1120 /*
1121  * call-seq: space_before=(space_before)
1122  *
1123  * Sets the string that is used to insert a space before the ':' in JSON objects.
1124  */
1125 static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1126 {
1127  unsigned long len;
1128  GET_STATE(self);
1129  Check_Type(space_before, T_STRING);
1130  len = RSTRING_LEN(space_before);
1131  if (len == 0) {
1132  if (state->space_before) {
1133  ruby_xfree(state->space_before);
1134  state->space_before = NULL;
1135  state->space_before_len = 0;
1136  }
1137  } else {
1138  if (state->space_before) ruby_xfree(state->space_before);
1139  state->space_before = fstrndup(RSTRING_PTR(space_before), len);
1140  state->space_before_len = len;
1141  }
1142  return Qnil;
1143 }
1144 
1145 /*
1146  * call-seq: object_nl()
1147  *
1148  * This string is put at the end of a line that holds a JSON object (or
1149  * Hash).
1150  */
1151 static VALUE cState_object_nl(VALUE self)
1152 {
1153  GET_STATE(self);
1154  return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
1155 }
1156 
1157 /*
1158  * call-seq: object_nl=(object_nl)
1159  *
1160  * This string is put at the end of a line that holds a JSON object (or
1161  * Hash).
1162  */
1163 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1164 {
1165  unsigned long len;
1166  GET_STATE(self);
1167  Check_Type(object_nl, T_STRING);
1168  len = RSTRING_LEN(object_nl);
1169  if (len == 0) {
1170  if (state->object_nl) {
1171  ruby_xfree(state->object_nl);
1172  state->object_nl = NULL;
1173  }
1174  } else {
1175  if (state->object_nl) ruby_xfree(state->object_nl);
1176  state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
1177  state->object_nl_len = len;
1178  }
1179  return Qnil;
1180 }
1181 
1182 /*
1183  * call-seq: array_nl()
1184  *
1185  * This string is put at the end of a line that holds a JSON array.
1186  */
1187 static VALUE cState_array_nl(VALUE self)
1188 {
1189  GET_STATE(self);
1190  return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
1191 }
1192 
1193 /*
1194  * call-seq: array_nl=(array_nl)
1195  *
1196  * This string is put at the end of a line that holds a JSON array.
1197  */
1198 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1199 {
1200  unsigned long len;
1201  GET_STATE(self);
1202  Check_Type(array_nl, T_STRING);
1203  len = RSTRING_LEN(array_nl);
1204  if (len == 0) {
1205  if (state->array_nl) {
1206  ruby_xfree(state->array_nl);
1207  state->array_nl = NULL;
1208  }
1209  } else {
1210  if (state->array_nl) ruby_xfree(state->array_nl);
1211  state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
1212  state->array_nl_len = len;
1213  }
1214  return Qnil;
1215 }
1216 
1217 
1218 /*
1219 * call-seq: check_circular?
1220 *
1221 * Returns true, if circular data structures should be checked,
1222 * otherwise returns false.
1223 */
1224 static VALUE cState_check_circular_p(VALUE self)
1225 {
1226  GET_STATE(self);
1227  return state->max_nesting ? Qtrue : Qfalse;
1228 }
1229 
1230 /*
1231  * call-seq: max_nesting
1232  *
1233  * This integer returns the maximum level of data structure nesting in
1234  * the generated JSON, max_nesting = 0 if no maximum is checked.
1235  */
1236 static VALUE cState_max_nesting(VALUE self)
1237 {
1238  GET_STATE(self);
1239  return LONG2FIX(state->max_nesting);
1240 }
1241 
1242 /*
1243  * call-seq: max_nesting=(depth)
1244  *
1245  * This sets the maximum level of data structure nesting in the generated JSON
1246  * to the integer depth, max_nesting = 0 if no maximum should be checked.
1247  */
1248 static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1249 {
1250  GET_STATE(self);
1251  Check_Type(depth, T_FIXNUM);
1252  return state->max_nesting = FIX2LONG(depth);
1253 }
1254 
1255 /*
1256  * call-seq: allow_nan?
1257  *
1258  * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1259  * returns false.
1260  */
1261 static VALUE cState_allow_nan_p(VALUE self)
1262 {
1263  GET_STATE(self);
1264  return state->allow_nan ? Qtrue : Qfalse;
1265 }
1266 
1267 /*
1268  * call-seq: ascii_only?
1269  *
1270  * Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
1271  * returns false.
1272  */
1273 static VALUE cState_ascii_only_p(VALUE self)
1274 {
1275  GET_STATE(self);
1276  return state->ascii_only ? Qtrue : Qfalse;
1277 }
1278 
1279 /*
1280  * call-seq: depth
1281  *
1282  * This integer returns the current depth of data structure nesting.
1283  */
1284 static VALUE cState_depth(VALUE self)
1285 {
1286  GET_STATE(self);
1287  return LONG2FIX(state->depth);
1288 }
1289 
1290 /*
1291  * call-seq: depth=(depth)
1292  *
1293  * This sets the maximum level of data structure nesting in the generated JSON
1294  * to the integer depth, max_nesting = 0 if no maximum should be checked.
1295  */
1296 static VALUE cState_depth_set(VALUE self, VALUE depth)
1297 {
1298  GET_STATE(self);
1299  Check_Type(depth, T_FIXNUM);
1300  state->depth = FIX2LONG(depth);
1301  return Qnil;
1302 }
1303 
1304 /*
1305  * call-seq: buffer_initial_length
1306  *
1307  * This integer returns the current initial length of the buffer.
1308  */
1309 static VALUE cState_buffer_initial_length(VALUE self)
1310 {
1311  GET_STATE(self);
1312  return LONG2FIX(state->buffer_initial_length);
1313 }
1314 
1315 /*
1316  * call-seq: buffer_initial_length=(length)
1317  *
1318  * This sets the initial length of the buffer to +length+, if +length+ > 0,
1319  * otherwise its value isn't changed.
1320  */
1321 static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
1322 {
1323  long initial_length;
1324  GET_STATE(self);
1325  Check_Type(buffer_initial_length, T_FIXNUM);
1326  initial_length = FIX2LONG(buffer_initial_length);
1327  if (initial_length > 0) {
1328  state->buffer_initial_length = initial_length;
1329  }
1330  return Qnil;
1331 }
1332 
1333 /*
1334  *
1335  */
1336 void Init_generator(void)
1337 {
1338  rb_require("json/common");
1339 
1340  mJSON = rb_define_module("JSON");
1341  mExt = rb_define_module_under(mJSON, "Ext");
1342  mGenerator = rb_define_module_under(mExt, "Generator");
1343 
1344  eGeneratorError = rb_path2class("JSON::GeneratorError");
1345  eNestingError = rb_path2class("JSON::NestingError");
1346 
1347  cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1348  rb_define_alloc_func(cState, cState_s_allocate);
1349  rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1350  rb_define_method(cState, "initialize", cState_initialize, -1);
1351  rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
1352  rb_define_method(cState, "indent", cState_indent, 0);
1353  rb_define_method(cState, "indent=", cState_indent_set, 1);
1354  rb_define_method(cState, "space", cState_space, 0);
1355  rb_define_method(cState, "space=", cState_space_set, 1);
1356  rb_define_method(cState, "space_before", cState_space_before, 0);
1357  rb_define_method(cState, "space_before=", cState_space_before_set, 1);
1358  rb_define_method(cState, "object_nl", cState_object_nl, 0);
1359  rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
1360  rb_define_method(cState, "array_nl", cState_array_nl, 0);
1361  rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1362  rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1363  rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1364  rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1365  rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1366  rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
1367  rb_define_method(cState, "depth", cState_depth, 0);
1368  rb_define_method(cState, "depth=", cState_depth_set, 1);
1369  rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
1370  rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
1371  rb_define_method(cState, "configure", cState_configure, 1);
1372  rb_define_alias(cState, "merge", "configure");
1373  rb_define_method(cState, "to_h", cState_to_h, 0);
1374  rb_define_alias(cState, "to_hash", "to_h");
1375  rb_define_method(cState, "[]", cState_aref, 1);
1376  rb_define_method(cState, "[]=", cState_aset, 2);
1377  rb_define_method(cState, "generate", cState_generate, 1);
1378 
1379  mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1380  mObject = rb_define_module_under(mGeneratorMethods, "Object");
1381  rb_define_method(mObject, "to_json", mObject_to_json, -1);
1382  mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1383  rb_define_method(mHash, "to_json", mHash_to_json, -1);
1384  mArray = rb_define_module_under(mGeneratorMethods, "Array");
1385  rb_define_method(mArray, "to_json", mArray_to_json, -1);
1386 #ifdef RUBY_INTEGER_UNIFICATION
1387  mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1388  rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1389 #else
1390  mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1391  rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
1392  mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1393  rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
1394 #endif
1395  mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1396  rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1397  mString = rb_define_module_under(mGeneratorMethods, "String");
1398  rb_define_singleton_method(mString, "included", mString_included_s, 1);
1399  rb_define_method(mString, "to_json", mString_to_json, -1);
1400  rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
1401  rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
1402  mString_Extend = rb_define_module_under(mString, "Extend");
1403  rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
1404  mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1405  rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
1406  mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1407  rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
1408  mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1409  rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1410 
1411  CRegexp_MULTILINE = rb_const_get(rb_cRegexp, rb_intern("MULTILINE"));
1412  i_to_s = rb_intern("to_s");
1413  i_to_json = rb_intern("to_json");
1414  i_new = rb_intern("new");
1415  i_indent = rb_intern("indent");
1416  i_space = rb_intern("space");
1417  i_space_before = rb_intern("space_before");
1418  i_object_nl = rb_intern("object_nl");
1419  i_array_nl = rb_intern("array_nl");
1420  i_max_nesting = rb_intern("max_nesting");
1421  i_allow_nan = rb_intern("allow_nan");
1422  i_ascii_only = rb_intern("ascii_only");
1423  i_depth = rb_intern("depth");
1424  i_buffer_initial_length = rb_intern("buffer_initial_length");
1425  i_pack = rb_intern("pack");
1426  i_unpack = rb_intern("unpack");
1427  i_create_id = rb_intern("create_id");
1428  i_extend = rb_intern("extend");
1429  i_key_p = rb_intern("key?");
1430  i_aref = rb_intern("[]");
1431  i_send = rb_intern("__send__");
1432  i_respond_to_p = rb_intern("respond_to?");
1433  i_match = rb_intern("match");
1434  i_keys = rb_intern("keys");
1435  i_dup = rb_intern("dup");
1436 #ifdef HAVE_RUBY_ENCODING_H
1437  CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1438  i_encoding = rb_intern("encoding");
1439  i_encode = rb_intern("encode");
1440 #endif
1441  i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
1442  CJSON_SAFE_STATE_PROTOTYPE = Qnil;
1443 }
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1927
#define FBUFFER_CAPA(fb)
Definition: fbuffer.h:57
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1215
RUBY_EXTERN VALUE rb_cFloat
Definition: ruby.h:1910
#define RARRAY_LEN(a)
Definition: ruby.h:1019
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1138
#define T_FIXNUM
Definition: ruby.h:503
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define CLASS_OF(v)
Definition: ruby.h:453
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define Qtrue
Definition: ruby.h:437
VALUE rb_cHash
Definition: hash.c:82
#define option_given_p(opts, key)
Definition: generator.h:23
unsigned char UTF8
Definition: generator.h:31
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2999
#define RB_OBJ_STRING(obj)
Definition: bigdecimal.c:99
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:3087
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define Check_Type(v, t)
Definition: ruby.h:562
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1210
void Init_generator(void)
Definition: generator.c:1336
#define RB_GC_GUARD(v)
Definition: ruby.h:552
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
#define T_HASH
Definition: ruby.h:499
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Tries to convert an object into another type.
Definition: object.c:3006
#define FIXNUM_P(f)
Definition: ruby.h:365
VALUE rb_path2class(const char *)
Definition: variable.c:432
unsigned short UTF16
Definition: generator.h:30
#define FBUFFER_INITIAL_LENGTH_DEFAULT
Definition: fbuffer.h:53
VALUE rb_eArgError
Definition: error.c:802
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
Definition: object.c:277
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
Definition: object.c:842
VALUE rb_require(const char *)
Definition: load.c:1061
#define rb_intern_str(string)
Definition: generator.h:16
VALUE rb_class_name(VALUE)
Definition: variable.c:444
#define ALLOC_N(type, n)
Definition: ruby.h:1587
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1616
VALUE rb_str_substr(VALUE, long, long)
Definition: string.c:2517
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
VALUE rb_str_cat2(VALUE, const char *)
unsigned long UTF32
Definition: generator.h:29
#define NIL_P(v)
Definition: ruby.h:451
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
#define T_BIGNUM
Definition: ruby.h:501
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
RUBY_EXTERN int isinf(double)
Definition: isinf.c:56
#define rb_str_new2
Definition: intern.h:835
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2292
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
RUBY_EXTERN VALUE rb_cRegexp
Definition: ruby.h:1925
#define RSTRING_LEN(str)
Definition: ruby.h:971
VALUE rb_hash_new(void)
Definition: hash.c:424
void ruby_xfree(void *x)
Definition: gc.c:8085
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1315
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
#define Qnil
Definition: ruby.h:438
unsigned long VALUE
Definition: ruby.h:85
#define UNI_SUR_HIGH_START
Definition: generator.h:39
#define isnan(x)
Definition: win32.h:346
VALUE rb_str_dup(VALUE)
Definition: string.c:1488
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1994
register unsigned int len
Definition: zonetab.h:51
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define StringValueCStr(v)
Definition: ruby.h:571
#define RSTRING_PTR(str)
Definition: ruby.h:975
#define UNI_MAX_BMP
Definition: generator.h:34
#define RFLOAT_VALUE(v)
Definition: ruby.h:933
int size
Definition: encoding.c:57
#define UNI_SUR_LOW_END
Definition: generator.h:42
#define GENERATE_JSON(type)
Definition: generator.h:86
#define FBUFFER_PTR(fb)
Definition: fbuffer.h:55
#define UNI_MAX_UTF16
Definition: generator.h:35
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:831
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
Definition: object.c:2965
#define LONG2FIX(i)
Definition: ruby.h:234
#define RTEST(v)
Definition: ruby.h:450
#define T_STRING
Definition: ruby.h:496
#define GET_STATE_TO(self, state)
Definition: generator.h:79
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1175
#define UNI_SUR_LOW_START
Definition: generator.h:41
#define UNI_REPLACEMENT_CHAR
Definition: generator.h:33
VALUE rb_cArray
Definition: array.c:26
const char * name
Definition: nkf.c:208
#define ID2SYM(x)
Definition: ruby.h:383
VALUE rb_str_intern(VALUE)
Definition: symbol.c:661
#define FBUFFER_LEN(fb)
Definition: fbuffer.h:56
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define rb_intern(str)
#define NULL
Definition: _sdbm.c:102
#define FIX2LONG(x)
Definition: ruby.h:363
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
#define rb_obj_instance_variables(object)
Definition: generator.h:20
#define GET_STATE(self)
Definition: generator.h:82
char ** argv
Definition: ruby.c:188
VALUE rb_str_new(const char *, long)
Definition: string.c:737