Class: JSON::Ext::Generator::State

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ext/generator/state.rb,
ext/json/ext/generator/generator.c

Class Method Summary collapse

Instance Method Summary collapse

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.



1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'ext/json/ext/generator/generator.c', line 1264

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

:nodoc:



1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
# File 'ext/json/ext/generator/generator.c', line 1672

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.



84
85
86
87
88
89
90
91
# File 'lib/json/ext/generator/state.rb', line 84

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.



96
97
98
99
100
101
102
# File 'lib/json/ext/generator/state.rb', line 96

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



1543
1544
1545
1546
1547
1548
# File 'ext/json/ext/generator/generator.c', line 1543

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.

Returns:

  • (Boolean)


1532
1533
1534
1535
1536
# File 'ext/json/ext/generator/generator.c', line 1532

static VALUE cState_allow_nan_p(VALUE self)
{
    GET_STATE(self);
    return state->allow_nan ? Qtrue : Qfalse;
}

#array_nlObject

This string is put at the end of a line that holds a JSON array.



1387
1388
1389
1390
1391
# File 'ext/json/ext/generator/generator.c', line 1387

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.



1398
1399
1400
1401
1402
1403
# File 'ext/json/ext/generator/generator.c', line 1398

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_jsonObject

This string is put at the end of a line that holds a JSON array.



1410
1411
1412
1413
1414
# File 'ext/json/ext/generator/generator.c', line 1410

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.



1421
1422
1423
1424
1425
1426
# File 'ext/json/ext/generator/generator.c', line 1421

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) ⇒ Object

This sets whether only ASCII characters should be generated.



1567
1568
1569
1570
1571
1572
# File 'ext/json/ext/generator/generator.c', line 1567

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.

Returns:

  • (Boolean)


1556
1557
1558
1559
1560
# File 'ext/json/ext/generator/generator.c', line 1556

static VALUE cState_ascii_only_p(VALUE self)
{
    GET_STATE(self);
    return state->ascii_only ? Qtrue : Qfalse;
}

#buffer_initial_lengthObject

This integer returns the current initial length of the buffer.



1603
1604
1605
1606
1607
# File 'ext/json/ext/generator/generator.c', line 1603

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.



1624
1625
1626
1627
1628
1629
# File 'ext/json/ext/generator/generator.c', line 1624

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.

Returns:

  • (Boolean)


1434
1435
1436
1437
1438
# File 'ext/json/ext/generator/generator.c', line 1434

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

#depthObject

This integer returns the current depth of data structure nesting.



1579
1580
1581
1582
1583
# File 'ext/json/ext/generator/generator.c', line 1579

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.



1591
1592
1593
1594
1595
1596
# File 'ext/json/ext/generator/generator.c', line 1591

static VALUE cState_depth_set(VALUE self, VALUE depth)
{
    GET_STATE(self);
    state->depth = long_config(depth);
    return Qnil;
}

#generate(obj) ⇒ String #generate(obj, anIO) ⇒ Object Also known as: generate_new

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.

Overloads:

  • #generate(obj) ⇒ String

    Returns:

    • (String)


1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
# File 'ext/json/ext/generator/generator.c', line 1215

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;
    VALUE result = cState_partial_generate(self, obj, generate_json, io);
    GET_STATE(self);
    (void)state;
    return result;
}

#indentObject

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



1280
1281
1282
1283
1284
# File 'ext/json/ext/generator/generator.c', line 1280

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.



1302
1303
1304
1305
1306
1307
# File 'ext/json/ext/generator/generator.c', line 1302

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.



1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
# File 'ext/json/ext/generator/generator.c', line 1238

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_nestingObject

This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.



1446
1447
1448
1449
1450
# File 'ext/json/ext/generator/generator.c', line 1446

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.



1463
1464
1465
1466
1467
1468
# File 'ext/json/ext/generator/generator.c', line 1463

static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
    GET_STATE(self);
    state->max_nesting = long_config(depth);
    return Qnil;
}

#object_nlObject

This string is put at the end of a line that holds a JSON object (or Hash).



1363
1364
1365
1366
1367
# File 'ext/json/ext/generator/generator.c', line 1363

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).



1375
1376
1377
1378
1379
1380
# File 'ext/json/ext/generator/generator.c', line 1375

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_safeObject Also known as: escape_slash

If this boolean is true, the forward slashes will be escaped in the json output.



1476
1477
1478
1479
1480
# File 'ext/json/ext/generator/generator.c', line 1476

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.



1488
1489
1490
1491
1492
1493
# File 'ext/json/ext/generator/generator.c', line 1488

static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
    GET_STATE(self);
    state->script_safe = RTEST(enable);
    return Qnil;
}

#script_safeBoolean Also known as: escape_slash?

If this boolean is true, the forward slashes will be escaped in the json output.

Returns:

  • (Boolean)


1476
1477
1478
1479
1480
# File 'ext/json/ext/generator/generator.c', line 1476

static VALUE cState_script_safe(VALUE self)
{
    GET_STATE(self);
    return state->script_safe ? Qtrue : Qfalse;
}

#spaceObject

Returns the string that is used to insert a space between the tokens in a JSON string.



1315
1316
1317
1318
1319
# File 'ext/json/ext/generator/generator.c', line 1315

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.



1327
1328
1329
1330
1331
1332
# File 'ext/json/ext/generator/generator.c', line 1327

static VALUE cState_space_set(VALUE self, VALUE space)
{
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->space, string_config(space));
    return Qnil;
}

#space_beforeObject

Returns the string that is used to insert a space before the ‘:’ in JSON objects.



1339
1340
1341
1342
1343
# File 'ext/json/ext/generator/generator.c', line 1339

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.



1350
1351
1352
1353
1354
1355
# File 'ext/json/ext/generator/generator.c', line 1350

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;
}

#strictObject

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.



1503
1504
1505
1506
1507
# File 'ext/json/ext/generator/generator.c', line 1503

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.



1519
1520
1521
1522
1523
1524
# File 'ext/json/ext/generator/generator.c', line 1519

static VALUE cState_strict_set(VALUE self, VALUE enable)
{
    GET_STATE(self);
    state->strict = RTEST(enable);
    return Qnil;
}

#strictBoolean

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.

Returns:

  • (Boolean)


1503
1504
1505
1506
1507
# File 'ext/json/ext/generator/generator.c', line 1503

static VALUE cState_strict(VALUE self)
{
    GET_STATE(self);
    return state->strict ? Qtrue : Qfalse;
}

#to_hObject 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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/json/ext/generator/state.rb', line 54

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,
  }

  instance_variables.each do |iv|
    iv = iv.to_s[1..-1]
    result[iv.to_sym] = self[iv]
  end

  result
end