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 #define RSTRING_PAIR(string) RSTRING_PTR(string), RSTRING_LEN(string)
00049
00050
00051
00052 typedef struct FBufferStruct {
00053 unsigned int initial_length;
00054 char *ptr;
00055 unsigned int len;
00056 unsigned int capa;
00057 } FBuffer;
00058
00059 #define FBUFFER_INITIAL_LENGTH 4096
00060
00061 #define FBUFFER_PTR(fb) (fb->ptr)
00062 #define FBUFFER_LEN(fb) (fb->len)
00063 #define FBUFFER_CAPA(fb) (fb->capa)
00064 #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
00065
00066 static char *fstrndup(const char *ptr, int len);
00067 static FBuffer *fbuffer_alloc();
00068 static FBuffer *fbuffer_alloc_with_length(unsigned initial_length);
00069 static void fbuffer_free(FBuffer *fb);
00070 static void fbuffer_free_only_buffer(FBuffer *fb);
00071 static void fbuffer_clear(FBuffer *fb);
00072 static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned int len);
00073 static void fbuffer_append_long(FBuffer *fb, long number);
00074 static void fbuffer_append_char(FBuffer *fb, char newchr);
00075 static FBuffer *fbuffer_dup(FBuffer *fb);
00076
00077
00078
00079 #define UNI_STRICT_CONVERSION 1
00080
00081 typedef unsigned long UTF32;
00082 typedef unsigned short UTF16;
00083 typedef unsigned char UTF8;
00084
00085 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
00086 #define UNI_MAX_BMP (UTF32)0x0000FFFF
00087 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
00088 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
00089 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
00090
00091 #define UNI_SUR_HIGH_START (UTF32)0xD800
00092 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
00093 #define UNI_SUR_LOW_START (UTF32)0xDC00
00094 #define UNI_SUR_LOW_END (UTF32)0xDFFF
00095
00096 static const int halfShift = 10;
00097
00098 static const UTF32 halfBase = 0x0010000UL;
00099 static const UTF32 halfMask = 0x3FFUL;
00100
00101 static unsigned char isLegalUTF8(const UTF8 *source, int length);
00102 static void unicode_escape(char *buf, UTF16 character);
00103 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
00104 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
00105 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
00106
00107
00108
00109 typedef struct JSON_Generator_StateStruct {
00110 char *indent;
00111 long indent_len;
00112 char *space;
00113 long space_len;
00114 char *space_before;
00115 long space_before_len;
00116 char *object_nl;
00117 long object_nl_len;
00118 char *array_nl;
00119 long array_nl_len;
00120 FBuffer *array_delim;
00121 FBuffer *object_delim;
00122 FBuffer *object_delim2;
00123 long max_nesting;
00124 char allow_nan;
00125 char ascii_only;
00126 } JSON_Generator_State;
00127
00128 #define GET_STATE(self) \
00129 JSON_Generator_State *state; \
00130 Data_Get_Struct(self, JSON_Generator_State, state)
00131
00132 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
00133 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
00134 static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self);
00135 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
00136 static VALUE mString_included_s(VALUE self, VALUE modul);
00137 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
00138 static VALUE mString_to_json_raw_object(VALUE self);
00139 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
00140 static VALUE mString_Extend_json_create(VALUE self, VALUE o);
00141 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
00142 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
00143 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
00144 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
00145 static void State_free(JSON_Generator_State *state);
00146 static JSON_Generator_State *State_allocate();
00147 static VALUE cState_s_allocate(VALUE klass);
00148 static VALUE cState_configure(VALUE self, VALUE opts);
00149 static VALUE cState_to_h(VALUE self);
00150 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj, long depth);
00151 static VALUE cState_partial_generate(VALUE self, VALUE obj, VALUE depth);
00152 static VALUE cState_generate(VALUE self, VALUE obj);
00153 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
00154 static VALUE cState_from_state_s(VALUE self, VALUE opts);
00155 static VALUE cState_indent(VALUE self);
00156 static VALUE cState_indent_set(VALUE self, VALUE indent);
00157 static VALUE cState_space(VALUE self);
00158 static VALUE cState_space_set(VALUE self, VALUE space);
00159 static VALUE cState_space_before(VALUE self);
00160 static VALUE cState_space_before_set(VALUE self, VALUE space_before);
00161 static VALUE cState_object_nl(VALUE self);
00162 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
00163 static VALUE cState_array_nl(VALUE self);
00164 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
00165 static VALUE cState_max_nesting(VALUE self);
00166 static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
00167 static VALUE cState_allow_nan_p(VALUE self);
00168 static VALUE cState_ascii_only_p(VALUE self);
00169
00170 #endif
00171