18 #ifdef HAVE_RUBY_ENCODING_H
19 #include "ruby/encoding.h"
20 #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
22 #define FORCE_UTF8(obj)
25 #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
28 #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
32 #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
36 #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
39 #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
42 #define RSTRING_PTR(string) RSTRING(string)->ptr
45 #define RSTRING_LEN(string) RSTRING(string)->len
48 /* We don't need to guard objects for rbx, so let's do nothing at all. */
50 #define RB_GC_GUARD(object)
53 /* fbuffer implementation */
55 typedef struct FBufferStruct {
56 unsigned long initial_length;
62 #define FBUFFER_INITIAL_LENGTH 4096
64 #define FBUFFER_PTR(fb) (fb->ptr)
65 #define FBUFFER_LEN(fb) (fb->len)
66 #define FBUFFER_CAPA(fb) (fb->capa)
67 #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
69 static char *fstrndup(const char *ptr, unsigned long len);
70 static FBuffer *fbuffer_alloc();
71 static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length);
72 static void fbuffer_free(FBuffer *fb);
73 static void fbuffer_clear(FBuffer *fb);
74 static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
75 static void fbuffer_append_long(FBuffer *fb, long number);
76 static void fbuffer_append_char(FBuffer *fb, char newchr);
77 static FBuffer *fbuffer_dup(FBuffer *fb);
78 static VALUE fbuffer_to_s(FBuffer *fb);
80 /* unicode defintions */
82 #define UNI_STRICT_CONVERSION 1
84 typedef unsigned long UTF32; /* at least 32 bits */
85 typedef unsigned short UTF16; /* at least 16 bits */
86 typedef unsigned char UTF8; /* typically 8 bits */
88 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
89 #define UNI_MAX_BMP (UTF32)0x0000FFFF
90 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
91 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
92 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
94 #define UNI_SUR_HIGH_START (UTF32)0xD800
95 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
96 #define UNI_SUR_LOW_START (UTF32)0xDC00
97 #define UNI_SUR_LOW_END (UTF32)0xDFFF
99 static const int halfShift = 10; /* used for shifting by 10 bits */
101 static const UTF32 halfBase = 0x0010000UL;
102 static const UTF32 halfMask = 0x3FFUL;
104 static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
105 static void unicode_escape(char *buf, UTF16 character);
106 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
107 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
108 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
110 /* ruby api and some helpers */
112 typedef struct JSON_Generator_StateStruct {
118 long space_before_len;
123 FBuffer *array_delim;
124 FBuffer *object_delim;
125 FBuffer *object_delim2;
131 } JSON_Generator_State;
133 #define GET_STATE(self) \
134 JSON_Generator_State *state; \
135 Data_Get_Struct(self, JSON_Generator_State, state)
137 #define GENERATE_JSON(type) \
140 JSON_Generator_State *state; \
142 rb_scan_args(argc, argv, "01", &Vstate); \
143 Vstate = cState_from_state_s(cState, Vstate); \
144 Data_Get_Struct(Vstate, JSON_Generator_State, state); \
145 buffer = cState_prepare_buffer(Vstate); \
146 generate_json_##type(buffer, Vstate, state, self); \
147 return fbuffer_to_s(buffer)
149 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
150 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
151 static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
152 static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
153 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
154 static VALUE mString_included_s(VALUE self, VALUE modul);
155 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
156 static VALUE mString_to_json_raw_object(VALUE self);
157 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
158 static VALUE mString_Extend_json_create(VALUE self, VALUE o);
159 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
160 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
161 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
162 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
163 static void State_free(JSON_Generator_State *state);
164 static JSON_Generator_State *State_allocate();
165 static VALUE cState_s_allocate(VALUE klass);
166 static VALUE cState_configure(VALUE self, VALUE opts);
167 static VALUE cState_to_h(VALUE self);
168 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
169 static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
170 static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
171 static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
172 static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
173 static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
174 static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
175 static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
176 static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
177 static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
178 static VALUE cState_partial_generate(VALUE self, VALUE obj);
179 static VALUE cState_generate(VALUE self, VALUE obj);
180 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
181 static VALUE cState_from_state_s(VALUE self, VALUE opts);
182 static VALUE cState_indent(VALUE self);
183 static VALUE cState_indent_set(VALUE self, VALUE indent);
184 static VALUE cState_space(VALUE self);
185 static VALUE cState_space_set(VALUE self, VALUE space);
186 static VALUE cState_space_before(VALUE self);
187 static VALUE cState_space_before_set(VALUE self, VALUE space_before);
188 static VALUE cState_object_nl(VALUE self);
189 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
190 static VALUE cState_array_nl(VALUE self);
191 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
192 static VALUE cState_max_nesting(VALUE self);
193 static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
194 static VALUE cState_allow_nan_p(VALUE self);
195 static VALUE cState_ascii_only_p(VALUE self);
196 static VALUE cState_depth(VALUE self);
197 static VALUE cState_depth_set(VALUE self, VALUE depth);
198 static FBuffer *cState_prepare_buffer(VALUE self);