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.
-
#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(obj, io = nil) ⇒ Object
call-seq: generate(obj) -> String generate(obj, anIO) -> anIO.
-
#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.
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 |
# File 'ext/json/ext/generator/generator.c', line 1145
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
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 |
# File 'ext/json/ext/generator/generator.c', line 1530
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);
}
|
Instance Method Details
#[](name) ⇒ Object
call-seq: [](name)
Returns the value returned by method name
.
94 95 96 97 98 99 100 101 |
# File '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 |
#[]=(name, value) ⇒ Object
call-seq: []=(name, value)
Sets the attribute name to value.
106 107 108 109 110 111 112 |
# File '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 |
#allow_nan=(enable) ⇒ Object
This sets whether or not to serialize NaN, Infinity, and -Infinity
1402 1403 1404 1405 1406 1407 |
# File 'ext/json/ext/generator/generator.c', line 1402
static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
{
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.
1391 1392 1393 1394 1395 |
# File 'ext/json/ext/generator/generator.c', line 1391
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.
1268 1269 1270 1271 1272 |
# File 'ext/json/ext/generator/generator.c', line 1268
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.
1279 1280 1281 1282 1283 1284 |
# File 'ext/json/ext/generator/generator.c', line 1279
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;
}
|
#ascii_only=(enable) ⇒ Object
This sets whether only ASCII characters should be generated.
1426 1427 1428 1429 1430 1431 |
# File 'ext/json/ext/generator/generator.c', line 1426
static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
{
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.
1415 1416 1417 1418 1419 |
# File 'ext/json/ext/generator/generator.c', line 1415
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.
1462 1463 1464 1465 1466 |
# File 'ext/json/ext/generator/generator.c', line 1462
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.
1483 1484 1485 1486 1487 1488 |
# File 'ext/json/ext/generator/generator.c', line 1483
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
Returns true, if circular data structures should be checked, otherwise returns false.
1293 1294 1295 1296 1297 |
# File 'ext/json/ext/generator/generator.c', line 1293
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.
35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 _configure(opts) end |
#depth ⇒ Object
This integer returns the current depth of data structure nesting.
1438 1439 1440 1441 1442 |
# File 'ext/json/ext/generator/generator.c', line 1438
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.
1450 1451 1452 1453 1454 1455 |
# File 'ext/json/ext/generator/generator.c', line 1450
static VALUE cState_depth_set(VALUE self, VALUE depth)
{
GET_STATE(self);
state->depth = long_config(depth);
return Qnil;
}
|
#generate(obj, io = nil) ⇒ Object
call-seq:
generate(obj) -> String
generate(obj, 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 GeneratorError exception.
57 58 59 |
# File 'lib/json/ext/generator/state.rb', line 57 def generate(obj, io = nil) _generate(obj, io) end |
#indent ⇒ Object
Returns the string that is used to indent levels in the JSON text.
1161 1162 1163 1164 1165 |
# File 'ext/json/ext/generator/generator.c', line 1161
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.
1183 1184 1185 1186 1187 1188 |
# File 'ext/json/ext/generator/generator.c', line 1183
static VALUE cState_indent_set(VALUE self, VALUE indent)
{
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.
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 |
# File 'ext/json/ext/generator/generator.c', line 1120
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;
}
|
#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.
1305 1306 1307 1308 1309 |
# File 'ext/json/ext/generator/generator.c', line 1305
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.
1322 1323 1324 1325 1326 1327 |
# File 'ext/json/ext/generator/generator.c', line 1322
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
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).
1244 1245 1246 1247 1248 |
# File 'ext/json/ext/generator/generator.c', line 1244
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).
1256 1257 1258 1259 1260 1261 |
# File 'ext/json/ext/generator/generator.c', line 1256
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 ⇒ Object Also known as: escape_slash
If this boolean is true, the forward slashes will be escaped in the json output.
1335 1336 1337 1338 1339 |
# File 'ext/json/ext/generator/generator.c', line 1335
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.
1347 1348 1349 1350 1351 1352 |
# File 'ext/json/ext/generator/generator.c', line 1347
static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
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.
1335 1336 1337 1338 1339 |
# File 'ext/json/ext/generator/generator.c', line 1335
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.
1196 1197 1198 1199 1200 |
# File 'ext/json/ext/generator/generator.c', line 1196
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.
1208 1209 1210 1211 1212 1213 |
# File 'ext/json/ext/generator/generator.c', line 1208
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 ⇒ Object
Returns the string that is used to insert a space before the ‘:’ in JSON objects.
1220 1221 1222 1223 1224 |
# File 'ext/json/ext/generator/generator.c', line 1220
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.
1231 1232 1233 1234 1235 1236 |
# File 'ext/json/ext/generator/generator.c', line 1231
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 ⇒ 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.
1362 1363 1364 1365 1366 |
# File 'ext/json/ext/generator/generator.c', line 1362
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.
1378 1379 1380 1381 1382 1383 |
# File 'ext/json/ext/generator/generator.c', line 1378
static VALUE cState_strict_set(VALUE self, VALUE enable)
{
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.
1362 1363 1364 1365 1366 |
# File 'ext/json/ext/generator/generator.c', line 1362
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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File '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 |