class JSON::Ext::Generator::State

Public Class Methods

from_state(opts) click to toggle source

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);
    }
}
new(*args) click to toggle 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 = {}) click to toggle source

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.

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

Public Instance Methods

[](name) click to toggle source

Returns the value returned by method name.

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

Sets the attribute name to value.

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

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

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

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;
}
array_nl() click to toggle source

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 ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
}
array_nl=(array_nl) click to toggle source

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)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(array_nl, T_STRING);
    len = RSTRING_LEN(array_nl);
    if (len == 0) {
        if (state->array_nl) {
            ruby_xfree(state->array_nl);
            state->array_nl = NULL;
        }
    } else {
        if (state->array_nl) ruby_xfree(state->array_nl);
        state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
        state->array_nl_len = len;
    }
    return Qnil;
}
ascii_only=(enable) click to toggle source

This sets whether only ASCII characters should be generated.

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

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;
}
buffer_initial_length click to toggle source

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);
}
buffer_initial_length=(length) click to toggle source

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)
{
    long initial_length;
    GET_STATE(self);
    Check_Type(buffer_initial_length, T_FIXNUM);
    initial_length = FIX2LONG(buffer_initial_length);
    if (initial_length > 0) {
        state->buffer_initial_length = initial_length;
    }
    return Qnil;
}
check_circular? click to toggle source

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;
}
configure(opts) click to toggle source

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

# File 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

  opts.each do |key, value|
    case key
    when :indent
      self.indent = value || ''
    when :space
      self.space = value || ''
    when :space_before
      self.space_before = value || ''
    when :array_nl
      self.array_nl = value || ''
    when :object_nl
      self.object_nl = value || ''
    when :max_nesting
      self.max_nesting = value || 0
    when :depth
      self.depth = value
    when :buffer_initial_length
      self.buffer_initial_length = value
    when :allow_nan
      self.allow_nan = value
    when :ascii_only
      self.ascii_only = value
    when :script_safe, :escape_slash
      self.script_safe = value
    when :strict
      self.strict = value
    end
  end

  self
end
Also aliased as: merge
depth click to toggle source

This integer returns the current depth of data structure nesting.

static VALUE cState_depth(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->depth);
}
depth=(depth) click to toggle source

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_depth_set(VALUE self, VALUE depth)
{
    GET_STATE(self);
    Check_Type(depth, T_FIXNUM);
    state->depth = FIX2LONG(depth);
    return Qnil;
}
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) click to toggle source

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(VALUE self, VALUE obj)
{
    VALUE result = cState_partial_generate(self, obj, generate_json);
    GET_STATE(self);
    (void)state;
    return result;
}
indent() click to toggle source

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 ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
}
indent=(indent) click to toggle source

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

static VALUE cState_indent_set(VALUE self, VALUE indent)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(indent, T_STRING);
    len = RSTRING_LEN(indent);
    if (len == 0) {
        if (state->indent) {
            ruby_xfree(state->indent);
            state->indent = NULL;
            state->indent_len = 0;
        }
    } else {
        if (state->indent) ruby_xfree(state->indent);
        state->indent = fstrndup(RSTRING_PTR(indent), len);
        state->indent_len = len;
    }
    return Qnil;
}
initialize_copy(orig) click to toggle source

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);
    objState->indent = fstrndup(origState->indent, origState->indent_len);
    objState->space = fstrndup(origState->space, origState->space_len);
    objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
    objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
    objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
    return obj;
}
max_nesting click to toggle source

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);
}
max_nesting=(depth) click to toggle source

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)
{
    GET_STATE(self);
    Check_Type(depth, T_FIXNUM);
    state->max_nesting = FIX2LONG(depth);
    return Qnil;
}
merge
Alias for: configure
object_nl() click to toggle source

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 ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
}
object_nl=(object_nl) click to toggle source

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)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(object_nl, T_STRING);
    len = RSTRING_LEN(object_nl);
    if (len == 0) {
        if (state->object_nl) {
            ruby_xfree(state->object_nl);
            state->object_nl = NULL;
        }
    } else {
        if (state->object_nl) ruby_xfree(state->object_nl);
        state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
        state->object_nl_len = len;
    }
    return Qnil;
}
script_safe click to toggle source

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?, escape_slash
script_safe=(enable) click to toggle source

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)
{
    GET_STATE(self);
    state->script_safe = RTEST(enable);
    return Qnil;
}
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() click to toggle source

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 ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
}
space=(space) click to toggle source

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)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(space, T_STRING);
    len = RSTRING_LEN(space);
    if (len == 0) {
        if (state->space) {
            ruby_xfree(state->space);
            state->space = NULL;
            state->space_len = 0;
        }
    } else {
        if (state->space) ruby_xfree(state->space);
        state->space = fstrndup(RSTRING_PTR(space), len);
        state->space_len = len;
    }
    return Qnil;
}
space_before() click to toggle source

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 ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
}
space_before=(space_before) click to toggle source

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)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(space_before, T_STRING);
    len = RSTRING_LEN(space_before);
    if (len == 0) {
        if (state->space_before) {
            ruby_xfree(state->space_before);
            state->space_before = NULL;
            state->space_before_len = 0;
        }
    } else {
        if (state->space_before) ruby_xfree(state->space_before);
        state->space_before = fstrndup(RSTRING_PTR(space_before), len);
        state->space_before_len = len;
    }
    return Qnil;
}
strict click to toggle source

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?
strict=(enable) click to toggle source

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)
{
    GET_STATE(self);
    state->strict = RTEST(enable);
    return Qnil;
}
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 click to toggle source

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

# File lib/json/ext/generator/state.rb, line 84
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
Also aliased as: to_hash
to_h
Alias for: to_h