# class JSON::Ext::Generator::State

### Public Class Methods

from_state(opts)
Source

    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);
        }
    }

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.

generate (p1, p2, p3)
Source

    static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
    {
        JSON_Generator_State state = {0};
        state_init(&state);
        configure_state(&state, opts);

        char stack_buffer[FBUFFER_STACK_SIZE];
        FBuffer buffer = {
            .io = RTEST(io) ? io : Qfalse,
        };
        fbuffer_stack_init(&buffer, state.buffer_initial_length, stack_buffer, FBUFFER_STACK_SIZE);

        struct generate_json_data data = {
            .buffer = &buffer,
            .vstate = Qfalse,
            .state = &state,
            .obj = obj,
            .func = generate_json,
        };
        rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);

        return fbuffer_finalize(&buffer);
    }

new (*args)
Source

    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;
    }

new(opts = {})
Source

    # File ext/json/lib/json/ext/generator/state.rb, line 25
    def initialize(opts = nil)
      if opts && !opts.empty?
        configure(opts)
      end
    end

Instantiates a new `State` object, configured by opts.

opts can have the following keys:

- indent: a string used to indent levels (default: ”),

- space: a string that is put after, a : or , delimiter (default: ”),

- space_before: a string that is put before a : pair delimiter (default: ”),

- object_nl: a string that is put at the end of a `JSON` object (default: ”),

- array_nl: a string that is put at the end of a `JSON` array (default: ”),

- allow_nan: true if NaN, Infinity, and -Infinity should be generated, otherwise an exception is thrown, if these values are encountered. This options defaults to false.

- ascii_only: true if only ASCII characters should be generated. This option defaults to false.

- buffer_initial_length: sets the initial length of the generator’s internal buffer.

Also aliased as: initialize

### Public Instance Methods

[](name)
Source

    # File ext/json/lib/json/ext/generator/state.rb, line 94
    def [](name)
      if respond_to?(name)
        __send__(name)
      else
        instance_variable_get("@#{name}") if
          instance_variables.include?("@#{name}".to_sym) # avoid warning
      end
    end

Returns the value returned by method `name`.

[]=(name, value)
Source

    # File ext/json/lib/json/ext/generator/state.rb, line 106
    def []=(name, value)
      if respond_to?(name_writer = "#{name}=")
        __send__ name_writer, value
      else
        instance_variable_set "@#{name}", value
      end
    end

Sets the attribute name to value.

allow_nan=(enable)
Source

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

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

allow_nan?
Source

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

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

array_nl()
Source

    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.

array_nl=(array_nl)
Source

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

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

ascii_only=(enable)
Source

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

This sets whether only ASCII characters should be generated.

ascii_only?
Source

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

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

buffer_initial_length
Source

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

This integer returns the current initial length of the buffer.

buffer_initial_length=(length)
Source

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

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

check_circular?
Source

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

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

configure(opts)
Source

    # File ext/json/lib/json/ext/generator/state.rb, line 35
    def configure(opts)
      unless opts.is_a?(Hash)
        if opts.respond_to?(:to_hash)
          opts = opts.to_hash
        elsif opts.respond_to?(:to_h)
          opts = opts.to_h
        else
          raise TypeError, "can't convert #{opts.class} into Hash"
        end
      end
      _configure(opts)
    end

Configure this `State` instance with the `Hash` opts, and return itself.

Also aliased as: merge
depth
Source

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

This integer returns the current depth of data structure nesting.

depth=(depth)
Source

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

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.

script_safe

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

Alias for: script_safe
escape_slash=
Alias for: script_safe=
escape_slash? ()

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

Alias for: script_safe?
generate(obj) → String
generate(obj, anIO) → anIO
Source

    # File ext/json/lib/json/ext/generator/state.rb, line 57
    def generate(obj, io = nil)
      _generate(obj, io)
    end

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.

indent()
Source

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

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

indent=(indent)
Source

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

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

initialize_copy(orig)
Source

    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);
        objState->indent = origState->indent;
        objState->space = origState->space;
        objState->space_before = origState->space_before;
        objState->object_nl = origState->object_nl;
        objState->array_nl = origState->array_nl;
        return obj;
    }

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

max_nesting
Source

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

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

max_nesting=(depth)
Source

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

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.

merge
Alias for: configure
object_nl()
Source

    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`).

object_nl=(object_nl)
Source

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

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

script_safe
Source

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

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

Also aliased as: script_safe?, escape_slash
script_safe=(enable)
Source

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

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

Also aliased as: escape_slash=
script_safe

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

Also aliased as: escape_slash?
Alias for: script_safe
space()
Source

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

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

space=(space)
Source

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

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

space_before()
Source

    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));
    }

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

space_before=(space_before)
Source

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

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

strict
Source

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

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`.

Also aliased as: strict?
strict=(enable)
Source

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

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`.

strict

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
to_h
Source

    # File ext/json/lib/json/ext/generator/state.rb, line 65
    def to_h
      result = {
        indent: indent,
        space: space,
        space_before: space_before,
        object_nl: object_nl,
        array_nl: array_nl,
        allow_nan: allow_nan?,
        ascii_only: ascii_only?,
        max_nesting: max_nesting,
        script_safe: script_safe?,
        strict: strict?,
        depth: depth,
        buffer_initial_length: buffer_initial_length,
      }

      instance_variables.each do |iv|
        iv = iv.to_s[1..-1]
        result[iv.to_sym] = self[iv]
      end

      result
    end

Returns the configuration instance variables as a hash, that can be passed to the configure method.

Also aliased as: to_hash
to_h
Alias for: to_h

### Private Instance Methods

_configure (p1)
Source

    static VALUE cState_configure(VALUE self, VALUE opts)
    {
        GET_STATE(self);
        configure_state(state, opts);
        return self;
    }

_generate (p1, p2)
Source

    static VALUE cState_generate(VALUE self, VALUE obj, VALUE io)
    {
        VALUE result = cState_partial_generate(self, obj, generate_json, io);
        GET_STATE(self);
        (void)state;
        return result;
    }