Class: JSON::Ext::Generator::State
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | ext/json/generator/generator.c, ext/json/lib/json/ext/generator/state.rb |
Class Method Summary
-
.from_state(opts)
Creates a
Stateobject from opts, which ought to be Hash to create a newStateinstance configured by opts, something else to create an unconfigured instance. - .generate(obj, opts, io)
- .new(*args) constructor
Instance Attribute Summary
- #allow_nan=(enable) rw
- #allow_nan? ⇒ Boolean rw
-
#array_nl
rw
This string is put at the end of a line that holds a
::JSONarray. -
#array_nl=(array_nl)
rw
This string is put at the end of a line that holds a
::JSONarray. -
#as_json
rw
This string is put at the end of a line that holds a
::JSONarray. -
#as_json=(as_json)
rw
This string is put at the end of a line that holds a
::JSONarray. -
#ascii_only=(enable)
rw
This sets whether only ASCII characters should be generated.
-
#ascii_only? ⇒ Boolean
rw
Returns true, if only ASCII characters should be generated.
-
#buffer_initial_length
rw
This integer returns the current initial length of the buffer.
-
#buffer_initial_length=(length)
rw
This sets the initial length of the buffer to
length, iflength> 0, otherwise its value isn’t changed. -
#check_circular? ⇒ Boolean
readonly
Returns true, if circular data structures should be checked, otherwise returns false.
-
#depth
rw
This integer returns the current depth of data structure nesting.
-
#depth=(depth)
rw
This sets the maximum level of data structure nesting in the generated
::JSONto the integer depth, max_nesting = 0 if no maximum should be checked. -
#escape_slash
rw
Alias for #script_safe.
-
#indent
rw
Returns the string that is used to indent levels in the
::JSONtext. -
#indent=(indent)
rw
Sets the string that is used to indent levels in the
::JSONtext. -
#max_nesting
rw
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)
rw
This sets the maximum level of data structure nesting in the generated
::JSONto the integer depth, max_nesting = 0 if no maximum should be checked. -
#object_nl
rw
This string is put at the end of a line that holds a
::JSONobject (or Hash). -
#object_nl=(object_nl)
rw
This string is put at the end of a line that holds a
::JSONobject (or Hash). -
#script_safe
(also: #script_safe?, #escape_slash)
rw
If this boolean is true, the forward slashes will be escaped in the json output.
-
#script_safe=(enable)
(also: #escape_slash=)
rw
This sets whether or not the forward slashes will be escaped in the json output.
-
#script_safe?
(also: #escape_slash?)
rw
Alias for #script_safe.
-
#space
rw
Returns the string that is used to insert a space between the tokens in a
::JSONstring. -
#space=(space)
rw
Sets space to the string that is used to insert a space between the tokens in a
::JSONstring. -
#space_before
rw
Returns the string that is used to insert a space before the ‘:’ in
::JSONobjects. -
#space_before=(space_before)
rw
Sets the string that is used to insert a space before the ‘:’ in
::JSONobjects. -
#strict
(also: #strict?)
rw
If this boolean is false, types unsupported by the
::JSONformat will be serialized as strings. -
#strict=(enable)
rw
This sets whether or not to serialize types unsupported by the
::JSONformat as strings. -
#strict?
rw
Alias for #strict.
- #allow_duplicate_key? ⇒ Boolean readonly private Internal use only
Instance Method Summary
-
#[](name)
Returns the value returned by method
name. -
#[]=(name, value)
Sets the attribute name to value.
-
#configure(opts)
(also: #merge)
Configure this
Stateinstance with the Hash opts, and return itself. -
#script_safe
rw
Alias for #script_safe?.
-
#generate(obj) ⇒ String
Generates a valid
::JSONdocument from objectobjand returns the result. -
#new(opts = {}) ⇒ State
constructor
Instantiates a new
Stateobject, configured by opts. -
#initialize_copy(orig)
Initializes this object from orig if it can be duplicated/cloned and returns it.
-
#merge(opts)
Alias for #configure.
-
#to_h ⇒ Hash
(also: #to_hash)
Returns the configuration instance variables as a hash, that can be passed to the configure method.
-
#to_hash
Alias for #to_h.
-
#_configure(opts)
private
avoid method redefinition warnings.
- #generate_new(*args) Internal use only
Constructor Details
.new(*args)
[ GitHub ]# File 'ext/json/generator/generator.c', line 1524
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 = {}) ⇒ State
Instantiates a new State object, configured by opts.
Argument opts, if given, contains a Hash of options for the generation. See Generating Options.
# File 'ext/json/lib/json/ext/generator/state.rb', line 13
def initialize(opts = nil) if opts && !opts.empty? configure(opts) end end
Class Method Details
.from_state(opts)
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.
# File 'ext/json/generator/generator.c', line 1562
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);
}
}
.generate(obj, opts, io)
[ GitHub ]# File 'ext/json/generator/generator.c', line 2008
static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
{
JSON_Generator_State state = {0};
state_init(&state);
configure_state(&state, Qfalse, 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,
};
return rb_ensure(generate_json_try, (VALUE)&data, generate_json_ensure, (VALUE)&data);
}
Instance Attribute Details
#allow_duplicate_key? ⇒ Boolean (readonly, private)
# File 'ext/json/generator/generator.c', line 1872
static VALUE cState_allow_duplicate_key_p(VALUE self)
{
GET_STATE(self);
switch (state->on_duplicate_key) {
case JSON_IGNORE:
return Qtrue;
case JSON_DEPRECATED:
return Qnil;
default:
return Qfalse;
}
}
#allow_nan=(enable) (rw)
[ GitHub ]# File 'ext/json/generator/generator.c', line 1841
static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->allow_nan = RTEST(enable);
return Qnil;
}
#allow_nan? ⇒ Boolean (rw)
[ GitHub ]
# File 'ext/json/generator/generator.c', line 1830
static VALUE cState_allow_nan_p(VALUE self)
{
GET_STATE(self);
return state->allow_nan ? Qtrue : Qfalse;
}
#array_nl (rw)
This string is put at the end of a line that holds a ::JSON array.
# File 'ext/json/generator/generator.c', line 1685
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));
}
#array_nl=(array_nl) (rw)
This string is put at the end of a line that holds a ::JSON array.
# File 'ext/json/generator/generator.c', line 1696
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;
}
#as_json (rw)
This string is put at the end of a line that holds a ::JSON array.
# File 'ext/json/generator/generator.c', line 1708
static VALUE cState_as_json(VALUE self)
{
GET_STATE(self);
return state->as_json;
}
#as_json=(as_json) (rw)
This string is put at the end of a line that holds a ::JSON array.
# File 'ext/json/generator/generator.c', line 1719
static VALUE cState_as_json_set(VALUE self, VALUE as_json)
{
GET_STATE(self);
RB_OBJ_WRITE(self, &state->as_json, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
return Qnil;
}
#ascii_only=(enable) (rw)
This sets whether only ASCII characters should be generated.
# File 'ext/json/generator/generator.c', line 1865
static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->ascii_only = RTEST(enable);
return Qnil;
}
#ascii_only? ⇒ Boolean (rw)
Returns true, if only ASCII characters should be generated. Otherwise returns false.
# File 'ext/json/generator/generator.c', line 1854
static VALUE cState_ascii_only_p(VALUE self)
{
GET_STATE(self);
return state->ascii_only ? Qtrue : Qfalse;
}
#buffer_initial_length (rw)
This integer returns the current initial length of the buffer.
# File 'ext/json/generator/generator.c', line 1914
static VALUE cState_buffer_initial_length(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->buffer_initial_length);
}
#buffer_initial_length=(length) (rw)
This sets the initial length of the buffer to length, if length > 0, otherwise its value isn’t changed.
# File 'ext/json/generator/generator.c', line 1935
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;
}
#check_circular? ⇒ Boolean (readonly)
Returns true, if circular data structures should be checked, otherwise returns false.
# File 'ext/json/generator/generator.c', line 1732
static VALUE cState_check_circular_p(VALUE self)
{
GET_STATE(self);
return state->max_nesting ? Qtrue : Qfalse;
}
#depth (rw)
This integer returns the current depth of data structure nesting.
# File 'ext/json/generator/generator.c', line 1890
static VALUE cState_depth(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->depth);
}
#depth=(depth) (rw)
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.
# File 'ext/json/generator/generator.c', line 1902
static VALUE cState_depth_set(VALUE self, VALUE depth)
{
GET_STATE(self);
state->depth = long_config(depth);
return Qnil;
}
#script_safe (rw)
#escape_slash
Alias for #script_safe.
#indent (rw)
Returns the string that is used to indent levels in the ::JSON text.
# File 'ext/json/generator/generator.c', line 1578
static VALUE cState_indent(VALUE self)
{
GET_STATE(self);
return state->indent ? state->indent : rb_str_freeze(rb_utf8_str_new("", 0));
}
#indent=(indent) (rw)
Sets the string that is used to indent levels in the ::JSON text.
# File 'ext/json/generator/generator.c', line 1600
static VALUE cState_indent_set(VALUE self, VALUE indent)
{
GET_STATE(self);
RB_OBJ_WRITE(self, &state->indent, string_config(indent));
return Qnil;
}
#max_nesting (rw)
This integer returns the maximum level of data structure nesting in the generated ::JSON, max_nesting = 0 if no maximum is checked.
# File 'ext/json/generator/generator.c', line 1744
static VALUE cState_max_nesting(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->max_nesting);
}
#max_nesting=(depth) (rw)
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.
# File 'ext/json/generator/generator.c', line 1761
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
GET_STATE(self);
state->max_nesting = long_config(depth);
return Qnil;
}
#object_nl (rw)
This string is put at the end of a line that holds a ::JSON object (or Hash).
# File 'ext/json/generator/generator.c', line 1661
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));
}
#object_nl=(object_nl) (rw)
This string is put at the end of a line that holds a ::JSON object (or Hash).
# File 'ext/json/generator/generator.c', line 1673
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;
}
#script_safe (rw) Also known as: #script_safe?, #escape_slash
If this boolean is true, the forward slashes will be escaped in the json output.
# File 'ext/json/generator/generator.c', line 1774
static VALUE cState_script_safe(VALUE self)
{
GET_STATE(self);
return state->script_safe ? Qtrue : Qfalse;
}
#script_safe=(enable) (rw) Also known as: #escape_slash=
This sets whether or not the forward slashes will be escaped in the json output.
# File 'ext/json/generator/generator.c', line 1786
static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->script_safe = RTEST(enable);
return Qnil;
}
#script_safe (rw)
#script_safe?
Also known as: #escape_slash?
Alias for #script_safe.
#space (rw)
Returns the string that is used to insert a space between the tokens in a ::JSON string.
# File 'ext/json/generator/generator.c', line 1613
static VALUE cState_space(VALUE self)
{
GET_STATE(self);
return state->space ? state->space : rb_str_freeze(rb_utf8_str_new("", 0));
}
#space=(space) (rw)
Sets space to the string that is used to insert a space between the tokens in a ::JSON string.
# File 'ext/json/generator/generator.c', line 1625
static VALUE cState_space_set(VALUE self, VALUE space)
{
GET_STATE(self);
RB_OBJ_WRITE(self, &state->space, string_config(space));
return Qnil;
}
#space_before (rw)
Returns the string that is used to insert a space before the ‘:’ in ::JSON objects.
# File 'ext/json/generator/generator.c', line 1637
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));
}
#space_before=(space_before) (rw)
Sets the string that is used to insert a space before the ‘:’ in ::JSON objects.
# File 'ext/json/generator/generator.c', line 1648
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;
}
#strict (rw) Also known as: #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.
# File 'ext/json/generator/generator.c', line 1801
static VALUE cState_strict(VALUE self)
{
GET_STATE(self);
return state->strict ? Qtrue : Qfalse;
}
#strict=(enable) (rw)
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.
# File 'ext/json/generator/generator.c', line 1817
static VALUE cState_strict_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->strict = RTEST(enable);
return Qnil;
}
#strict (rw)
#strict?
Alias for #strict.
Instance Method Details
#[](name)
Returns the value returned by method name.
# File 'ext/json/lib/json/ext/generator/state.rb', line 77
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)
Sets the attribute name to value.
# File 'ext/json/lib/json/ext/generator/state.rb', line 89
def []=(name, value) if respond_to?(name_writer = "#{name}=") __send__ name_writer, value else instance_variable_set "@#{name}", value end end
#_configure(opts) (private)
avoid method redefinition warnings
# File 'ext/json/generator/generator.c', line 2001
static VALUE cState_configure(VALUE self, VALUE opts)
{
GET_STATE(self);
configure_state(state, self, opts);
return self;
}
#configure(opts) Also known as: #merge
Configure this State instance with the Hash opts, and return itself.
# File 'ext/json/lib/json/ext/generator/state.rb', line 23
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
#script_safe (rw)
#script_safe?
Alias for #script_safe?.
#generate(obj) ⇒ String
#generate(obj, anIO) ⇒ anIO
anIO
Generates a valid ::JSON document from object obj and returns the result. If no valid ::JSON document can be created this method raises a ::JSON::GeneratorError exception.
# File 'ext/json/generator/generator.c', line 1486
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);
}
#generate_new(*args)
# File 'ext/json/generator/generator.c', line 1494
static VALUE cState_generate_new(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 1, 2);
VALUE obj = argv[0];
VALUE io = argc > 1 ? argv[1] : Qnil;
GET_STATE(self);
JSON_Generator_State new_state;
MEMCPY(&new_state, state, JSON_Generator_State, 1);
// FIXME: depth shouldn't be part of JSON_Generator_State, as that prevents it from being used concurrently.
new_state.depth = 0;
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 = &new_state,
.obj = obj,
.func = generate_json
};
return rb_ensure(generate_json_try, (VALUE)&data, generate_json_ensure, (VALUE)&data);
}
#initialize_copy(orig)
Initializes this object from orig if it can be duplicated/cloned and returns it.
# File 'ext/json/generator/generator.c', line 1536
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;
objState->as_json = origState->as_json;
return obj;
}
#merge(opts)
Alias for #configure.
# File 'ext/json/lib/json/ext/generator/state.rb', line 36
alias_method :merge, :configure
#to_h ⇒ Hash Also known as: #to_hash
Returns the configuration instance variables as a hash, that can be passed to the configure method.
# File 'ext/json/lib/json/ext/generator/state.rb', line 42
def to_h result = { indent: indent, space: space, space_before: space_before, object_nl: object_nl, array_nl: array_nl, as_json: as_json, 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, } allow_duplicate_key = allow_duplicate_key? unless allow_duplicate_key.nil? result[:allow_duplicate_key] = allow_duplicate_key end instance_variables.each do |iv| iv = iv.to_s[1..-1] result[iv.to_sym] = self[iv] end result end
#to_hash
Alias for #to_h.
# File 'ext/json/lib/json/ext/generator/state.rb', line 72
alias_method :to_hash, :to_h