Module: JSON::Ext::Generator::GeneratorMethods::Float

Defined in:
ext/json/ext/generator/generator.c

Instance Method Summary collapse

Instance Method Details

#to_json(*) ⇒ Object

Returns a JSON string representation for this Float number.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'ext/json/ext/generator/generator.c', line 282

static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
{
    JSON_Generator_State *state = NULL;
    VALUE Vstate, rest, tmp;
    double value = RFLOAT_VALUE(self);
    rb_scan_args(argc, argv, "01*", &Vstate, &rest);
    if (!NIL_P(Vstate)) Data_Get_Struct(Vstate, JSON_Generator_State, state);
    if (isinf(value)) {
        if (!state || state->allow_nan) {
            return rb_funcall(self, i_to_s, 0);
        } else {
            tmp = rb_funcall(self, i_to_s, 0);
            rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
        }
    } else if (isnan(value)) {
        if (!state || state->allow_nan) {
            return rb_funcall(self, i_to_s, 0);
        } else {
            tmp = rb_funcall(self, i_to_s, 0);
            rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
        }
    } else {
        return rb_funcall(self, i_to_s, 0);
    }
}