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

._generate_no_fallback(obj, opts, io) ⇒ Object



1950
1951
1952
1953
# File 'ext/json/ext/generator/generator.c', line 1950

static VALUE cState_m_generate_no_fallback(VALUE klass, VALUE obj, VALUE opts, VALUE io)
{
    return cState_m_do_generate(klass, obj, opts, io, generate_json_no_fallback);
}

.default_sort_keys_proc=(proc) ⇒ Object



1731
1732
1733
1734
1735
1736
1737
# File 'ext/json/ext/generator/generator.c', line 1731

static VALUE cState_set_default_sort_keys_proc(VALUE self, VALUE proc)
{
    if (!rb_obj_is_proc(proc)) {
        rb_raise(rb_eTypeError, "sort_key_proc must be a Proc");
    }
    return default_sort_keys_proc = proc;
}

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



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

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



1945
1946
1947
1948
# File 'ext/json/ext/generator/generator.c', line 1945

static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
{
    return cState_m_do_generate(klass, obj, opts, io, generate_json);
}

Instance Method Details

#_generate_no_fallback(*args) ⇒ Object

:nodoc:



1347
1348
1349
1350
1351
1352
1353
# File 'ext/json/ext/generator/generator.c', line 1347

static VALUE cState_generate_no_fallback(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_no_fallback, io);
}

#allow_nan=(enable) ⇒ Object

This sets whether or not to serialize NaN, Infinity, and -Infinity



1698
1699
1700
1701
1702
1703
1704
# File 'ext/json/ext/generator/generator.c', line 1698

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.

Returns:

  • (Boolean)


1687
1688
1689
1690
1691
# File 'ext/json/ext/generator/generator.c', line 1687

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.



1523
1524
1525
1526
1527
# File 'ext/json/ext/generator/generator.c', line 1523

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.



1534
1535
1536
1537
1538
1539
1540
# File 'ext/json/ext/generator/generator.c', line 1534

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.



1547
1548
1549
1550
1551
# File 'ext/json/ext/generator/generator.c', line 1547

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.



1558
1559
1560
1561
1562
1563
1564
# File 'ext/json/ext/generator/generator.c', line 1558

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.



1723
1724
1725
1726
1727
1728
1729
# File 'ext/json/ext/generator/generator.c', line 1723

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.

Returns:

  • (Boolean)


1712
1713
1714
1715
1716
# File 'ext/json/ext/generator/generator.c', line 1712

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.



1816
1817
1818
1819
1820
# File 'ext/json/ext/generator/generator.c', line 1816

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.



1837
1838
1839
1840
1841
1842
1843
# File 'ext/json/ext/generator/generator.c', line 1837

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.

Returns:

  • (Boolean)


1572
1573
1574
1575
1576
# File 'ext/json/ext/generator/generator.c', line 1572

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.



1791
1792
1793
1794
1795
# File 'ext/json/ext/generator/generator.c', line 1791

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.



1803
1804
1805
1806
1807
1808
1809
# File 'ext/json/ext/generator/generator.c', line 1803

static VALUE cState_depth_set(VALUE self, VALUE depth)
{
    rb_check_frozen(self);
    GET_STATE(self);
    state->depth = depth_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.

Overloads:

  • #generate(obj) ⇒ String

    Returns:

    • (String)


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

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.



1412
1413
1414
1415
1416
# File 'ext/json/ext/generator/generator.c', line 1412

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.



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

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.



1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'ext/json/ext/generator/generator.c', line 1367

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

    RB_OBJ_WRITTEN(obj, Qundef, objState->indent);
    RB_OBJ_WRITTEN(obj, Qundef, objState->space);
    RB_OBJ_WRITTEN(obj, Qundef, objState->space_before);
    RB_OBJ_WRITTEN(obj, Qundef, objState->object_nl);
    RB_OBJ_WRITTEN(obj, Qundef, objState->array_nl);
    RB_OBJ_WRITTEN(obj, Qundef, objState->as_json);
    RB_OBJ_WRITTEN(obj, Qundef, objState->sort_keys);

    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.



1584
1585
1586
1587
1588
# File 'ext/json/ext/generator/generator.c', line 1584

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.



1615
1616
1617
1618
1619
1620
1621
# File 'ext/json/ext/generator/generator.c', line 1615

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



1498
1499
1500
1501
1502
# File 'ext/json/ext/generator/generator.c', line 1498

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



1510
1511
1512
1513
1514
1515
1516
# File 'ext/json/ext/generator/generator.c', line 1510

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

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



1629
1630
1631
1632
1633
# File 'ext/json/ext/generator/generator.c', line 1629

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

#script_safe=(enable) ⇒ Object

This sets whether or not the forward slashes will be escaped in the json output.



1641
1642
1643
1644
1645
1646
1647
# File 'ext/json/ext/generator/generator.c', line 1641

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

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

Returns:

  • (Boolean)


1629
1630
1631
1632
1633
# File 'ext/json/ext/generator/generator.c', line 1629

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

#sort_keys ⇒ Object

Get the value of sort_keys.



1757
1758
1759
1760
1761
# File 'ext/json/ext/generator/generator.c', line 1757

static VALUE cState_sort_keys_p(VALUE self)
{
    GET_STATE(self);
    return state->sort_keys;
}

#sort_keys=(value) ⇒ Object

value is a boolean or a proc. If the value is the boolean true, object keys will be sorted lexicographically in ascending order.

If the value is a proc, it receives the entire Hash and must return a Hash with its pairs in the desired order, allowing for arbitrary sorting.



1772
1773
1774
1775
1776
1777
1778
# File 'ext/json/ext/generator/generator.c', line 1772

static VALUE cState_sort_keys_set(VALUE self, VALUE value)
{
    rb_check_frozen(self);
    GET_STATE(self);
    RB_OBJ_WRITE(self, &state->sort_keys, normalize_sort_keys(value));
    return Qnil;
}

#space ⇒ Object

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



1448
1449
1450
1451
1452
# File 'ext/json/ext/generator/generator.c', line 1448

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.



1460
1461
1462
1463
1464
1465
1466
# File 'ext/json/ext/generator/generator.c', line 1460

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.



1473
1474
1475
1476
1477
# File 'ext/json/ext/generator/generator.c', line 1473

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.



1484
1485
1486
1487
1488
1489
1490
# File 'ext/json/ext/generator/generator.c', line 1484

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.



1657
1658
1659
1660
1661
# File 'ext/json/ext/generator/generator.c', line 1657

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.



1673
1674
1675
1676
1677
1678
1679
# File 'ext/json/ext/generator/generator.c', line 1673

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.

Returns:

  • (Boolean)


1657
1658
1659
1660
1661
# File 'ext/json/ext/generator/generator.c', line 1657

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
71
# 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,
    sort_keys: sort_keys
  }

  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