Class: JSON::Ext::Generator::State
- Inherits:
-
Object
- Object
- JSON::Ext::Generator::State
- Defined in:
- lib/json/ext/generator/state.rb,
ext/json/ext/generator/generator.c
Class Method Summary collapse
-
.from_state(opts) ⇒ Object
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.
- .generate(obj, opts, io) ⇒ Object
Instance Method Summary collapse
-
#[](name) ⇒ Object
call-seq: [](name).
-
#[]=(name, value) ⇒ Object
call-seq: []=(name, value).
-
#allow_nan=(enable) ⇒ Object
This sets whether or not to serialize NaN, Infinity, and -Infinity.
-
#allow_nan? ⇒ Boolean
Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.
-
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#array_nl=(array_nl) ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#as_json ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#as_json=(as_json) ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#ascii_only=(enable) ⇒ Object
This sets whether only ASCII characters should be generated.
-
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated.
-
#buffer_initial_length ⇒ Object
This integer returns the current initial length of the buffer.
-
#buffer_initial_length=(length) ⇒ Object
This sets the initial length of the buffer to
length, iflength> 0, otherwise its value isn’t changed. -
#check_circular? ⇒ Boolean
Returns true, if circular data structures should be checked, otherwise returns false.
-
#configure(opts) ⇒ Object
(also: #merge)
call-seq: configure(opts).
-
#depth ⇒ Object
This integer returns the current depth of data structure nesting.
-
#depth=(depth) ⇒ Object
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.
-
#generate(*args) ⇒ Object
Generates a valid JSON document from object
objand returns the result. -
#indent ⇒ Object
Returns the string that is used to indent levels in the JSON text.
-
#indent=(indent) ⇒ Object
Sets the string that is used to indent levels in the JSON text.
-
#initialize_copy(orig) ⇒ Object
Initializes this object from orig if it can be duplicated/cloned and returns it.
-
#max_nesting ⇒ Object
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) ⇒ Object
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 ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
-
#object_nl=(object_nl) ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
-
#script_safe ⇒ Object
(also: #escape_slash)
If this boolean is true, the forward slashes will be escaped in the json output.
-
#script_safe=(enable) ⇒ Object
(also: #escape_slash=)
This sets whether or not the forward slashes will be escaped in the json output.
-
#script_safe ⇒ Boolean
(also: #escape_slash?)
If this boolean is true, the forward slashes will be escaped in the json output.
-
#space ⇒ Object
Returns the string that is used to insert a space between the tokens in a JSON string.
-
#space=(space) ⇒ Object
Sets space to the string that is used to insert a space between the tokens in a JSON string.
-
#space_before ⇒ Object
Returns the string that is used to insert a space before the ‘:’ in JSON objects.
-
#space_before=(space_before) ⇒ Object
Sets the string that is used to insert a space before the ‘:’ in JSON objects.
-
#strict ⇒ Object
If this boolean is false, types unsupported by the JSON format will be serialized as strings.
-
#strict=(enable) ⇒ Object
This sets whether or not to serialize types unsupported by the JSON format as strings.
-
#strict ⇒ Boolean
If this boolean is false, types unsupported by the JSON format will be serialized as strings.
-
#to_h ⇒ Object
(also: #to_hash)
call-seq: to_h.
Class Method Details
.from_state(opts) ⇒ Object
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.
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 |
# File 'ext/json/ext/generator/generator.c', line 1602
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) ⇒ Object
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 |
# File 'ext/json/ext/generator/generator.c', line 2062
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,
.depth = state.depth,
.obj = obj,
.func = generate_json,
};
return rb_ensure(generate_json_try, (VALUE)&data, generate_json_ensure, (VALUE)&data);
}
|
Instance Method Details
#[](name) ⇒ Object
call-seq: [](name)
Returns the value returned by method name.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/json/ext/generator/state.rb', line 77 def [](name) ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0") if respond_to?(name) __send__(name) else instance_variable_get("@#{name}") if instance_variables.include?("@#{name}".to_sym) # avoid warning end end |
#[]=(name, value) ⇒ Object
call-seq: []=(name, value)
Sets the attribute name to value.
91 92 93 94 95 96 97 98 99 |
# File 'lib/json/ext/generator/state.rb', line 91 def []=(name, value) ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0") if respond_to?(name_writer = "#{name}=") __send__ name_writer, value else instance_variable_set "@#{name}", value end end |
#allow_nan=(enable) ⇒ Object
This sets whether or not to serialize NaN, Infinity, and -Infinity
1890 1891 1892 1893 1894 1895 1896 |
# File 'ext/json/ext/generator/generator.c', line 1890
static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->allow_nan = RTEST(enable);
return Qnil;
}
|
#allow_nan? ⇒ Boolean
Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.
1879 1880 1881 1882 1883 |
# File 'ext/json/ext/generator/generator.c', line 1879
static VALUE cState_allow_nan_p(VALUE self)
{
GET_STATE(self);
return state->allow_nan ? Qtrue : Qfalse;
}
|
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
1729 1730 1731 1732 1733 |
# File 'ext/json/ext/generator/generator.c', line 1729
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) ⇒ Object
This string is put at the end of a line that holds a JSON array.
1740 1741 1742 1743 1744 1745 1746 |
# File 'ext/json/ext/generator/generator.c', line 1740
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;
}
|
#as_json ⇒ Object
This string is put at the end of a line that holds a JSON array.
1753 1754 1755 1756 1757 |
# File 'ext/json/ext/generator/generator.c', line 1753
static VALUE cState_as_json(VALUE self)
{
GET_STATE(self);
return state->as_json;
}
|
#as_json=(as_json) ⇒ Object
This string is put at the end of a line that holds a JSON array.
1764 1765 1766 1767 1768 1769 1770 |
# File 'ext/json/ext/generator/generator.c', line 1764
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, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
return Qnil;
}
|
#ascii_only=(enable) ⇒ Object
This sets whether only ASCII characters should be generated.
1915 1916 1917 1918 1919 1920 1921 |
# File 'ext/json/ext/generator/generator.c', line 1915
static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->ascii_only = RTEST(enable);
return Qnil;
}
|
#ascii_only? ⇒ Boolean
Returns true, if only ASCII characters should be generated. Otherwise returns false.
1904 1905 1906 1907 1908 |
# File 'ext/json/ext/generator/generator.c', line 1904
static VALUE cState_ascii_only_p(VALUE self)
{
GET_STATE(self);
return state->ascii_only ? Qtrue : Qfalse;
}
|
#buffer_initial_length ⇒ Object
This integer returns the current initial length of the buffer.
1966 1967 1968 1969 1970 |
# File 'ext/json/ext/generator/generator.c', line 1966
static VALUE cState_buffer_initial_length(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->buffer_initial_length);
}
|
#buffer_initial_length=(length) ⇒ Object
This sets the initial length of the buffer to length, if length > 0, otherwise its value isn’t changed.
1987 1988 1989 1990 1991 1992 1993 |
# File 'ext/json/ext/generator/generator.c', line 1987
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;
}
|
#check_circular? ⇒ Boolean
Returns true, if circular data structures should be checked, otherwise returns false.
1778 1779 1780 1781 1782 |
# File 'ext/json/ext/generator/generator.c', line 1778
static VALUE cState_check_circular_p(VALUE self)
{
GET_STATE(self);
return state->max_nesting ? Qtrue : Qfalse;
}
|
#configure(opts) ⇒ Object Also known as: merge
call-seq: configure(opts)
Configure this State instance with the Hash opts, and return itself.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File '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 |
#depth ⇒ Object
This integer returns the current depth of data structure nesting.
1941 1942 1943 1944 1945 |
# File 'ext/json/ext/generator/generator.c', line 1941
static VALUE cState_depth(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->depth);
}
|
#depth=(depth) ⇒ Object
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.
1953 1954 1955 1956 1957 1958 1959 |
# File 'ext/json/ext/generator/generator.c', line 1953
static VALUE cState_depth_set(VALUE self, VALUE depth)
{
rb_check_frozen(self);
GET_STATE(self);
state->depth = long_config(depth);
return Qnil;
}
|
#generate(obj) ⇒ String #generate(obj, anIO) ⇒ Object
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.
1556 1557 1558 1559 1560 1561 1562 |
# File 'ext/json/ext/generator/generator.c', line 1556
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);
}
|
#indent ⇒ Object
Returns the string that is used to indent levels in the JSON text.
1618 1619 1620 1621 1622 |
# File 'ext/json/ext/generator/generator.c', line 1618
static VALUE cState_indent(VALUE self)
{
GET_STATE(self);
return state->indent ? state->indent : rb_str_freeze(rb_utf8_str_new("", 0));
}
|
#indent=(indent) ⇒ Object
Sets the string that is used to indent levels in the JSON text.
1640 1641 1642 1643 1644 1645 1646 |
# File 'ext/json/ext/generator/generator.c', line 1640
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;
}
|
#initialize_copy(orig) ⇒ Object
Initializes this object from orig if it can be duplicated/cloned and returns it.
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 |
# File 'ext/json/ext/generator/generator.c', line 1576
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;
}
|
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
1790 1791 1792 1793 1794 |
# File 'ext/json/ext/generator/generator.c', line 1790
static VALUE cState_max_nesting(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->max_nesting);
}
|
#max_nesting=(depth) ⇒ Object
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.
1807 1808 1809 1810 1811 1812 1813 |
# File 'ext/json/ext/generator/generator.c', line 1807
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;
}
|
#object_nl ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
1704 1705 1706 1707 1708 |
# File 'ext/json/ext/generator/generator.c', line 1704
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) ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
1716 1717 1718 1719 1720 1721 1722 |
# File 'ext/json/ext/generator/generator.c', line 1716
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;
}
|
#script_safe ⇒ Object Also known as: escape_slash
If this boolean is true, the forward slashes will be escaped in the json output.
1821 1822 1823 1824 1825 |
# File 'ext/json/ext/generator/generator.c', line 1821
static VALUE cState_script_safe(VALUE self)
{
GET_STATE(self);
return state->script_safe ? Qtrue : Qfalse;
}
|
#script_safe=(enable) ⇒ Object Also known as: escape_slash=
This sets whether or not the forward slashes will be escaped in the json output.
1833 1834 1835 1836 1837 1838 1839 |
# File 'ext/json/ext/generator/generator.c', line 1833
static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->script_safe = RTEST(enable);
return Qnil;
}
|
#script_safe ⇒ Boolean Also known as: escape_slash?
If this boolean is true, the forward slashes will be escaped in the json output.
1821 1822 1823 1824 1825 |
# File 'ext/json/ext/generator/generator.c', line 1821
static VALUE cState_script_safe(VALUE self)
{
GET_STATE(self);
return state->script_safe ? Qtrue : Qfalse;
}
|
#space ⇒ Object
Returns the string that is used to insert a space between the tokens in a JSON string.
1654 1655 1656 1657 1658 |
# File 'ext/json/ext/generator/generator.c', line 1654
static VALUE cState_space(VALUE self)
{
GET_STATE(self);
return state->space ? state->space : rb_str_freeze(rb_utf8_str_new("", 0));
}
|
#space=(space) ⇒ Object
Sets space to the string that is used to insert a space between the tokens in a JSON string.
1666 1667 1668 1669 1670 1671 1672 |
# File 'ext/json/ext/generator/generator.c', line 1666
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;
}
|
#space_before ⇒ Object
Returns the string that is used to insert a space before the ‘:’ in JSON objects.
1679 1680 1681 1682 1683 |
# File 'ext/json/ext/generator/generator.c', line 1679
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) ⇒ Object
Sets the string that is used to insert a space before the ‘:’ in JSON objects.
1690 1691 1692 1693 1694 1695 1696 |
# File 'ext/json/ext/generator/generator.c', line 1690
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;
}
|
#strict ⇒ Object
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.
1849 1850 1851 1852 1853 |
# File 'ext/json/ext/generator/generator.c', line 1849
static VALUE cState_strict(VALUE self)
{
GET_STATE(self);
return state->strict ? Qtrue : Qfalse;
}
|
#strict=(enable) ⇒ Object
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.
1865 1866 1867 1868 1869 1870 1871 |
# File 'ext/json/ext/generator/generator.c', line 1865
static VALUE cState_strict_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->strict = RTEST(enable);
return Qnil;
}
|
#strict ⇒ Boolean
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.
1849 1850 1851 1852 1853 |
# File 'ext/json/ext/generator/generator.c', line 1849
static VALUE cState_strict(VALUE self)
{
GET_STATE(self);
return state->strict ? Qtrue : Qfalse;
}
|
#to_h ⇒ Object Also known as: to_hash
call-seq: to_h
Returns the configuration instance variables as a hash, that can be passed to the configure method.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File '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 |