json_pure 2.4.0 → 2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +14 -0
- data/VERSION +1 -1
- data/json_pure.gemspec +47 -19
- data/lib/json/version.rb +1 -1
- data/tests/fixtures/fail29.json +1 -0
- data/tests/fixtures/fail30.json +1 -0
- data/tests/fixtures/fail31.json +1 -0
- data/tests/fixtures/fail32.json +1 -0
- metadata +16 -48
- data/.gitignore +0 -18
- data/.travis.yml +0 -23
- data/README-json-jruby.md +0 -33
- data/Rakefile +0 -334
- data/diagrams/.keep +0 -0
- data/ext/json/ext/fbuffer/fbuffer.h +0 -187
- data/ext/json/ext/generator/depend +0 -1
- data/ext/json/ext/generator/extconf.rb +0 -4
- data/ext/json/ext/generator/generator.c +0 -1612
- data/ext/json/ext/generator/generator.h +0 -174
- data/ext/json/ext/parser/depend +0 -1
- data/ext/json/ext/parser/extconf.rb +0 -31
- data/ext/json/ext/parser/parser.c +0 -2164
- data/ext/json/ext/parser/parser.h +0 -92
- data/ext/json/ext/parser/parser.rl +0 -924
- data/ext/json/extconf.rb +0 -3
- data/install.rb +0 -23
- data/java/src/json/ext/ByteListTranscoder.java +0 -166
- data/java/src/json/ext/Generator.java +0 -447
- data/java/src/json/ext/GeneratorMethods.java +0 -231
- data/java/src/json/ext/GeneratorService.java +0 -42
- data/java/src/json/ext/GeneratorState.java +0 -520
- data/java/src/json/ext/OptionsReader.java +0 -113
- data/java/src/json/ext/Parser.java +0 -2374
- data/java/src/json/ext/Parser.rl +0 -905
- data/java/src/json/ext/ParserService.java +0 -34
- data/java/src/json/ext/RuntimeInfo.java +0 -116
- data/java/src/json/ext/StringDecoder.java +0 -166
- data/java/src/json/ext/StringEncoder.java +0 -117
- data/java/src/json/ext/Utils.java +0 -88
- data/json-java.gemspec +0 -38
- data/json.gemspec +0 -140
- data/references/rfc7159.txt +0 -899
- data/tools/diff.sh +0 -18
- data/tools/fuzz.rb +0 -131
- data/tools/server.rb +0 -62
data/diagrams/.keep
DELETED
File without changes
|
@@ -1,187 +0,0 @@
|
|
1
|
-
|
2
|
-
#ifndef _FBUFFER_H_
|
3
|
-
#define _FBUFFER_H_
|
4
|
-
|
5
|
-
#include "ruby.h"
|
6
|
-
|
7
|
-
#ifndef RHASH_SIZE
|
8
|
-
#define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
|
9
|
-
#endif
|
10
|
-
|
11
|
-
#ifndef RFLOAT_VALUE
|
12
|
-
#define RFLOAT_VALUE(val) (RFLOAT(val)->value)
|
13
|
-
#endif
|
14
|
-
|
15
|
-
#ifndef RARRAY_LEN
|
16
|
-
#define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
|
17
|
-
#endif
|
18
|
-
#ifndef RSTRING_PTR
|
19
|
-
#define RSTRING_PTR(string) RSTRING(string)->ptr
|
20
|
-
#endif
|
21
|
-
#ifndef RSTRING_LEN
|
22
|
-
#define RSTRING_LEN(string) RSTRING(string)->len
|
23
|
-
#endif
|
24
|
-
|
25
|
-
#ifdef PRIsVALUE
|
26
|
-
# define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
|
27
|
-
# define RB_OBJ_STRING(obj) (obj)
|
28
|
-
#else
|
29
|
-
# define PRIsVALUE "s"
|
30
|
-
# define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
|
31
|
-
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
|
32
|
-
#endif
|
33
|
-
|
34
|
-
#ifdef HAVE_RUBY_ENCODING_H
|
35
|
-
#include "ruby/encoding.h"
|
36
|
-
#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
|
37
|
-
#else
|
38
|
-
#define FORCE_UTF8(obj)
|
39
|
-
#endif
|
40
|
-
|
41
|
-
/* We don't need to guard objects for rbx, so let's do nothing at all. */
|
42
|
-
#ifndef RB_GC_GUARD
|
43
|
-
#define RB_GC_GUARD(object)
|
44
|
-
#endif
|
45
|
-
|
46
|
-
typedef struct FBufferStruct {
|
47
|
-
unsigned long initial_length;
|
48
|
-
char *ptr;
|
49
|
-
unsigned long len;
|
50
|
-
unsigned long capa;
|
51
|
-
} FBuffer;
|
52
|
-
|
53
|
-
#define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
|
54
|
-
|
55
|
-
#define FBUFFER_PTR(fb) (fb->ptr)
|
56
|
-
#define FBUFFER_LEN(fb) (fb->len)
|
57
|
-
#define FBUFFER_CAPA(fb) (fb->capa)
|
58
|
-
#define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
|
59
|
-
|
60
|
-
static FBuffer *fbuffer_alloc(unsigned long initial_length);
|
61
|
-
static void fbuffer_free(FBuffer *fb);
|
62
|
-
static void fbuffer_clear(FBuffer *fb);
|
63
|
-
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
|
64
|
-
#ifdef JSON_GENERATOR
|
65
|
-
static void fbuffer_append_long(FBuffer *fb, long number);
|
66
|
-
#endif
|
67
|
-
static void fbuffer_append_char(FBuffer *fb, char newchr);
|
68
|
-
#ifdef JSON_GENERATOR
|
69
|
-
static FBuffer *fbuffer_dup(FBuffer *fb);
|
70
|
-
static VALUE fbuffer_to_s(FBuffer *fb);
|
71
|
-
#endif
|
72
|
-
|
73
|
-
static FBuffer *fbuffer_alloc(unsigned long initial_length)
|
74
|
-
{
|
75
|
-
FBuffer *fb;
|
76
|
-
if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
|
77
|
-
fb = ALLOC(FBuffer);
|
78
|
-
memset((void *) fb, 0, sizeof(FBuffer));
|
79
|
-
fb->initial_length = initial_length;
|
80
|
-
return fb;
|
81
|
-
}
|
82
|
-
|
83
|
-
static void fbuffer_free(FBuffer *fb)
|
84
|
-
{
|
85
|
-
if (fb->ptr) ruby_xfree(fb->ptr);
|
86
|
-
ruby_xfree(fb);
|
87
|
-
}
|
88
|
-
|
89
|
-
static void fbuffer_clear(FBuffer *fb)
|
90
|
-
{
|
91
|
-
fb->len = 0;
|
92
|
-
}
|
93
|
-
|
94
|
-
static void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
|
95
|
-
{
|
96
|
-
unsigned long required;
|
97
|
-
|
98
|
-
if (!fb->ptr) {
|
99
|
-
fb->ptr = ALLOC_N(char, fb->initial_length);
|
100
|
-
fb->capa = fb->initial_length;
|
101
|
-
}
|
102
|
-
|
103
|
-
for (required = fb->capa; requested > required - fb->len; required <<= 1);
|
104
|
-
|
105
|
-
if (required > fb->capa) {
|
106
|
-
REALLOC_N(fb->ptr, char, required);
|
107
|
-
fb->capa = required;
|
108
|
-
}
|
109
|
-
}
|
110
|
-
|
111
|
-
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
|
112
|
-
{
|
113
|
-
if (len > 0) {
|
114
|
-
fbuffer_inc_capa(fb, len);
|
115
|
-
MEMCPY(fb->ptr + fb->len, newstr, char, len);
|
116
|
-
fb->len += len;
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
#ifdef JSON_GENERATOR
|
121
|
-
static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
122
|
-
{
|
123
|
-
const char *newstr = StringValuePtr(str);
|
124
|
-
unsigned long len = RSTRING_LEN(str);
|
125
|
-
|
126
|
-
RB_GC_GUARD(str);
|
127
|
-
|
128
|
-
fbuffer_append(fb, newstr, len);
|
129
|
-
}
|
130
|
-
#endif
|
131
|
-
|
132
|
-
static void fbuffer_append_char(FBuffer *fb, char newchr)
|
133
|
-
{
|
134
|
-
fbuffer_inc_capa(fb, 1);
|
135
|
-
*(fb->ptr + fb->len) = newchr;
|
136
|
-
fb->len++;
|
137
|
-
}
|
138
|
-
|
139
|
-
#ifdef JSON_GENERATOR
|
140
|
-
static void freverse(char *start, char *end)
|
141
|
-
{
|
142
|
-
char c;
|
143
|
-
|
144
|
-
while (end > start) {
|
145
|
-
c = *end, *end-- = *start, *start++ = c;
|
146
|
-
}
|
147
|
-
}
|
148
|
-
|
149
|
-
static long fltoa(long number, char *buf)
|
150
|
-
{
|
151
|
-
static char digits[] = "0123456789";
|
152
|
-
long sign = number;
|
153
|
-
char* tmp = buf;
|
154
|
-
|
155
|
-
if (sign < 0) number = -number;
|
156
|
-
do *tmp++ = digits[number % 10]; while (number /= 10);
|
157
|
-
if (sign < 0) *tmp++ = '-';
|
158
|
-
freverse(buf, tmp - 1);
|
159
|
-
return tmp - buf;
|
160
|
-
}
|
161
|
-
|
162
|
-
static void fbuffer_append_long(FBuffer *fb, long number)
|
163
|
-
{
|
164
|
-
char buf[20];
|
165
|
-
unsigned long len = fltoa(number, buf);
|
166
|
-
fbuffer_append(fb, buf, len);
|
167
|
-
}
|
168
|
-
|
169
|
-
static FBuffer *fbuffer_dup(FBuffer *fb)
|
170
|
-
{
|
171
|
-
unsigned long len = fb->len;
|
172
|
-
FBuffer *result;
|
173
|
-
|
174
|
-
result = fbuffer_alloc(len);
|
175
|
-
fbuffer_append(result, FBUFFER_PAIR(fb));
|
176
|
-
return result;
|
177
|
-
}
|
178
|
-
|
179
|
-
static VALUE fbuffer_to_s(FBuffer *fb)
|
180
|
-
{
|
181
|
-
VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
|
182
|
-
fbuffer_free(fb);
|
183
|
-
FORCE_UTF8(result);
|
184
|
-
return result;
|
185
|
-
}
|
186
|
-
#endif
|
187
|
-
#endif
|
@@ -1 +0,0 @@
|
|
1
|
-
generator.o: generator.c generator.h $(srcdir)/../fbuffer/fbuffer.h
|
@@ -1,1612 +0,0 @@
|
|
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,
|
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, i_escape_slash;
|
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, char escape_slash)
|
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
|
-
case '/':
|
184
|
-
if(escape_slash) {
|
185
|
-
fbuffer_append(buffer, "\\/", 2);
|
186
|
-
break;
|
187
|
-
}
|
188
|
-
default:
|
189
|
-
fbuffer_append_char(buffer, (char)ch);
|
190
|
-
break;
|
191
|
-
}
|
192
|
-
} else {
|
193
|
-
switch (ch) {
|
194
|
-
case '\n':
|
195
|
-
fbuffer_append(buffer, "\\n", 2);
|
196
|
-
break;
|
197
|
-
case '\r':
|
198
|
-
fbuffer_append(buffer, "\\r", 2);
|
199
|
-
break;
|
200
|
-
case '\t':
|
201
|
-
fbuffer_append(buffer, "\\t", 2);
|
202
|
-
break;
|
203
|
-
case '\f':
|
204
|
-
fbuffer_append(buffer, "\\f", 2);
|
205
|
-
break;
|
206
|
-
case '\b':
|
207
|
-
fbuffer_append(buffer, "\\b", 2);
|
208
|
-
break;
|
209
|
-
default:
|
210
|
-
unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
|
211
|
-
break;
|
212
|
-
}
|
213
|
-
}
|
214
|
-
}
|
215
|
-
} else if (ch > UNI_MAX_UTF16) {
|
216
|
-
#if UNI_STRICT_CONVERSION
|
217
|
-
source -= (extraBytesToRead+1); /* return to the start */
|
218
|
-
rb_raise(rb_path2class("JSON::GeneratorError"),
|
219
|
-
"source sequence is illegal/malformed utf8");
|
220
|
-
#else
|
221
|
-
unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
|
222
|
-
#endif
|
223
|
-
} else {
|
224
|
-
/* target is a character in range 0xFFFF - 0x10FFFF. */
|
225
|
-
ch -= halfBase;
|
226
|
-
unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
|
227
|
-
unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
|
228
|
-
}
|
229
|
-
}
|
230
|
-
RB_GC_GUARD(string);
|
231
|
-
}
|
232
|
-
|
233
|
-
/* Converts string to a JSON string in FBuffer buffer, where only the
|
234
|
-
* characters required by the JSON standard are JSON escaped. The remaining
|
235
|
-
* characters (should be UTF8) are just passed through and appended to the
|
236
|
-
* result. */
|
237
|
-
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char escape_slash)
|
238
|
-
{
|
239
|
-
const char *ptr = RSTRING_PTR(string), *p;
|
240
|
-
unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
|
241
|
-
const char *escape = NULL;
|
242
|
-
int escape_len;
|
243
|
-
unsigned char c;
|
244
|
-
char buf[6] = { '\\', 'u' };
|
245
|
-
int ascii_only = rb_enc_str_asciionly_p(string);
|
246
|
-
|
247
|
-
for (start = 0, end = 0; end < len;) {
|
248
|
-
p = ptr + end;
|
249
|
-
c = (unsigned char) *p;
|
250
|
-
if (c < 0x20) {
|
251
|
-
switch (c) {
|
252
|
-
case '\n':
|
253
|
-
escape = "\\n";
|
254
|
-
escape_len = 2;
|
255
|
-
break;
|
256
|
-
case '\r':
|
257
|
-
escape = "\\r";
|
258
|
-
escape_len = 2;
|
259
|
-
break;
|
260
|
-
case '\t':
|
261
|
-
escape = "\\t";
|
262
|
-
escape_len = 2;
|
263
|
-
break;
|
264
|
-
case '\f':
|
265
|
-
escape = "\\f";
|
266
|
-
escape_len = 2;
|
267
|
-
break;
|
268
|
-
case '\b':
|
269
|
-
escape = "\\b";
|
270
|
-
escape_len = 2;
|
271
|
-
break;
|
272
|
-
default:
|
273
|
-
unicode_escape(buf, (UTF16) *p);
|
274
|
-
escape = buf;
|
275
|
-
escape_len = 6;
|
276
|
-
break;
|
277
|
-
}
|
278
|
-
} else {
|
279
|
-
switch (c) {
|
280
|
-
case '\\':
|
281
|
-
escape = "\\\\";
|
282
|
-
escape_len = 2;
|
283
|
-
break;
|
284
|
-
case '"':
|
285
|
-
escape = "\\\"";
|
286
|
-
escape_len = 2;
|
287
|
-
break;
|
288
|
-
case '/':
|
289
|
-
if(escape_slash) {
|
290
|
-
escape = "\\/";
|
291
|
-
escape_len = 2;
|
292
|
-
break;
|
293
|
-
}
|
294
|
-
default:
|
295
|
-
{
|
296
|
-
unsigned short clen = 1;
|
297
|
-
if (!ascii_only) {
|
298
|
-
clen += trailingBytesForUTF8[c];
|
299
|
-
if (end + clen > len) {
|
300
|
-
rb_raise(rb_path2class("JSON::GeneratorError"),
|
301
|
-
"partial character in source, but hit end");
|
302
|
-
}
|
303
|
-
if (!isLegalUTF8((UTF8 *) p, clen)) {
|
304
|
-
rb_raise(rb_path2class("JSON::GeneratorError"),
|
305
|
-
"source sequence is illegal/malformed utf-8");
|
306
|
-
}
|
307
|
-
}
|
308
|
-
end += clen;
|
309
|
-
}
|
310
|
-
continue;
|
311
|
-
break;
|
312
|
-
}
|
313
|
-
}
|
314
|
-
fbuffer_append(buffer, ptr + start, end - start);
|
315
|
-
fbuffer_append(buffer, escape, escape_len);
|
316
|
-
start = ++end;
|
317
|
-
escape = NULL;
|
318
|
-
}
|
319
|
-
fbuffer_append(buffer, ptr + start, end - start);
|
320
|
-
}
|
321
|
-
|
322
|
-
static char *fstrndup(const char *ptr, unsigned long len) {
|
323
|
-
char *result;
|
324
|
-
if (len <= 0) return NULL;
|
325
|
-
result = ALLOC_N(char, len);
|
326
|
-
memcpy(result, ptr, len);
|
327
|
-
return result;
|
328
|
-
}
|
329
|
-
|
330
|
-
/*
|
331
|
-
* Document-module: JSON::Ext::Generator
|
332
|
-
*
|
333
|
-
* This is the JSON generator implemented as a C extension. It can be
|
334
|
-
* configured to be used by setting
|
335
|
-
*
|
336
|
-
* JSON.generator = JSON::Ext::Generator
|
337
|
-
*
|
338
|
-
* with the method generator= in JSON.
|
339
|
-
*
|
340
|
-
*/
|
341
|
-
|
342
|
-
/* Explanation of the following: that's the only way to not pollute
|
343
|
-
* standard library's docs with GeneratorMethods::<ClassName> which
|
344
|
-
* are uninformative and take a large place in a list of classes
|
345
|
-
*/
|
346
|
-
|
347
|
-
/*
|
348
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods
|
349
|
-
* :nodoc:
|
350
|
-
*/
|
351
|
-
|
352
|
-
/*
|
353
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Array
|
354
|
-
* :nodoc:
|
355
|
-
*/
|
356
|
-
|
357
|
-
/*
|
358
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Bignum
|
359
|
-
* :nodoc:
|
360
|
-
*/
|
361
|
-
|
362
|
-
/*
|
363
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::FalseClass
|
364
|
-
* :nodoc:
|
365
|
-
*/
|
366
|
-
|
367
|
-
/*
|
368
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Fixnum
|
369
|
-
* :nodoc:
|
370
|
-
*/
|
371
|
-
|
372
|
-
/*
|
373
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Float
|
374
|
-
* :nodoc:
|
375
|
-
*/
|
376
|
-
|
377
|
-
/*
|
378
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Hash
|
379
|
-
* :nodoc:
|
380
|
-
*/
|
381
|
-
|
382
|
-
/*
|
383
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Integer
|
384
|
-
* :nodoc:
|
385
|
-
*/
|
386
|
-
|
387
|
-
/*
|
388
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::NilClass
|
389
|
-
* :nodoc:
|
390
|
-
*/
|
391
|
-
|
392
|
-
/*
|
393
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::Object
|
394
|
-
* :nodoc:
|
395
|
-
*/
|
396
|
-
|
397
|
-
/*
|
398
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::String
|
399
|
-
* :nodoc:
|
400
|
-
*/
|
401
|
-
|
402
|
-
/*
|
403
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::String::Extend
|
404
|
-
* :nodoc:
|
405
|
-
*/
|
406
|
-
|
407
|
-
/*
|
408
|
-
* Document-module: JSON::Ext::Generator::GeneratorMethods::TrueClass
|
409
|
-
* :nodoc:
|
410
|
-
*/
|
411
|
-
|
412
|
-
/*
|
413
|
-
* call-seq: to_json(state = nil)
|
414
|
-
*
|
415
|
-
* Returns a JSON string containing a JSON object, that is generated from
|
416
|
-
* this Hash instance.
|
417
|
-
* _state_ is a JSON::State object, that can also be used to configure the
|
418
|
-
* produced JSON string output further.
|
419
|
-
*/
|
420
|
-
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
|
421
|
-
{
|
422
|
-
GENERATE_JSON(object);
|
423
|
-
}
|
424
|
-
|
425
|
-
/*
|
426
|
-
* call-seq: to_json(state = nil)
|
427
|
-
*
|
428
|
-
* Returns a JSON string containing a JSON array, that is generated from
|
429
|
-
* this Array instance.
|
430
|
-
* _state_ is a JSON::State object, that can also be used to configure the
|
431
|
-
* produced JSON string output further.
|
432
|
-
*/
|
433
|
-
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
|
434
|
-
GENERATE_JSON(array);
|
435
|
-
}
|
436
|
-
|
437
|
-
#ifdef RUBY_INTEGER_UNIFICATION
|
438
|
-
/*
|
439
|
-
* call-seq: to_json(*)
|
440
|
-
*
|
441
|
-
* Returns a JSON string representation for this Integer number.
|
442
|
-
*/
|
443
|
-
static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
|
444
|
-
{
|
445
|
-
GENERATE_JSON(integer);
|
446
|
-
}
|
447
|
-
|
448
|
-
#else
|
449
|
-
/*
|
450
|
-
* call-seq: to_json(*)
|
451
|
-
*
|
452
|
-
* Returns a JSON string representation for this Integer number.
|
453
|
-
*/
|
454
|
-
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
|
455
|
-
{
|
456
|
-
GENERATE_JSON(fixnum);
|
457
|
-
}
|
458
|
-
|
459
|
-
/*
|
460
|
-
* call-seq: to_json(*)
|
461
|
-
*
|
462
|
-
* Returns a JSON string representation for this Integer number.
|
463
|
-
*/
|
464
|
-
static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
|
465
|
-
{
|
466
|
-
GENERATE_JSON(bignum);
|
467
|
-
}
|
468
|
-
#endif
|
469
|
-
|
470
|
-
/*
|
471
|
-
* call-seq: to_json(*)
|
472
|
-
*
|
473
|
-
* Returns a JSON string representation for this Float number.
|
474
|
-
*/
|
475
|
-
static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
|
476
|
-
{
|
477
|
-
GENERATE_JSON(float);
|
478
|
-
}
|
479
|
-
|
480
|
-
/*
|
481
|
-
* call-seq: String.included(modul)
|
482
|
-
*
|
483
|
-
* Extends _modul_ with the String::Extend module.
|
484
|
-
*/
|
485
|
-
static VALUE mString_included_s(VALUE self, VALUE modul) {
|
486
|
-
VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
|
487
|
-
return result;
|
488
|
-
}
|
489
|
-
|
490
|
-
/*
|
491
|
-
* call-seq: to_json(*)
|
492
|
-
*
|
493
|
-
* This string should be encoded with UTF-8 A call to this method
|
494
|
-
* returns a JSON string encoded with UTF16 big endian characters as
|
495
|
-
* \u????.
|
496
|
-
*/
|
497
|
-
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
|
498
|
-
{
|
499
|
-
GENERATE_JSON(string);
|
500
|
-
}
|
501
|
-
|
502
|
-
/*
|
503
|
-
* call-seq: to_json_raw_object()
|
504
|
-
*
|
505
|
-
* This method creates a raw object hash, that can be nested into
|
506
|
-
* other data structures and will be generated as a raw string. This
|
507
|
-
* method should be used, if you want to convert raw strings to JSON
|
508
|
-
* instead of UTF-8 strings, e. g. binary data.
|
509
|
-
*/
|
510
|
-
static VALUE mString_to_json_raw_object(VALUE self)
|
511
|
-
{
|
512
|
-
VALUE ary;
|
513
|
-
VALUE result = rb_hash_new();
|
514
|
-
rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
|
515
|
-
ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
|
516
|
-
rb_hash_aset(result, rb_str_new2("raw"), ary);
|
517
|
-
return result;
|
518
|
-
}
|
519
|
-
|
520
|
-
/*
|
521
|
-
* call-seq: to_json_raw(*args)
|
522
|
-
*
|
523
|
-
* This method creates a JSON text from the result of a call to
|
524
|
-
* to_json_raw_object of this String.
|
525
|
-
*/
|
526
|
-
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
|
527
|
-
{
|
528
|
-
VALUE obj = mString_to_json_raw_object(self);
|
529
|
-
Check_Type(obj, T_HASH);
|
530
|
-
return mHash_to_json(argc, argv, obj);
|
531
|
-
}
|
532
|
-
|
533
|
-
/*
|
534
|
-
* call-seq: json_create(o)
|
535
|
-
*
|
536
|
-
* Raw Strings are JSON Objects (the raw bytes are stored in an array for the
|
537
|
-
* key "raw"). The Ruby String can be created by this module method.
|
538
|
-
*/
|
539
|
-
static VALUE mString_Extend_json_create(VALUE self, VALUE o)
|
540
|
-
{
|
541
|
-
VALUE ary;
|
542
|
-
Check_Type(o, T_HASH);
|
543
|
-
ary = rb_hash_aref(o, rb_str_new2("raw"));
|
544
|
-
return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
|
545
|
-
}
|
546
|
-
|
547
|
-
/*
|
548
|
-
* call-seq: to_json(*)
|
549
|
-
*
|
550
|
-
* Returns a JSON string for true: 'true'.
|
551
|
-
*/
|
552
|
-
static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
|
553
|
-
{
|
554
|
-
GENERATE_JSON(true);
|
555
|
-
}
|
556
|
-
|
557
|
-
/*
|
558
|
-
* call-seq: to_json(*)
|
559
|
-
*
|
560
|
-
* Returns a JSON string for false: 'false'.
|
561
|
-
*/
|
562
|
-
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
|
563
|
-
{
|
564
|
-
GENERATE_JSON(false);
|
565
|
-
}
|
566
|
-
|
567
|
-
/*
|
568
|
-
* call-seq: to_json(*)
|
569
|
-
*
|
570
|
-
* Returns a JSON string for nil: 'null'.
|
571
|
-
*/
|
572
|
-
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
|
573
|
-
{
|
574
|
-
GENERATE_JSON(null);
|
575
|
-
}
|
576
|
-
|
577
|
-
/*
|
578
|
-
* call-seq: to_json(*)
|
579
|
-
*
|
580
|
-
* Converts this object to a string (calling #to_s), converts
|
581
|
-
* it to a JSON string, and returns the result. This is a fallback, if no
|
582
|
-
* special method #to_json was defined for some object.
|
583
|
-
*/
|
584
|
-
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
|
585
|
-
{
|
586
|
-
VALUE state;
|
587
|
-
VALUE string = rb_funcall(self, i_to_s, 0);
|
588
|
-
rb_scan_args(argc, argv, "01", &state);
|
589
|
-
Check_Type(string, T_STRING);
|
590
|
-
state = cState_from_state_s(cState, state);
|
591
|
-
return cState_partial_generate(state, string);
|
592
|
-
}
|
593
|
-
|
594
|
-
static void State_free(void *ptr)
|
595
|
-
{
|
596
|
-
JSON_Generator_State *state = ptr;
|
597
|
-
if (state->indent) ruby_xfree(state->indent);
|
598
|
-
if (state->space) ruby_xfree(state->space);
|
599
|
-
if (state->space_before) ruby_xfree(state->space_before);
|
600
|
-
if (state->object_nl) ruby_xfree(state->object_nl);
|
601
|
-
if (state->array_nl) ruby_xfree(state->array_nl);
|
602
|
-
if (state->array_delim) fbuffer_free(state->array_delim);
|
603
|
-
if (state->object_delim) fbuffer_free(state->object_delim);
|
604
|
-
if (state->object_delim2) fbuffer_free(state->object_delim2);
|
605
|
-
ruby_xfree(state);
|
606
|
-
}
|
607
|
-
|
608
|
-
static size_t State_memsize(const void *ptr)
|
609
|
-
{
|
610
|
-
const JSON_Generator_State *state = ptr;
|
611
|
-
size_t size = sizeof(*state);
|
612
|
-
if (state->indent) size += state->indent_len + 1;
|
613
|
-
if (state->space) size += state->space_len + 1;
|
614
|
-
if (state->space_before) size += state->space_before_len + 1;
|
615
|
-
if (state->object_nl) size += state->object_nl_len + 1;
|
616
|
-
if (state->array_nl) size += state->array_nl_len + 1;
|
617
|
-
if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
|
618
|
-
if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
|
619
|
-
if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
|
620
|
-
return size;
|
621
|
-
}
|
622
|
-
|
623
|
-
#ifdef NEW_TYPEDDATA_WRAPPER
|
624
|
-
static const rb_data_type_t JSON_Generator_State_type = {
|
625
|
-
"JSON/Generator/State",
|
626
|
-
{NULL, State_free, State_memsize,},
|
627
|
-
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
|
628
|
-
0, 0,
|
629
|
-
RUBY_TYPED_FREE_IMMEDIATELY,
|
630
|
-
#endif
|
631
|
-
};
|
632
|
-
#endif
|
633
|
-
|
634
|
-
static VALUE cState_s_allocate(VALUE klass)
|
635
|
-
{
|
636
|
-
JSON_Generator_State *state;
|
637
|
-
return TypedData_Make_Struct(klass, JSON_Generator_State,
|
638
|
-
&JSON_Generator_State_type, state);
|
639
|
-
}
|
640
|
-
|
641
|
-
/*
|
642
|
-
* call-seq: configure(opts)
|
643
|
-
*
|
644
|
-
* Configure this State instance with the Hash _opts_, and return
|
645
|
-
* itself.
|
646
|
-
*/
|
647
|
-
static VALUE cState_configure(VALUE self, VALUE opts)
|
648
|
-
{
|
649
|
-
VALUE tmp;
|
650
|
-
GET_STATE(self);
|
651
|
-
tmp = rb_check_convert_type(opts, T_HASH, "Hash", "to_hash");
|
652
|
-
if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
|
653
|
-
opts = tmp;
|
654
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_indent));
|
655
|
-
if (RTEST(tmp)) {
|
656
|
-
unsigned long len;
|
657
|
-
Check_Type(tmp, T_STRING);
|
658
|
-
len = RSTRING_LEN(tmp);
|
659
|
-
state->indent = fstrndup(RSTRING_PTR(tmp), len + 1);
|
660
|
-
state->indent_len = len;
|
661
|
-
}
|
662
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_space));
|
663
|
-
if (RTEST(tmp)) {
|
664
|
-
unsigned long len;
|
665
|
-
Check_Type(tmp, T_STRING);
|
666
|
-
len = RSTRING_LEN(tmp);
|
667
|
-
state->space = fstrndup(RSTRING_PTR(tmp), len + 1);
|
668
|
-
state->space_len = len;
|
669
|
-
}
|
670
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
|
671
|
-
if (RTEST(tmp)) {
|
672
|
-
unsigned long len;
|
673
|
-
Check_Type(tmp, T_STRING);
|
674
|
-
len = RSTRING_LEN(tmp);
|
675
|
-
state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1);
|
676
|
-
state->space_before_len = len;
|
677
|
-
}
|
678
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
|
679
|
-
if (RTEST(tmp)) {
|
680
|
-
unsigned long len;
|
681
|
-
Check_Type(tmp, T_STRING);
|
682
|
-
len = RSTRING_LEN(tmp);
|
683
|
-
state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
|
684
|
-
state->array_nl_len = len;
|
685
|
-
}
|
686
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
|
687
|
-
if (RTEST(tmp)) {
|
688
|
-
unsigned long len;
|
689
|
-
Check_Type(tmp, T_STRING);
|
690
|
-
len = RSTRING_LEN(tmp);
|
691
|
-
state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
|
692
|
-
state->object_nl_len = len;
|
693
|
-
}
|
694
|
-
tmp = ID2SYM(i_max_nesting);
|
695
|
-
state->max_nesting = 100;
|
696
|
-
if (option_given_p(opts, tmp)) {
|
697
|
-
VALUE max_nesting = rb_hash_aref(opts, tmp);
|
698
|
-
if (RTEST(max_nesting)) {
|
699
|
-
Check_Type(max_nesting, T_FIXNUM);
|
700
|
-
state->max_nesting = FIX2LONG(max_nesting);
|
701
|
-
} else {
|
702
|
-
state->max_nesting = 0;
|
703
|
-
}
|
704
|
-
}
|
705
|
-
tmp = ID2SYM(i_depth);
|
706
|
-
state->depth = 0;
|
707
|
-
if (option_given_p(opts, tmp)) {
|
708
|
-
VALUE depth = rb_hash_aref(opts, tmp);
|
709
|
-
if (RTEST(depth)) {
|
710
|
-
Check_Type(depth, T_FIXNUM);
|
711
|
-
state->depth = FIX2LONG(depth);
|
712
|
-
} else {
|
713
|
-
state->depth = 0;
|
714
|
-
}
|
715
|
-
}
|
716
|
-
tmp = ID2SYM(i_buffer_initial_length);
|
717
|
-
if (option_given_p(opts, tmp)) {
|
718
|
-
VALUE buffer_initial_length = rb_hash_aref(opts, tmp);
|
719
|
-
if (RTEST(buffer_initial_length)) {
|
720
|
-
long initial_length;
|
721
|
-
Check_Type(buffer_initial_length, T_FIXNUM);
|
722
|
-
initial_length = FIX2LONG(buffer_initial_length);
|
723
|
-
if (initial_length > 0) state->buffer_initial_length = initial_length;
|
724
|
-
}
|
725
|
-
}
|
726
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
|
727
|
-
state->allow_nan = RTEST(tmp);
|
728
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
|
729
|
-
state->ascii_only = RTEST(tmp);
|
730
|
-
tmp = rb_hash_aref(opts, ID2SYM(i_escape_slash));
|
731
|
-
state->escape_slash = RTEST(tmp);
|
732
|
-
return self;
|
733
|
-
}
|
734
|
-
|
735
|
-
static void set_state_ivars(VALUE hash, VALUE state)
|
736
|
-
{
|
737
|
-
VALUE ivars = rb_obj_instance_variables(state);
|
738
|
-
int i = 0;
|
739
|
-
for (i = 0; i < RARRAY_LEN(ivars); i++) {
|
740
|
-
VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0);
|
741
|
-
long key_len = RSTRING_LEN(key);
|
742
|
-
VALUE value = rb_iv_get(state, StringValueCStr(key));
|
743
|
-
rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value);
|
744
|
-
}
|
745
|
-
}
|
746
|
-
|
747
|
-
/*
|
748
|
-
* call-seq: to_h
|
749
|
-
*
|
750
|
-
* Returns the configuration instance variables as a hash, that can be
|
751
|
-
* passed to the configure method.
|
752
|
-
*/
|
753
|
-
static VALUE cState_to_h(VALUE self)
|
754
|
-
{
|
755
|
-
VALUE result = rb_hash_new();
|
756
|
-
GET_STATE(self);
|
757
|
-
set_state_ivars(result, self);
|
758
|
-
rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
|
759
|
-
rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
|
760
|
-
rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
|
761
|
-
rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
|
762
|
-
rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
|
763
|
-
rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
|
764
|
-
rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
|
765
|
-
rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
|
766
|
-
rb_hash_aset(result, ID2SYM(i_escape_slash), state->escape_slash ? Qtrue : Qfalse);
|
767
|
-
rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
|
768
|
-
rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
|
769
|
-
return result;
|
770
|
-
}
|
771
|
-
|
772
|
-
/*
|
773
|
-
* call-seq: [](name)
|
774
|
-
*
|
775
|
-
* Returns the value returned by method +name+.
|
776
|
-
*/
|
777
|
-
static VALUE cState_aref(VALUE self, VALUE name)
|
778
|
-
{
|
779
|
-
name = rb_funcall(name, i_to_s, 0);
|
780
|
-
if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
|
781
|
-
return rb_funcall(self, i_send, 1, name);
|
782
|
-
} else {
|
783
|
-
return rb_attr_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
|
784
|
-
}
|
785
|
-
}
|
786
|
-
|
787
|
-
/*
|
788
|
-
* call-seq: []=(name, value)
|
789
|
-
*
|
790
|
-
* Sets the attribute name to value.
|
791
|
-
*/
|
792
|
-
static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
|
793
|
-
{
|
794
|
-
VALUE name_writer;
|
795
|
-
|
796
|
-
name = rb_funcall(name, i_to_s, 0);
|
797
|
-
name_writer = rb_str_cat2(rb_str_dup(name), "=");
|
798
|
-
if (RTEST(rb_funcall(self, i_respond_to_p, 1, name_writer))) {
|
799
|
-
return rb_funcall(self, i_send, 2, name_writer, value);
|
800
|
-
} else {
|
801
|
-
rb_ivar_set(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)), value);
|
802
|
-
}
|
803
|
-
return Qnil;
|
804
|
-
}
|
805
|
-
|
806
|
-
struct hash_foreach_arg {
|
807
|
-
FBuffer *buffer;
|
808
|
-
JSON_Generator_State *state;
|
809
|
-
VALUE Vstate;
|
810
|
-
int iter;
|
811
|
-
};
|
812
|
-
|
813
|
-
static int
|
814
|
-
json_object_i(VALUE key, VALUE val, VALUE _arg)
|
815
|
-
{
|
816
|
-
struct hash_foreach_arg *arg = (struct hash_foreach_arg *)_arg;
|
817
|
-
FBuffer *buffer = arg->buffer;
|
818
|
-
JSON_Generator_State *state = arg->state;
|
819
|
-
VALUE Vstate = arg->Vstate;
|
820
|
-
|
821
|
-
char *object_nl = state->object_nl;
|
822
|
-
long object_nl_len = state->object_nl_len;
|
823
|
-
char *indent = state->indent;
|
824
|
-
long indent_len = state->indent_len;
|
825
|
-
char *delim = FBUFFER_PTR(state->object_delim);
|
826
|
-
long delim_len = FBUFFER_LEN(state->object_delim);
|
827
|
-
char *delim2 = FBUFFER_PTR(state->object_delim2);
|
828
|
-
long delim2_len = FBUFFER_LEN(state->object_delim2);
|
829
|
-
long depth = state->depth;
|
830
|
-
int j;
|
831
|
-
VALUE klass, key_to_s;
|
832
|
-
|
833
|
-
if (arg->iter > 0) fbuffer_append(buffer, delim, delim_len);
|
834
|
-
if (object_nl) {
|
835
|
-
fbuffer_append(buffer, object_nl, object_nl_len);
|
836
|
-
}
|
837
|
-
if (indent) {
|
838
|
-
for (j = 0; j < depth; j++) {
|
839
|
-
fbuffer_append(buffer, indent, indent_len);
|
840
|
-
}
|
841
|
-
}
|
842
|
-
|
843
|
-
klass = CLASS_OF(key);
|
844
|
-
if (klass == rb_cString) {
|
845
|
-
key_to_s = key;
|
846
|
-
} else if (klass == rb_cSymbol) {
|
847
|
-
key_to_s = rb_id2str(SYM2ID(key));
|
848
|
-
} else {
|
849
|
-
key_to_s = rb_funcall(key, i_to_s, 0);
|
850
|
-
}
|
851
|
-
Check_Type(key_to_s, T_STRING);
|
852
|
-
generate_json(buffer, Vstate, state, key_to_s);
|
853
|
-
fbuffer_append(buffer, delim2, delim2_len);
|
854
|
-
generate_json(buffer, Vstate, state, val);
|
855
|
-
|
856
|
-
arg->iter++;
|
857
|
-
return ST_CONTINUE;
|
858
|
-
}
|
859
|
-
|
860
|
-
static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
861
|
-
{
|
862
|
-
char *object_nl = state->object_nl;
|
863
|
-
long object_nl_len = state->object_nl_len;
|
864
|
-
char *indent = state->indent;
|
865
|
-
long indent_len = state->indent_len;
|
866
|
-
long max_nesting = state->max_nesting;
|
867
|
-
long depth = ++state->depth;
|
868
|
-
int j;
|
869
|
-
struct hash_foreach_arg arg;
|
870
|
-
|
871
|
-
if (max_nesting != 0 && depth > max_nesting) {
|
872
|
-
fbuffer_free(buffer);
|
873
|
-
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
|
874
|
-
}
|
875
|
-
fbuffer_append_char(buffer, '{');
|
876
|
-
|
877
|
-
arg.buffer = buffer;
|
878
|
-
arg.state = state;
|
879
|
-
arg.Vstate = Vstate;
|
880
|
-
arg.iter = 0;
|
881
|
-
rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
|
882
|
-
|
883
|
-
depth = --state->depth;
|
884
|
-
if (object_nl) {
|
885
|
-
fbuffer_append(buffer, object_nl, object_nl_len);
|
886
|
-
if (indent) {
|
887
|
-
for (j = 0; j < depth; j++) {
|
888
|
-
fbuffer_append(buffer, indent, indent_len);
|
889
|
-
}
|
890
|
-
}
|
891
|
-
}
|
892
|
-
fbuffer_append_char(buffer, '}');
|
893
|
-
}
|
894
|
-
|
895
|
-
static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
896
|
-
{
|
897
|
-
char *array_nl = state->array_nl;
|
898
|
-
long array_nl_len = state->array_nl_len;
|
899
|
-
char *indent = state->indent;
|
900
|
-
long indent_len = state->indent_len;
|
901
|
-
long max_nesting = state->max_nesting;
|
902
|
-
char *delim = FBUFFER_PTR(state->array_delim);
|
903
|
-
long delim_len = FBUFFER_LEN(state->array_delim);
|
904
|
-
long depth = ++state->depth;
|
905
|
-
int i, j;
|
906
|
-
if (max_nesting != 0 && depth > max_nesting) {
|
907
|
-
fbuffer_free(buffer);
|
908
|
-
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
|
909
|
-
}
|
910
|
-
fbuffer_append_char(buffer, '[');
|
911
|
-
if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
|
912
|
-
for(i = 0; i < RARRAY_LEN(obj); i++) {
|
913
|
-
if (i > 0) fbuffer_append(buffer, delim, delim_len);
|
914
|
-
if (indent) {
|
915
|
-
for (j = 0; j < depth; j++) {
|
916
|
-
fbuffer_append(buffer, indent, indent_len);
|
917
|
-
}
|
918
|
-
}
|
919
|
-
generate_json(buffer, Vstate, state, rb_ary_entry(obj, i));
|
920
|
-
}
|
921
|
-
state->depth = --depth;
|
922
|
-
if (array_nl) {
|
923
|
-
fbuffer_append(buffer, array_nl, array_nl_len);
|
924
|
-
if (indent) {
|
925
|
-
for (j = 0; j < depth; j++) {
|
926
|
-
fbuffer_append(buffer, indent, indent_len);
|
927
|
-
}
|
928
|
-
}
|
929
|
-
}
|
930
|
-
fbuffer_append_char(buffer, ']');
|
931
|
-
}
|
932
|
-
|
933
|
-
#ifdef HAVE_RUBY_ENCODING_H
|
934
|
-
static int enc_utf8_compatible_p(rb_encoding *enc)
|
935
|
-
{
|
936
|
-
if (enc == rb_usascii_encoding()) return 1;
|
937
|
-
if (enc == rb_utf8_encoding()) return 1;
|
938
|
-
return 0;
|
939
|
-
}
|
940
|
-
#endif
|
941
|
-
|
942
|
-
static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
943
|
-
{
|
944
|
-
fbuffer_append_char(buffer, '"');
|
945
|
-
#ifdef HAVE_RUBY_ENCODING_H
|
946
|
-
if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
|
947
|
-
obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil);
|
948
|
-
}
|
949
|
-
#endif
|
950
|
-
if (state->ascii_only) {
|
951
|
-
convert_UTF8_to_JSON_ASCII(buffer, obj, state->escape_slash);
|
952
|
-
} else {
|
953
|
-
convert_UTF8_to_JSON(buffer, obj, state->escape_slash);
|
954
|
-
}
|
955
|
-
fbuffer_append_char(buffer, '"');
|
956
|
-
}
|
957
|
-
|
958
|
-
static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
959
|
-
{
|
960
|
-
fbuffer_append(buffer, "null", 4);
|
961
|
-
}
|
962
|
-
|
963
|
-
static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
964
|
-
{
|
965
|
-
fbuffer_append(buffer, "false", 5);
|
966
|
-
}
|
967
|
-
|
968
|
-
static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
969
|
-
{
|
970
|
-
fbuffer_append(buffer, "true", 4);
|
971
|
-
}
|
972
|
-
|
973
|
-
static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
974
|
-
{
|
975
|
-
fbuffer_append_long(buffer, FIX2LONG(obj));
|
976
|
-
}
|
977
|
-
|
978
|
-
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
979
|
-
{
|
980
|
-
VALUE tmp = rb_funcall(obj, i_to_s, 0);
|
981
|
-
fbuffer_append_str(buffer, tmp);
|
982
|
-
}
|
983
|
-
|
984
|
-
#ifdef RUBY_INTEGER_UNIFICATION
|
985
|
-
static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
986
|
-
{
|
987
|
-
if (FIXNUM_P(obj))
|
988
|
-
generate_json_fixnum(buffer, Vstate, state, obj);
|
989
|
-
else
|
990
|
-
generate_json_bignum(buffer, Vstate, state, obj);
|
991
|
-
}
|
992
|
-
#endif
|
993
|
-
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
994
|
-
{
|
995
|
-
double value = RFLOAT_VALUE(obj);
|
996
|
-
char allow_nan = state->allow_nan;
|
997
|
-
VALUE tmp = rb_funcall(obj, i_to_s, 0);
|
998
|
-
if (!allow_nan) {
|
999
|
-
if (isinf(value)) {
|
1000
|
-
fbuffer_free(buffer);
|
1001
|
-
rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
|
1002
|
-
} else if (isnan(value)) {
|
1003
|
-
fbuffer_free(buffer);
|
1004
|
-
rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
|
1005
|
-
}
|
1006
|
-
}
|
1007
|
-
fbuffer_append_str(buffer, tmp);
|
1008
|
-
}
|
1009
|
-
|
1010
|
-
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
1011
|
-
{
|
1012
|
-
VALUE tmp;
|
1013
|
-
VALUE klass = CLASS_OF(obj);
|
1014
|
-
if (klass == rb_cHash) {
|
1015
|
-
generate_json_object(buffer, Vstate, state, obj);
|
1016
|
-
} else if (klass == rb_cArray) {
|
1017
|
-
generate_json_array(buffer, Vstate, state, obj);
|
1018
|
-
} else if (klass == rb_cString) {
|
1019
|
-
generate_json_string(buffer, Vstate, state, obj);
|
1020
|
-
} else if (obj == Qnil) {
|
1021
|
-
generate_json_null(buffer, Vstate, state, obj);
|
1022
|
-
} else if (obj == Qfalse) {
|
1023
|
-
generate_json_false(buffer, Vstate, state, obj);
|
1024
|
-
} else if (obj == Qtrue) {
|
1025
|
-
generate_json_true(buffer, Vstate, state, obj);
|
1026
|
-
} else if (FIXNUM_P(obj)) {
|
1027
|
-
generate_json_fixnum(buffer, Vstate, state, obj);
|
1028
|
-
} else if (RB_TYPE_P(obj, T_BIGNUM)) {
|
1029
|
-
generate_json_bignum(buffer, Vstate, state, obj);
|
1030
|
-
} else if (klass == rb_cFloat) {
|
1031
|
-
generate_json_float(buffer, Vstate, state, obj);
|
1032
|
-
} else if (rb_respond_to(obj, i_to_json)) {
|
1033
|
-
tmp = rb_funcall(obj, i_to_json, 1, Vstate);
|
1034
|
-
Check_Type(tmp, T_STRING);
|
1035
|
-
fbuffer_append_str(buffer, tmp);
|
1036
|
-
} else {
|
1037
|
-
tmp = rb_funcall(obj, i_to_s, 0);
|
1038
|
-
Check_Type(tmp, T_STRING);
|
1039
|
-
generate_json_string(buffer, Vstate, state, tmp);
|
1040
|
-
}
|
1041
|
-
}
|
1042
|
-
|
1043
|
-
static FBuffer *cState_prepare_buffer(VALUE self)
|
1044
|
-
{
|
1045
|
-
FBuffer *buffer;
|
1046
|
-
GET_STATE(self);
|
1047
|
-
buffer = fbuffer_alloc(state->buffer_initial_length);
|
1048
|
-
|
1049
|
-
if (state->object_delim) {
|
1050
|
-
fbuffer_clear(state->object_delim);
|
1051
|
-
} else {
|
1052
|
-
state->object_delim = fbuffer_alloc(16);
|
1053
|
-
}
|
1054
|
-
fbuffer_append_char(state->object_delim, ',');
|
1055
|
-
if (state->object_delim2) {
|
1056
|
-
fbuffer_clear(state->object_delim2);
|
1057
|
-
} else {
|
1058
|
-
state->object_delim2 = fbuffer_alloc(16);
|
1059
|
-
}
|
1060
|
-
if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
|
1061
|
-
fbuffer_append_char(state->object_delim2, ':');
|
1062
|
-
if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
|
1063
|
-
|
1064
|
-
if (state->array_delim) {
|
1065
|
-
fbuffer_clear(state->array_delim);
|
1066
|
-
} else {
|
1067
|
-
state->array_delim = fbuffer_alloc(16);
|
1068
|
-
}
|
1069
|
-
fbuffer_append_char(state->array_delim, ',');
|
1070
|
-
if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
|
1071
|
-
return buffer;
|
1072
|
-
}
|
1073
|
-
|
1074
|
-
static VALUE cState_partial_generate(VALUE self, VALUE obj)
|
1075
|
-
{
|
1076
|
-
FBuffer *buffer = cState_prepare_buffer(self);
|
1077
|
-
GET_STATE(self);
|
1078
|
-
generate_json(buffer, self, state, obj);
|
1079
|
-
return fbuffer_to_s(buffer);
|
1080
|
-
}
|
1081
|
-
|
1082
|
-
/*
|
1083
|
-
* call-seq: generate(obj)
|
1084
|
-
*
|
1085
|
-
* Generates a valid JSON document from object +obj+ and returns the
|
1086
|
-
* result. If no valid JSON document can be created this method raises a
|
1087
|
-
* GeneratorError exception.
|
1088
|
-
*/
|
1089
|
-
static VALUE cState_generate(VALUE self, VALUE obj)
|
1090
|
-
{
|
1091
|
-
VALUE result = cState_partial_generate(self, obj);
|
1092
|
-
GET_STATE(self);
|
1093
|
-
(void)state;
|
1094
|
-
return result;
|
1095
|
-
}
|
1096
|
-
|
1097
|
-
/*
|
1098
|
-
* call-seq: new(opts = {})
|
1099
|
-
*
|
1100
|
-
* Instantiates a new State object, configured by _opts_.
|
1101
|
-
*
|
1102
|
-
* _opts_ can have the following keys:
|
1103
|
-
*
|
1104
|
-
* * *indent*: a string used to indent levels (default: ''),
|
1105
|
-
* * *space*: a string that is put after, a : or , delimiter (default: ''),
|
1106
|
-
* * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
1107
|
-
* * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
1108
|
-
* * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
1109
|
-
* * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
1110
|
-
* generated, otherwise an exception is thrown, if these values are
|
1111
|
-
* encountered. This options defaults to false.
|
1112
|
-
* * *ascii_only*: true if only ASCII characters should be generated. This
|
1113
|
-
* option defaults to false.
|
1114
|
-
* * *buffer_initial_length*: sets the initial length of the generator's
|
1115
|
-
* internal buffer.
|
1116
|
-
*/
|
1117
|
-
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
|
1118
|
-
{
|
1119
|
-
VALUE opts;
|
1120
|
-
GET_STATE(self);
|
1121
|
-
state->max_nesting = 100;
|
1122
|
-
state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
|
1123
|
-
rb_scan_args(argc, argv, "01", &opts);
|
1124
|
-
if (!NIL_P(opts)) cState_configure(self, opts);
|
1125
|
-
return self;
|
1126
|
-
}
|
1127
|
-
|
1128
|
-
/*
|
1129
|
-
* call-seq: initialize_copy(orig)
|
1130
|
-
*
|
1131
|
-
* Initializes this object from orig if it can be duplicated/cloned and returns
|
1132
|
-
* it.
|
1133
|
-
*/
|
1134
|
-
static VALUE cState_init_copy(VALUE obj, VALUE orig)
|
1135
|
-
{
|
1136
|
-
JSON_Generator_State *objState, *origState;
|
1137
|
-
|
1138
|
-
if (obj == orig) return obj;
|
1139
|
-
GET_STATE_TO(obj, objState);
|
1140
|
-
GET_STATE_TO(orig, origState);
|
1141
|
-
if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
|
1142
|
-
|
1143
|
-
MEMCPY(objState, origState, JSON_Generator_State, 1);
|
1144
|
-
objState->indent = fstrndup(origState->indent, origState->indent_len);
|
1145
|
-
objState->space = fstrndup(origState->space, origState->space_len);
|
1146
|
-
objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
|
1147
|
-
objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
|
1148
|
-
objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
|
1149
|
-
if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
|
1150
|
-
if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
|
1151
|
-
if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
|
1152
|
-
return obj;
|
1153
|
-
}
|
1154
|
-
|
1155
|
-
/*
|
1156
|
-
* call-seq: from_state(opts)
|
1157
|
-
*
|
1158
|
-
* Creates a State object from _opts_, which ought to be Hash to create a
|
1159
|
-
* new State instance configured by _opts_, something else to create an
|
1160
|
-
* unconfigured instance. If _opts_ is a State object, it is just returned.
|
1161
|
-
*/
|
1162
|
-
static VALUE cState_from_state_s(VALUE self, VALUE opts)
|
1163
|
-
{
|
1164
|
-
if (rb_obj_is_kind_of(opts, self)) {
|
1165
|
-
return opts;
|
1166
|
-
} else if (rb_obj_is_kind_of(opts, rb_cHash)) {
|
1167
|
-
return rb_funcall(self, i_new, 1, opts);
|
1168
|
-
} else {
|
1169
|
-
VALUE prototype = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
|
1170
|
-
return rb_funcall(prototype, i_dup, 0);
|
1171
|
-
}
|
1172
|
-
}
|
1173
|
-
|
1174
|
-
/*
|
1175
|
-
* call-seq: indent()
|
1176
|
-
*
|
1177
|
-
* Returns the string that is used to indent levels in the JSON text.
|
1178
|
-
*/
|
1179
|
-
static VALUE cState_indent(VALUE self)
|
1180
|
-
{
|
1181
|
-
GET_STATE(self);
|
1182
|
-
return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
|
1183
|
-
}
|
1184
|
-
|
1185
|
-
/*
|
1186
|
-
* call-seq: indent=(indent)
|
1187
|
-
*
|
1188
|
-
* Sets the string that is used to indent levels in the JSON text.
|
1189
|
-
*/
|
1190
|
-
static VALUE cState_indent_set(VALUE self, VALUE indent)
|
1191
|
-
{
|
1192
|
-
unsigned long len;
|
1193
|
-
GET_STATE(self);
|
1194
|
-
Check_Type(indent, T_STRING);
|
1195
|
-
len = RSTRING_LEN(indent);
|
1196
|
-
if (len == 0) {
|
1197
|
-
if (state->indent) {
|
1198
|
-
ruby_xfree(state->indent);
|
1199
|
-
state->indent = NULL;
|
1200
|
-
state->indent_len = 0;
|
1201
|
-
}
|
1202
|
-
} else {
|
1203
|
-
if (state->indent) ruby_xfree(state->indent);
|
1204
|
-
state->indent = fstrndup(RSTRING_PTR(indent), len);
|
1205
|
-
state->indent_len = len;
|
1206
|
-
}
|
1207
|
-
return Qnil;
|
1208
|
-
}
|
1209
|
-
|
1210
|
-
/*
|
1211
|
-
* call-seq: space()
|
1212
|
-
*
|
1213
|
-
* Returns the string that is used to insert a space between the tokens in a JSON
|
1214
|
-
* string.
|
1215
|
-
*/
|
1216
|
-
static VALUE cState_space(VALUE self)
|
1217
|
-
{
|
1218
|
-
GET_STATE(self);
|
1219
|
-
return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
|
1220
|
-
}
|
1221
|
-
|
1222
|
-
/*
|
1223
|
-
* call-seq: space=(space)
|
1224
|
-
*
|
1225
|
-
* Sets _space_ to the string that is used to insert a space between the tokens in a JSON
|
1226
|
-
* string.
|
1227
|
-
*/
|
1228
|
-
static VALUE cState_space_set(VALUE self, VALUE space)
|
1229
|
-
{
|
1230
|
-
unsigned long len;
|
1231
|
-
GET_STATE(self);
|
1232
|
-
Check_Type(space, T_STRING);
|
1233
|
-
len = RSTRING_LEN(space);
|
1234
|
-
if (len == 0) {
|
1235
|
-
if (state->space) {
|
1236
|
-
ruby_xfree(state->space);
|
1237
|
-
state->space = NULL;
|
1238
|
-
state->space_len = 0;
|
1239
|
-
}
|
1240
|
-
} else {
|
1241
|
-
if (state->space) ruby_xfree(state->space);
|
1242
|
-
state->space = fstrndup(RSTRING_PTR(space), len);
|
1243
|
-
state->space_len = len;
|
1244
|
-
}
|
1245
|
-
return Qnil;
|
1246
|
-
}
|
1247
|
-
|
1248
|
-
/*
|
1249
|
-
* call-seq: space_before()
|
1250
|
-
*
|
1251
|
-
* Returns the string that is used to insert a space before the ':' in JSON objects.
|
1252
|
-
*/
|
1253
|
-
static VALUE cState_space_before(VALUE self)
|
1254
|
-
{
|
1255
|
-
GET_STATE(self);
|
1256
|
-
return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
|
1257
|
-
}
|
1258
|
-
|
1259
|
-
/*
|
1260
|
-
* call-seq: space_before=(space_before)
|
1261
|
-
*
|
1262
|
-
* Sets the string that is used to insert a space before the ':' in JSON objects.
|
1263
|
-
*/
|
1264
|
-
static VALUE cState_space_before_set(VALUE self, VALUE space_before)
|
1265
|
-
{
|
1266
|
-
unsigned long len;
|
1267
|
-
GET_STATE(self);
|
1268
|
-
Check_Type(space_before, T_STRING);
|
1269
|
-
len = RSTRING_LEN(space_before);
|
1270
|
-
if (len == 0) {
|
1271
|
-
if (state->space_before) {
|
1272
|
-
ruby_xfree(state->space_before);
|
1273
|
-
state->space_before = NULL;
|
1274
|
-
state->space_before_len = 0;
|
1275
|
-
}
|
1276
|
-
} else {
|
1277
|
-
if (state->space_before) ruby_xfree(state->space_before);
|
1278
|
-
state->space_before = fstrndup(RSTRING_PTR(space_before), len);
|
1279
|
-
state->space_before_len = len;
|
1280
|
-
}
|
1281
|
-
return Qnil;
|
1282
|
-
}
|
1283
|
-
|
1284
|
-
/*
|
1285
|
-
* call-seq: object_nl()
|
1286
|
-
*
|
1287
|
-
* This string is put at the end of a line that holds a JSON object (or
|
1288
|
-
* Hash).
|
1289
|
-
*/
|
1290
|
-
static VALUE cState_object_nl(VALUE self)
|
1291
|
-
{
|
1292
|
-
GET_STATE(self);
|
1293
|
-
return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
|
1294
|
-
}
|
1295
|
-
|
1296
|
-
/*
|
1297
|
-
* call-seq: object_nl=(object_nl)
|
1298
|
-
*
|
1299
|
-
* This string is put at the end of a line that holds a JSON object (or
|
1300
|
-
* Hash).
|
1301
|
-
*/
|
1302
|
-
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
|
1303
|
-
{
|
1304
|
-
unsigned long len;
|
1305
|
-
GET_STATE(self);
|
1306
|
-
Check_Type(object_nl, T_STRING);
|
1307
|
-
len = RSTRING_LEN(object_nl);
|
1308
|
-
if (len == 0) {
|
1309
|
-
if (state->object_nl) {
|
1310
|
-
ruby_xfree(state->object_nl);
|
1311
|
-
state->object_nl = NULL;
|
1312
|
-
}
|
1313
|
-
} else {
|
1314
|
-
if (state->object_nl) ruby_xfree(state->object_nl);
|
1315
|
-
state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
|
1316
|
-
state->object_nl_len = len;
|
1317
|
-
}
|
1318
|
-
return Qnil;
|
1319
|
-
}
|
1320
|
-
|
1321
|
-
/*
|
1322
|
-
* call-seq: array_nl()
|
1323
|
-
*
|
1324
|
-
* This string is put at the end of a line that holds a JSON array.
|
1325
|
-
*/
|
1326
|
-
static VALUE cState_array_nl(VALUE self)
|
1327
|
-
{
|
1328
|
-
GET_STATE(self);
|
1329
|
-
return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
|
1330
|
-
}
|
1331
|
-
|
1332
|
-
/*
|
1333
|
-
* call-seq: array_nl=(array_nl)
|
1334
|
-
*
|
1335
|
-
* This string is put at the end of a line that holds a JSON array.
|
1336
|
-
*/
|
1337
|
-
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
|
1338
|
-
{
|
1339
|
-
unsigned long len;
|
1340
|
-
GET_STATE(self);
|
1341
|
-
Check_Type(array_nl, T_STRING);
|
1342
|
-
len = RSTRING_LEN(array_nl);
|
1343
|
-
if (len == 0) {
|
1344
|
-
if (state->array_nl) {
|
1345
|
-
ruby_xfree(state->array_nl);
|
1346
|
-
state->array_nl = NULL;
|
1347
|
-
}
|
1348
|
-
} else {
|
1349
|
-
if (state->array_nl) ruby_xfree(state->array_nl);
|
1350
|
-
state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
|
1351
|
-
state->array_nl_len = len;
|
1352
|
-
}
|
1353
|
-
return Qnil;
|
1354
|
-
}
|
1355
|
-
|
1356
|
-
|
1357
|
-
/*
|
1358
|
-
* call-seq: check_circular?
|
1359
|
-
*
|
1360
|
-
* Returns true, if circular data structures should be checked,
|
1361
|
-
* otherwise returns false.
|
1362
|
-
*/
|
1363
|
-
static VALUE cState_check_circular_p(VALUE self)
|
1364
|
-
{
|
1365
|
-
GET_STATE(self);
|
1366
|
-
return state->max_nesting ? Qtrue : Qfalse;
|
1367
|
-
}
|
1368
|
-
|
1369
|
-
/*
|
1370
|
-
* call-seq: max_nesting
|
1371
|
-
*
|
1372
|
-
* This integer returns the maximum level of data structure nesting in
|
1373
|
-
* the generated JSON, max_nesting = 0 if no maximum is checked.
|
1374
|
-
*/
|
1375
|
-
static VALUE cState_max_nesting(VALUE self)
|
1376
|
-
{
|
1377
|
-
GET_STATE(self);
|
1378
|
-
return LONG2FIX(state->max_nesting);
|
1379
|
-
}
|
1380
|
-
|
1381
|
-
/*
|
1382
|
-
* call-seq: max_nesting=(depth)
|
1383
|
-
*
|
1384
|
-
* This sets the maximum level of data structure nesting in the generated JSON
|
1385
|
-
* to the integer depth, max_nesting = 0 if no maximum should be checked.
|
1386
|
-
*/
|
1387
|
-
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
|
1388
|
-
{
|
1389
|
-
GET_STATE(self);
|
1390
|
-
Check_Type(depth, T_FIXNUM);
|
1391
|
-
return state->max_nesting = FIX2LONG(depth);
|
1392
|
-
}
|
1393
|
-
|
1394
|
-
/*
|
1395
|
-
* call-seq: escape_slash
|
1396
|
-
*
|
1397
|
-
* If this boolean is true, the forward slashes will be escaped in
|
1398
|
-
* the json output.
|
1399
|
-
*/
|
1400
|
-
static VALUE cState_escape_slash(VALUE self)
|
1401
|
-
{
|
1402
|
-
GET_STATE(self);
|
1403
|
-
return state->escape_slash ? Qtrue : Qfalse;
|
1404
|
-
}
|
1405
|
-
|
1406
|
-
/*
|
1407
|
-
* call-seq: escape_slash=(depth)
|
1408
|
-
*
|
1409
|
-
* This sets whether or not the forward slashes will be escaped in
|
1410
|
-
* the json output.
|
1411
|
-
*/
|
1412
|
-
static VALUE cState_escape_slash_set(VALUE self, VALUE enable)
|
1413
|
-
{
|
1414
|
-
GET_STATE(self);
|
1415
|
-
state->escape_slash = RTEST(enable);
|
1416
|
-
return Qnil;
|
1417
|
-
}
|
1418
|
-
|
1419
|
-
/*
|
1420
|
-
* call-seq: allow_nan?
|
1421
|
-
*
|
1422
|
-
* Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise
|
1423
|
-
* returns false.
|
1424
|
-
*/
|
1425
|
-
static VALUE cState_allow_nan_p(VALUE self)
|
1426
|
-
{
|
1427
|
-
GET_STATE(self);
|
1428
|
-
return state->allow_nan ? Qtrue : Qfalse;
|
1429
|
-
}
|
1430
|
-
|
1431
|
-
/*
|
1432
|
-
* call-seq: ascii_only?
|
1433
|
-
*
|
1434
|
-
* Returns true, if only ASCII characters should be generated. Otherwise
|
1435
|
-
* returns false.
|
1436
|
-
*/
|
1437
|
-
static VALUE cState_ascii_only_p(VALUE self)
|
1438
|
-
{
|
1439
|
-
GET_STATE(self);
|
1440
|
-
return state->ascii_only ? Qtrue : Qfalse;
|
1441
|
-
}
|
1442
|
-
|
1443
|
-
/*
|
1444
|
-
* call-seq: depth
|
1445
|
-
*
|
1446
|
-
* This integer returns the current depth of data structure nesting.
|
1447
|
-
*/
|
1448
|
-
static VALUE cState_depth(VALUE self)
|
1449
|
-
{
|
1450
|
-
GET_STATE(self);
|
1451
|
-
return LONG2FIX(state->depth);
|
1452
|
-
}
|
1453
|
-
|
1454
|
-
/*
|
1455
|
-
* call-seq: depth=(depth)
|
1456
|
-
*
|
1457
|
-
* This sets the maximum level of data structure nesting in the generated JSON
|
1458
|
-
* to the integer depth, max_nesting = 0 if no maximum should be checked.
|
1459
|
-
*/
|
1460
|
-
static VALUE cState_depth_set(VALUE self, VALUE depth)
|
1461
|
-
{
|
1462
|
-
GET_STATE(self);
|
1463
|
-
Check_Type(depth, T_FIXNUM);
|
1464
|
-
state->depth = FIX2LONG(depth);
|
1465
|
-
return Qnil;
|
1466
|
-
}
|
1467
|
-
|
1468
|
-
/*
|
1469
|
-
* call-seq: buffer_initial_length
|
1470
|
-
*
|
1471
|
-
* This integer returns the current initial length of the buffer.
|
1472
|
-
*/
|
1473
|
-
static VALUE cState_buffer_initial_length(VALUE self)
|
1474
|
-
{
|
1475
|
-
GET_STATE(self);
|
1476
|
-
return LONG2FIX(state->buffer_initial_length);
|
1477
|
-
}
|
1478
|
-
|
1479
|
-
/*
|
1480
|
-
* call-seq: buffer_initial_length=(length)
|
1481
|
-
*
|
1482
|
-
* This sets the initial length of the buffer to +length+, if +length+ > 0,
|
1483
|
-
* otherwise its value isn't changed.
|
1484
|
-
*/
|
1485
|
-
static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
|
1486
|
-
{
|
1487
|
-
long initial_length;
|
1488
|
-
GET_STATE(self);
|
1489
|
-
Check_Type(buffer_initial_length, T_FIXNUM);
|
1490
|
-
initial_length = FIX2LONG(buffer_initial_length);
|
1491
|
-
if (initial_length > 0) {
|
1492
|
-
state->buffer_initial_length = initial_length;
|
1493
|
-
}
|
1494
|
-
return Qnil;
|
1495
|
-
}
|
1496
|
-
|
1497
|
-
/*
|
1498
|
-
*
|
1499
|
-
*/
|
1500
|
-
void Init_generator(void)
|
1501
|
-
{
|
1502
|
-
#undef rb_intern
|
1503
|
-
rb_require("json/common");
|
1504
|
-
|
1505
|
-
mJSON = rb_define_module("JSON");
|
1506
|
-
mExt = rb_define_module_under(mJSON, "Ext");
|
1507
|
-
mGenerator = rb_define_module_under(mExt, "Generator");
|
1508
|
-
|
1509
|
-
eGeneratorError = rb_path2class("JSON::GeneratorError");
|
1510
|
-
eNestingError = rb_path2class("JSON::NestingError");
|
1511
|
-
rb_gc_register_mark_object(eGeneratorError);
|
1512
|
-
rb_gc_register_mark_object(eNestingError);
|
1513
|
-
|
1514
|
-
cState = rb_define_class_under(mGenerator, "State", rb_cObject);
|
1515
|
-
rb_define_alloc_func(cState, cState_s_allocate);
|
1516
|
-
rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
|
1517
|
-
rb_define_method(cState, "initialize", cState_initialize, -1);
|
1518
|
-
rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
|
1519
|
-
rb_define_method(cState, "indent", cState_indent, 0);
|
1520
|
-
rb_define_method(cState, "indent=", cState_indent_set, 1);
|
1521
|
-
rb_define_method(cState, "space", cState_space, 0);
|
1522
|
-
rb_define_method(cState, "space=", cState_space_set, 1);
|
1523
|
-
rb_define_method(cState, "space_before", cState_space_before, 0);
|
1524
|
-
rb_define_method(cState, "space_before=", cState_space_before_set, 1);
|
1525
|
-
rb_define_method(cState, "object_nl", cState_object_nl, 0);
|
1526
|
-
rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
|
1527
|
-
rb_define_method(cState, "array_nl", cState_array_nl, 0);
|
1528
|
-
rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
|
1529
|
-
rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
|
1530
|
-
rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
|
1531
|
-
rb_define_method(cState, "escape_slash", cState_escape_slash, 0);
|
1532
|
-
rb_define_method(cState, "escape_slash?", cState_escape_slash, 0);
|
1533
|
-
rb_define_method(cState, "escape_slash=", cState_escape_slash_set, 1);
|
1534
|
-
rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
|
1535
|
-
rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
|
1536
|
-
rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
|
1537
|
-
rb_define_method(cState, "depth", cState_depth, 0);
|
1538
|
-
rb_define_method(cState, "depth=", cState_depth_set, 1);
|
1539
|
-
rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
|
1540
|
-
rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
|
1541
|
-
rb_define_method(cState, "configure", cState_configure, 1);
|
1542
|
-
rb_define_alias(cState, "merge", "configure");
|
1543
|
-
rb_define_method(cState, "to_h", cState_to_h, 0);
|
1544
|
-
rb_define_alias(cState, "to_hash", "to_h");
|
1545
|
-
rb_define_method(cState, "[]", cState_aref, 1);
|
1546
|
-
rb_define_method(cState, "[]=", cState_aset, 2);
|
1547
|
-
rb_define_method(cState, "generate", cState_generate, 1);
|
1548
|
-
|
1549
|
-
mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
|
1550
|
-
mObject = rb_define_module_under(mGeneratorMethods, "Object");
|
1551
|
-
rb_define_method(mObject, "to_json", mObject_to_json, -1);
|
1552
|
-
mHash = rb_define_module_under(mGeneratorMethods, "Hash");
|
1553
|
-
rb_define_method(mHash, "to_json", mHash_to_json, -1);
|
1554
|
-
mArray = rb_define_module_under(mGeneratorMethods, "Array");
|
1555
|
-
rb_define_method(mArray, "to_json", mArray_to_json, -1);
|
1556
|
-
#ifdef RUBY_INTEGER_UNIFICATION
|
1557
|
-
mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
|
1558
|
-
rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
|
1559
|
-
#else
|
1560
|
-
mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
|
1561
|
-
rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
|
1562
|
-
mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
|
1563
|
-
rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
|
1564
|
-
#endif
|
1565
|
-
mFloat = rb_define_module_under(mGeneratorMethods, "Float");
|
1566
|
-
rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
|
1567
|
-
mString = rb_define_module_under(mGeneratorMethods, "String");
|
1568
|
-
rb_define_singleton_method(mString, "included", mString_included_s, 1);
|
1569
|
-
rb_define_method(mString, "to_json", mString_to_json, -1);
|
1570
|
-
rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
|
1571
|
-
rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
|
1572
|
-
mString_Extend = rb_define_module_under(mString, "Extend");
|
1573
|
-
rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
|
1574
|
-
mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
|
1575
|
-
rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
|
1576
|
-
mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
|
1577
|
-
rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
|
1578
|
-
mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
|
1579
|
-
rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
|
1580
|
-
|
1581
|
-
i_to_s = rb_intern("to_s");
|
1582
|
-
i_to_json = rb_intern("to_json");
|
1583
|
-
i_new = rb_intern("new");
|
1584
|
-
i_indent = rb_intern("indent");
|
1585
|
-
i_space = rb_intern("space");
|
1586
|
-
i_space_before = rb_intern("space_before");
|
1587
|
-
i_object_nl = rb_intern("object_nl");
|
1588
|
-
i_array_nl = rb_intern("array_nl");
|
1589
|
-
i_max_nesting = rb_intern("max_nesting");
|
1590
|
-
i_escape_slash = rb_intern("escape_slash");
|
1591
|
-
i_allow_nan = rb_intern("allow_nan");
|
1592
|
-
i_ascii_only = rb_intern("ascii_only");
|
1593
|
-
i_depth = rb_intern("depth");
|
1594
|
-
i_buffer_initial_length = rb_intern("buffer_initial_length");
|
1595
|
-
i_pack = rb_intern("pack");
|
1596
|
-
i_unpack = rb_intern("unpack");
|
1597
|
-
i_create_id = rb_intern("create_id");
|
1598
|
-
i_extend = rb_intern("extend");
|
1599
|
-
i_key_p = rb_intern("key?");
|
1600
|
-
i_aref = rb_intern("[]");
|
1601
|
-
i_send = rb_intern("__send__");
|
1602
|
-
i_respond_to_p = rb_intern("respond_to?");
|
1603
|
-
i_match = rb_intern("match");
|
1604
|
-
i_keys = rb_intern("keys");
|
1605
|
-
i_dup = rb_intern("dup");
|
1606
|
-
#ifdef HAVE_RUBY_ENCODING_H
|
1607
|
-
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
|
1608
|
-
i_encoding = rb_intern("encoding");
|
1609
|
-
i_encode = rb_intern("encode");
|
1610
|
-
#endif
|
1611
|
-
i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
|
1612
|
-
}
|