class JSON::Ext::Generator::State

Public Class Methods

static VALUE cState_m_generate_no_fallback(VALUE klass, VALUE obj, VALUE opts, VALUE io)
{
    return cState_m_do_generate(klass, obj, opts, io, generate_json_no_fallback);
}
static VALUE cState_set_default_sort_keys_proc(VALUE self, VALUE proc)
{
    if (!rb_obj_is_proc(proc)) {
        rb_raise(rb_eTypeError, "sort_key_proc must be a Proc");
    }
    if (default_sort_keys_proc) {
        rb_raise(rb_eArgError, "sort_key_proc can only be set once");
    }
    return default_sort_keys_proc = proc;
}

Creates a State object from opts, which ought to be Hash to create a new State instance configured by opts, something else to create an unconfigured instance. If opts is a State object, it is just returned.

static VALUE cState_from_state_s(VALUE self, VALUE opts)
{
    if (rb_obj_is_kind_of(opts, self)) {
        return opts;
    } else if (rb_obj_is_kind_of(opts, rb_cHash)) {
        return rb_funcall(self, i_new, 1, opts);
    } else {
        return rb_class_new_instance(0, NULL, cState);
    }
}
static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
{
    return cState_m_do_generate(klass, obj, opts, io, generate_json);
}
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
    rb_warn("The json gem extension was loaded with the stdlib ruby code. You should upgrade rubygems with `gem update --system`");
    return self;
}
static VALUE cState_set_rfc8785_number_formatter_proc(VALUE self, VALUE proc)
{
    if (!rb_obj_is_proc(proc)) {
        rb_raise(rb_eTypeError, "rfc8785_number_formatter_proc must be a Proc");
    }
    if (rfc8785_number_formatter_proc) {
        rb_raise(rb_eArgError, "rfc8785_number_formatter_proc can only be set once");
    }
    return rfc8785_number_formatter_proc = proc;
}
static VALUE cState_set_rfc8785_sort_keys_proc(VALUE self, VALUE proc)
{
    if (!rb_obj_is_proc(proc)) {
        rb_raise(rb_eTypeError, "rfc8785_sort_keys_proc must be a Proc");
    }
    if (rfc8785_sort_keys_proc) {
        rb_raise(rb_eArgError, "rfc8785_sort_keys_proc can only be set once");
    }
    return rfc8785_sort_keys_proc = proc;
}

Public Instance Methods

This sets whether or not to serialize NaN, Infinity, and -Infinity

static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->allow_nan = RTEST(enable);
    return Qnil;
}

Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.

static VALUE cState_allow_nan_p(VALUE self)
{
    GET_STATE(self);
    return state->allow_nan ? Qtrue : Qfalse;
}

This string is put at the end of a line that holds a JSON array.

static VALUE cState_array_nl(VALUE self)
{
    GET_STATE(self);
    return state->array_nl ? state->array_nl : rb_str_freeze(rb_utf8_str_new("", 0));
}

This string is put at the end of a line that holds a JSON array.

static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->array_nl, string_config(array_nl));
    return Qnil;
}

Returns the callback used to convert unsupported objects in strict mode.

static VALUE cState_as_json(VALUE self)
{
    GET_STATE(self);
    return state->as_json;
}

Sets the Proc used to convert unsupported objects in strict mode.

static VALUE cState_as_json_set(VALUE self, VALUE as_json)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->as_json, as_json_config(as_json));
    return Qnil;
}

This sets whether only ASCII characters should be generated.

static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->ascii_only = RTEST(enable);
    return Qnil;
}

Returns true, if only ASCII characters should be generated. Otherwise returns false.

static VALUE cState_ascii_only_p(VALUE self)
{
    GET_STATE(self);
    return state->ascii_only ? Qtrue : Qfalse;
}

This integer returns the current initial length of the buffer.

static VALUE cState_buffer_initial_length(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->buffer_initial_length);
}

This sets the initial length of the buffer to length, if length > 0, otherwise its value isn’t changed.

static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
{
    rb_check_frozen(self);
    GET_STATE(self);
    buffer_initial_length_set(state, buffer_initial_length);
    return Qnil;
}

Returns true, if circular data structures should be checked, otherwise returns false.

static VALUE cState_check_circular_p(VALUE self)
{
    GET_STATE(self);
    return state->max_nesting ? Qtrue : Qfalse;
}

This integer returns the current depth of data structure nesting.

static VALUE cState_depth(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->depth);
}

Sets the current depth of data structure nesting to the integer depth.

static VALUE cState_depth_set(VALUE self, VALUE depth)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->depth = depth_config(depth);
    return Qnil;
}

Generates a valid JSON document from object obj and returns the result. If no valid JSON document can be created this method raises a GeneratorError exception.

static VALUE cState_generate(int argc, VALUE *argv, VALUE self)
{
    rb_check_arity(argc, 1, 2);
    VALUE obj = argv[0];
    VALUE io = argc > 1 ? argv[1] : Qnil;
    return cState_partial_generate(self, obj, generate_json, io);
}

Returns the string that is used to indent levels in the JSON text.

