diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 2a3db296..8cc0c270 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -7,7 +7,13 @@ static ID i_encoding, i_encode; #endif static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject, - mHash, mArray, mFixnum, mBignum, mFloat, mString, mString_Extend, + mHash, mArray, +#ifdef RUBY_INTEGER_UNIFICATION + mInteger, +#else + mFixnum, mBignum, +#endif + mFloat, mString, mString_Extend, mTrueClass, mFalseClass, mNilClass, eGeneratorError, eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE, i_SAFE_STATE_PROTOTYPE; @@ -342,6 +348,18 @@ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) { GENERATE_JSON(array); } +#ifdef RUBY_INTEGER_UNIFICATION +/* + * call-seq: to_json(*) + * + * Returns a JSON string representation for this Integer number. + */ +static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self) +{ + GENERATE_JSON(integer); +} + +#else /* * call-seq: to_json(*) * @@ -361,6 +379,7 @@ static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self) { GENERATE_JSON(bignum); } +#endif /* * call-seq: to_json(*) @@ -822,6 +841,15 @@ static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_S fbuffer_append_str(buffer, tmp); } +#ifdef RUBY_INTEGER_UNIFICATION +static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj) +{ + if (FIXNUM_P(obj)) + generate_json_fixnum(buffer, Vstate, state, obj); + else + generate_json_bignum(buffer, Vstate, state, obj); +} +#endif static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj) { double value = RFLOAT_VALUE(obj); @@ -855,9 +883,9 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s generate_json_false(buffer, Vstate, state, obj); } else if (obj == Qtrue) { generate_json_true(buffer, Vstate, state, obj); - } else if (klass == rb_cFixnum) { + } else if (FIXNUM_P(obj)) { generate_json_fixnum(buffer, Vstate, state, obj); - } else if (klass == rb_cBignum) { + } else if (RB_TYPE_P(obj, T_BIGNUM)) { generate_json_bignum(buffer, Vstate, state, obj); } else if (klass == rb_cFloat) { generate_json_float(buffer, Vstate, state, obj); @@ -1353,10 +1381,15 @@ void Init_generator(void) rb_define_method(mHash, "to_json", mHash_to_json, -1); mArray = rb_define_module_under(mGeneratorMethods, "Array"); rb_define_method(mArray, "to_json", mArray_to_json, -1); +#ifdef RUBY_INTEGER_UNIFICATION + mInteger = rb_define_module_under(mGeneratorMethods, "Integer"); + rb_define_method(mInteger, "to_json", mInteger_to_json, -1); +#else mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum"); rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1); mBignum = rb_define_module_under(mGeneratorMethods, "Bignum"); rb_define_method(mBignum, "to_json", mBignum_to_json, -1); +#endif mFloat = rb_define_module_under(mGeneratorMethods, "Float"); rb_define_method(mFloat, "to_json", mFloat_to_json, -1); mString = rb_define_module_under(mGeneratorMethods, "String"); diff --git a/ext/json/ext/generator/generator.h b/ext/json/ext/generator/generator.h index 7432f268..900b4d58 100644 --- a/ext/json/ext/generator/generator.h +++ b/ext/json/ext/generator/generator.h @@ -98,8 +98,12 @@ typedef struct JSON_Generator_StateStruct { static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self); static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self); +#ifdef RUBY_INTEGER_UNIFICATION +static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self); +#else static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self); static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self); +#endif static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self); static VALUE mString_included_s(VALUE self, VALUE modul); static VALUE mString_to_json(int argc, VALUE *argv, VALUE self); @@ -121,6 +125,9 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj); static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj); static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj); +#ifdef RUBY_INTEGER_UNIFICATION +static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj); +#endif static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj); static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj); static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);