Class: JSON::Ext::Generator::State
Relationships & Source Files | |
Inherits: | Object |
Defined in: | ext/json/generator/generator.c |
Class Method Summary
-
.from_state(opts)
Creates a
State
object from opts, which ought to be Hash to create a newState
instance configured by opts, something else to create an unconfigured instance. -
.new(opts = {})
constructor
Instantiates a new
State
object, configured by opts.
Instance Attribute Summary
- #allow_nan? ⇒ Boolean readonly
-
#array_nl
rw
This string is put at the end of a line that holds a
::JSON
array. -
#array_nl=(array_nl)
rw
This string is put at the end of a line that holds a
::JSON
array. - #ascii_only? ⇒ Boolean readonly
-
#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
::JSON
to the integer depth, max_nesting = 0 if no maximum should be checked. -
#indent
rw
Returns the string that is used to indent levels in the
::JSON
text. -
#indent=(indent)
rw
Sets the string that is used to indent levels in the
::JSON
text. -
#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
::JSON
to 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
::JSON
object (or Hash). -
#object_nl=(object_nl)
rw
This string is put at the end of a line that holds a
::JSON
object (or Hash). -
#space
rw
Returns the string that is used to insert a space between the tokens in a
::JSON
string. -
#space=(space)
rw
Sets space to the string that is used to insert a space between the tokens in a
::JSON
string. -
#space_before
rw
Returns the string that is used to insert a space before the ‘:’ in
::JSON
objects. -
#space_before=(space_before)
rw
Sets the string that is used to insert a space before the ‘:’ in
::JSON
objects.
Instance Method Summary
-
#[](name)
Returns the value returned by method
name
. -
#[]=(name, value)
Sets the attribute name to value.
-
#configure(opts)
Alias for #merge.
-
#generate(obj)
Generates a valid
::JSON
document from objectobj
and returns the result. -
#initialize_copy(orig)
Initializes this object from orig if it can be duplicated/cloned and returns it.
-
#merge(opts)
(also: #configure)
Configure this
State
instance with the Hash opts, and return itself. -
#to_h ⇒ Hash
(also: #to_hash)
Returns the configuration instance variables as a hash, that can be passed to the configure method.
-
#to_hash ⇒ Hash
Alias for #to_h.
Constructor Details
.new(opts = {})
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.
-
buffer_initial_length: sets the initial length of the generator’s internal buffer.
# File 'ext/json/generator/generator.c', line 976
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self) { VALUE opts; GET_STATE(self); state->max_nesting = 100; state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT; rb_scan_args(argc, argv, "01", &opts); if (!NIL_P(opts)) cState_configure(self, opts); return self; }
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 1021
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 { if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) { CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE); } return rb_funcall(CJSON_SAFE_STATE_PROTOTYPE, i_dup, 0); } }
Instance Attribute Details
#allow_nan? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'ext/json/generator/generator.c', line 1261
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 1187
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) (rw)
This string is put at the end of a line that holds a ::JSON
array.
# File 'ext/json/generator/generator.c', line 1198
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? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'ext/json/generator/generator.c', line 1273
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 1309
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 1321
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? ⇒ Boolean
(readonly)
Returns true, if circular data structures should be checked, otherwise returns false.
# File 'ext/json/generator/generator.c', line 1224
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 1284
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 1296
static VALUE cState_depth_set(VALUE self, VALUE depth) { GET_STATE(self); Check_Type(depth, T_FIXNUM); state->depth = FIX2LONG(depth); return Qnil; }
#indent (rw)
Returns the string that is used to indent levels in the ::JSON
text.
# File 'ext/json/generator/generator.c', line 1040
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) (rw)
Sets the string that is used to indent levels in the ::JSON
text.
# File 'ext/json/generator/generator.c', line 1051
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; }
#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 1236
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 1248
static VALUE cState_max_nesting_set(VALUE self, VALUE depth) { GET_STATE(self); Check_Type(depth, T_FIXNUM); return state->max_nesting = FIX2LONG(depth); }
#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 1151
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) (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 1163
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; }
#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 1077
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) (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 1089
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 (rw)
Returns the string that is used to insert a space before the ‘:’ in ::JSON
objects.
# File 'ext/json/generator/generator.c', line 1114
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) (rw)
Sets the string that is used to insert a space before the ‘:’ in ::JSON
objects.
# File 'ext/json/generator/generator.c', line 1125
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; }
Instance Method Details
#[](name)
Returns the value returned by method name
.
# File 'ext/json/generator/generator.c', line 689
static VALUE cState_aref(VALUE self, VALUE name) { name = rb_funcall(name, i_to_s, 0); if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) { return rb_funcall(self, i_send, 1, name); } else { return rb_ivar_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name))); } }
#[]=(name, value)
Sets the attribute name to value.
# File 'ext/json/generator/generator.c', line 704
static VALUE cState_aset(VALUE self, VALUE name, VALUE value) { VALUE name_writer; name = rb_funcall(name, i_to_s, 0); name_writer = rb_str_cat2(rb_str_dup(name), "="); if (RTEST(rb_funcall(self, i_respond_to_p, 1, name_writer))) { return rb_funcall(self, i_send, 2, name_writer, value); } else { rb_ivar_set(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)), value); } return Qnil; }
#merge(opts)
#configure(opts)
Alias for #merge.
#generate(obj)
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 950
static VALUE cState_generate(VALUE self, VALUE obj) { VALUE result = cState_partial_generate(self, obj); GET_STATE(self); (void)state; return result; }
#initialize_copy(orig)
Initializes this object from orig if it can be duplicated/cloned and returns it.
# File 'ext/json/generator/generator.c', line 993
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); if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim); if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim); if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2); return obj; }
#merge(opts) Also known as: #configure
Configure this State
instance with the Hash opts, and return itself.
# File 'ext/json/generator/generator.c', line 562
static VALUE cState_configure(VALUE self, VALUE opts) { VALUE tmp; GET_STATE(self); tmp = rb_check_convert_type(opts, T_HASH, "Hash", "to_hash"); if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h"); opts = tmp; tmp = rb_hash_aref(opts, ID2SYM(i_indent)); if (RTEST(tmp)) { unsigned long len; Check_Type(tmp, T_STRING); len = RSTRING_LEN(tmp); state->indent = fstrndup(RSTRING_PTR(tmp), len + 1); state->indent_len = len; } tmp = rb_hash_aref(opts, ID2SYM(i_space)); if (RTEST(tmp)) { unsigned long len; Check_Type(tmp, T_STRING); len = RSTRING_LEN(tmp); state->space = fstrndup(RSTRING_PTR(tmp), len + 1); state->space_len = len; } tmp = rb_hash_aref(opts, ID2SYM(i_space_before)); if (RTEST(tmp)) { unsigned long len; Check_Type(tmp, T_STRING); len = RSTRING_LEN(tmp); state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1); state->space_before_len = len; } tmp = rb_hash_aref(opts, ID2SYM(i_array_nl)); if (RTEST(tmp)) { unsigned long len; Check_Type(tmp, T_STRING); len = RSTRING_LEN(tmp); state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1); state->array_nl_len = len; } tmp = rb_hash_aref(opts, ID2SYM(i_object_nl)); if (RTEST(tmp)) { unsigned long len; Check_Type(tmp, T_STRING); len = RSTRING_LEN(tmp); state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1); state->object_nl_len = len; } tmp = ID2SYM(i_max_nesting); state->max_nesting = 100; if (option_given_p(opts, tmp)) { VALUE max_nesting = rb_hash_aref(opts, tmp); if (RTEST(max_nesting)) { Check_Type(max_nesting, T_FIXNUM); state->max_nesting = FIX2LONG(max_nesting); } else { state->max_nesting = 0; } } tmp = ID2SYM(i_depth); state->depth = 0; if (option_given_p(opts, tmp)) { VALUE depth = rb_hash_aref(opts, tmp); if (RTEST(depth)) { Check_Type(depth, T_FIXNUM); state->depth = FIX2LONG(depth); } else { state->depth = 0; } } tmp = ID2SYM(i_buffer_initial_length); if (option_given_p(opts, tmp)) { VALUE buffer_initial_length = rb_hash_aref(opts, tmp); if (RTEST(buffer_initial_length)) { long initial_length; Check_Type(buffer_initial_length, T_FIXNUM); initial_length = FIX2LONG(buffer_initial_length); if (initial_length > 0) state->buffer_initial_length = initial_length; } } tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan)); state->allow_nan = RTEST(tmp); tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only)); state->ascii_only = RTEST(tmp); return self; }
#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/generator/generator.c', line 666
static VALUE cState_to_h(VALUE self) { VALUE result = rb_hash_new(); GET_STATE(self); set_state_ivars(result, self); rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len)); rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len)); rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len)); rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len)); rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len)); rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse); rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse); rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting)); rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth)); rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length)); return result; }
#to_h ⇒ Hash
#to_hash ⇒ Hash
Hash
#to_hash ⇒ Hash
Alias for #to_h.