static VALUE cState_indent(VALUE self)
{
    GET_STATE(self);
    return state->indent ? state->indent : rb_str_freeze(rb_utf8_str_new("", 0));
}

Sets the string that is used to indent levels in the JSON text.

static VALUE cState_indent_set(VALUE self, VALUE indent)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->indent, string_config(indent));
    return Qnil;
}

Initializes this object from orig if it can be duplicated/cloned and returns it.

static VALUE cState_init_copy(VALUE obj, VALUE orig)
{
    JSON_Generator_State *objState, *origState;

    if (obj == orig) return obj;
    GET_STATE_TO(obj, objState);
    GET_STATE_TO(orig, origState);
    if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");

    MEMCPY(objState, origState, JSON_Generator_State, 1);

    RB_OBJ_WRITTEN(obj, Qundef, objState->indent);
    RB_OBJ_WRITTEN(obj, Qundef, objState->space);
    RB_OBJ_WRITTEN(obj, Qundef, objState->space_before);
    RB_OBJ_WRITTEN(obj, Qundef, objState->object_nl);
    RB_OBJ_WRITTEN(obj, Qundef, objState->array_nl);
    RB_OBJ_WRITTEN(obj, Qundef, objState->as_json);
    RB_OBJ_WRITTEN(obj, Qundef, objState->sort_keys);

    return obj;
}

This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.

static VALUE cState_max_nesting(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->max_nesting);
}

This sets the maximum level of data structure nesting in the generated JSON to the integer depth, max_nesting = 0 if no maximum should be checked.

static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->max_nesting = long_config(depth);
    return Qnil;
}

This string is put at the end of a line that holds a JSON object (or Hash).

static VALUE cState_object_nl(VALUE self)
{
    GET_STATE(self);
    return state->object_nl ? state->object_nl : rb_str_freeze(rb_utf8_str_new("", 0));
}

This string is put at the end of a line that holds a JSON object (or Hash).

static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->object_nl, string_config(object_nl));
    return Qnil;
}

Sets whether RFC8785 (canonicalization) is enabled.

static VALUE cState_rfc8785_set(VALUE self, VALUE value)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->rfc8785 = RTEST(value);
    return state->rfc8785 ? Qtrue : Qfalse;
}

Get whether RFC8785 (canonicalization) is enabled.

static VALUE cState_rfc8785_p(VALUE self)
{
    GET_STATE(self);
    return state->rfc8785 ? Qtrue : Qfalse;
}

If this boolean is true, the forward slashes will be escaped in the json output.

static VALUE cState_script_safe(VALUE self)
{
    GET_STATE(self);
    return state->script_safe ? Qtrue : Qfalse;
}
Also aliased as: script_safe?

This sets whether or not the forward slashes will be escaped in the json output.

static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->script_safe = RTEST(enable);
    return Qnil;
}

If this boolean is true, the forward slashes will be escaped in the json output.

Alias for: script_safe

Get the value of sort_keys.

static VALUE cState_sort_keys(VALUE self)
{
    GET_STATE(self);
    return state->sort_keys;
}

value is a boolean or a proc. If the value is the boolean true, object keys will be sorted lexicographically in ascending order.

If the value is a proc, it receives the entire Hash and must return a Hash with its pairs in the desired order, allowing for arbitrary sorting.

static VALUE cState_sort_keys_set(VALUE self, VALUE value)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->sort_keys, normalize_sort_keys(value));
    return state->sort_keys;
}

Returns the string that is used to insert a space between the tokens in a JSON string.

static VALUE cState_space(VALUE self)
{
    GET_STATE(self);
    return state->space ? state->space : rb_str_freeze(rb_utf8_str_new("", 0));
}

Sets space to the string that is used to insert a space between the tokens in a JSON string.

static VALUE cState_space_set(VALUE self, VALUE space)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->space, string_config(space));
    return Qnil;
}

Returns the string that is used to insert a space before the ‘:’ in JSON objects.

static VALUE cState_space_before(VALUE self)
{
    GET_STATE(self);
    return state->space_before ? state->space_before : rb_str_freeze(rb_utf8_str_new("", 0));
}

Sets the string that is used to insert a space before the ‘:’ in JSON objects.

static VALUE cState_space_before_set(VALUE self, VALUE space_before)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->space_before, string_config(space_before));
    return Qnil;
}

If this boolean is false, types unsupported by the JSON format will be serialized as strings. If this boolean is true, types unsupported by the JSON format will raise a JSON::GeneratorError.

static VALUE cState_strict(VALUE self)
{
    GET_STATE(self);
    return state->strict ? Qtrue : Qfalse;
}
Also aliased as: strict?

This sets whether or not to serialize types unsupported by the JSON format as strings. If this boolean is false, types unsupported by the JSON format will be serialized as strings. If this boolean is true, types unsupported by the JSON format will raise a JSON::GeneratorError.

static VALUE cState_strict_set(VALUE self, VALUE enable)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->strict = RTEST(enable);
    return Qnil;
}

If this boolean is false, types unsupported by the JSON format will be serialized as strings. If this boolean is true, types unsupported by the JSON format will raise a JSON::GeneratorError.

Alias for: strict