00001 #ifndef _GENERATOR_H_
00002 #define _GENERATOR_H_
00003
00004 #include <string.h>
00005 #include <assert.h>
00006 #include <math.h>
00007
00008 #include "ruby.h"
00009
00010 #if HAVE_RUBY_RE_H
00011 #include "ruby/re.h"
00012 #endif
00013
00014 #if HAVE_RE_H
00015 #include "re.h"
00016 #endif
00017
00018 #ifdef HAVE_RUBY_ENCODING_H
00019 #include "ruby/encoding.h"
00020 #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
00021 #else
00022 #define FORCE_UTF8(obj)
00023 #endif
00024
00025 #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
00026
00027 #ifndef RHASH_SIZE
00028 #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
00029 #endif
00030
00031 #ifndef RFLOAT_VALUE
00032 #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
00033 #endif
00034
00035 #ifndef RARRAY_PTR
00036 #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
00037 #endif
00038 #ifndef RARRAY_LEN
00039 #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
00040 #endif
00041 #ifndef RSTRING_PTR
00042 #define RSTRING_PTR(string) RSTRING(string)->ptr
00043 #endif
00044 #ifndef RSTRING_LEN
00045 #define RSTRING_LEN(string) RSTRING(string)->len
00046 #endif
00047
00048
00049 #ifndef RB_GC_GUARD
00050 #define RB_GC_GUARD(object)
00051 #endif
00052
00053
00054
00055 typedef struct FBufferStruct {
00056 unsigned long initial_length;
00057 char *ptr;
00058 unsigned long len;
00059 unsigned long capa;
00060 } FBuffer;
00061
00062 #define FBUFFER_INITIAL_LENGTH 4096
00063
00064 #define FBUFFER_PTR(fb) (fb->ptr)
00065 #define FBUFFER_LEN(fb) (fb->len)
00066 #define FBUFFER_CAPA(fb) (fb->capa)
00067 #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
00068
00069 static char *fstrndup(const char *ptr, unsigned long len);
00070 static FBuffer *fbuffer_alloc();
00071 static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length);
00072 static void fbuffer_free(FBuffer *fb);
00073 static void fbuffer_clear(FBuffer *fb);
00074 static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
00075 static void fbuffer_append_long(FBuffer *fb, long number);
00076 static void fbuffer_append_char(FBuffer *fb, char newchr);
00077 static FBuffer *fbuffer_dup(FBuffer *fb);
00078 static VALUE fbuffer_to_s(FBuffer *fb);
00079
00080
00081
00082 #define UNI_STRICT_CONVERSION 1
00083
00084 typedef unsigned long UTF32;
00085 typedef unsigned short UTF16;
00086 typedef unsigned char UTF8;
00087
00088 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
00089 #define UNI_MAX_BMP (UTF32)0x0000FFFF
00090 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
00091 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
00092 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
00093
00094 #define UNI_SUR_HIGH_START (UTF32)0xD800
00095 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
00096 #define UNI_SUR_LOW_START (UTF32)0xDC00
00097 #define UNI_SUR_LOW_END (UTF32)0xDFFF
00098
00099 static const int halfShift = 10;
00100
00101 static const UTF32 halfBase = 0x0010000UL;
00102 static const UTF32 halfMask = 0x3FFUL;
00103
00104 static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
00105 static void unicode_escape(char *buf, UTF16 character);
00106 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
00107 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
00108 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
00109
00110
00111
00112 typedef struct JSON_Generator_StateStruct {
00113 char *indent;
00114 long indent_len;
00115 char *space;
00116 long space_len;
00117 char *space_before;
00118 long space_before_len;
00119 char *object_nl;
00120 long object_nl_len;
00121 char *array_nl;
00122 long array_nl_len;
00123 FBuffer *array_delim;
00124 FBuffer *object_delim;
00125 FBuffer *object_delim2;
00126 long max_nesting;
00127 char allow_nan;
00128 char ascii_only;
00129 char quirks_mode;
00130 long depth;
00131 } JSON_Generator_State;
00132
00133 #define GET_STATE(self) \
00134 JSON_Generator_State *state; \
00135 Data_Get_Struct(self, JSON_Generator_State, state)
00136
00137 #define GENERATE_JSON(type) \
00138 FBuffer *buffer; \
00139 VALUE Vstate; \
00140 JSON_Generator_State *state; \
00141 \
00142 rb_scan_args(argc, argv, "01", &Vstate); \
00143 Vstate = cState_from_state_s(cState, Vstate); \
00144 Data_Get_Struct(Vstate, JSON_Generator_State, state); \
00145 buffer = cState_prepare_buffer(Vstate); \
00146 generate_json_##type(buffer, Vstate, state, self); \
00147 return fbuffer_to_s(buffer)
00148
00149 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
00150 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
00151 static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
00152 static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
00153 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
00154 static VALUE mString_included_s(VALUE self, VALUE modul);
00155 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
00156 static VALUE mString_to_json_raw_object(VALUE self);
00157 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
00158 static VALUE mString_Extend_json_create(VALUE self, VALUE o);
00159 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
00160 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
00161 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
00162 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
00163 static void State_free(JSON_Generator_State *state);
00164 static JSON_Generator_State *State_allocate();
00165 static VALUE cState_s_allocate(VALUE klass);
00166 static VALUE cState_configure(VALUE self, VALUE opts);
00167 static VALUE cState_to_h(VALUE self);
00168 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00169 static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00170 static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00171 static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00172 static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00173 static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00174 static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00175 static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00176 static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00177 static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
00178 static VALUE cState_partial_generate(VALUE self, VALUE obj);
00179 static VALUE cState_generate(VALUE self, VALUE obj);
00180 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
00181 static VALUE cState_from_state_s(VALUE self, VALUE opts);
00182 static VALUE cState_indent(VALUE self);
00183 static VALUE cState_indent_set(VALUE self, VALUE indent);
00184 static VALUE cState_space(VALUE self);
00185 static VALUE cState_space_set(VALUE self, VALUE space);
00186 static VALUE cState_space_before(VALUE self);
00187 static VALUE cState_space_before_set(VALUE self, VALUE space_before);
00188 static VALUE cState_object_nl(VALUE self);
00189 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
00190 static VALUE cState_array_nl(VALUE self);
00191 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
00192 static VALUE cState_max_nesting(VALUE self);
00193 static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
00194 static VALUE cState_allow_nan_p(VALUE self);
00195 static VALUE cState_ascii_only_p(VALUE self);
00196 static VALUE cState_depth(VALUE self);
00197 static VALUE cState_depth_set(VALUE self, VALUE depth);
00198 static FBuffer *cState_prepare_buffer(VALUE self);
00199
00200 #endif
00201