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



1952
1953
1954
1955
# File 'ext/json/ext/generator/generator.c', line 1952

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



1735
1736
1737
1738
1739
1740
1741
# File 'ext/json/ext/generator/generator.c', line 1735

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



1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
# File 'ext/json/ext/generator/generator.c', line 1400

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



1947
1948
1949
1950
# File 'ext/json/ext/generator/generator.c', line 1947

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

#[](name) ⇒ Object

call-seq:

Returns the value returned by method name.



78
79
80
81
82
83
84
85
86
87
# File 'lib/json/ext/generator/state.rb', line 78

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.



92
93
94
95
96
97
98
99
100
# File 'lib/json/ext/generator/state.rb', line 92

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

#_generate_no_fallback(*args) ⇒ Object



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

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



1702
1703
1704
1705
1706
1707
1708
# File 'ext/json/ext/generator/generator.c', line 1702

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?Object



1691
1692
1693
1694
1695
# File 'ext/json/ext/generator/generator.c', line 1691

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

#array_nlObject



1527
1528
1529
1530
1531
# File 'ext/json/ext/generator/generator.c', line 1527

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



1538
1539
1540
1541
1542
1543
1544
# File 'ext/json/ext/generator/generator.c', line 1538

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_jsonObject



1551
1552
1553
1554
1555
# File 'ext/json/ext/generator/generator.c', line 1551

static VALUE cState_as_json(VALUE self)
{
    GET_STATE(self);
    return state->as_json;
}

#as_json=(as_json) ⇒ Object



1562
1563
1564
1565
1566
1567
1568
# File 'ext/json/ext/generator/generator.c', line 1562

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



1727
1728
1729
1730
1731
1732
1733
# File 'ext/json/ext/generator/generator.c', line 1727

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?Object



1716
1717
1718
1719
1720
# File 'ext/json/ext/generator/generator.c', line 1716

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

#buffer_initial_lengthObject



1827
1828
1829
1830
1831
# File 'ext/json/ext/generator/generator.c', line 1827

static VALUE cState_buffer_initial_length(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->buffer_initial_length);
}

#buffer_initial_length=(buffer_initial_length) ⇒ Object



1848
1849
1850
1851
1852
1853
1854
# File 'ext/json/ext/generator/generator.c', line 1848

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?Object



1576
1577
1578
1579
1580
# File 'ext/json/ext/generator/generator.c', line 1576

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

#depthObject



1802
1803
1804
1805
1806
# File 'ext/json/ext/generator/generator.c', line 1802

static VALUE cState_depth(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->depth);
}

#depth=(depth) ⇒ Object



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

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

#generate(*args) ⇒ Object



1342
1343
1344
1345
1346
1347
1348
# File 'ext/json/ext/generator/generator.c', line 1342

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

#indentObject



1416
1417
1418
1419
1420
# File 'ext/json/ext/generator/generator.c', line 1416

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



1438
1439
1440
1441
1442
1443
1444
# File 'ext/json/ext/generator/generator.c', line 1438

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



1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
# File 'ext/json/ext/generator/generator.c', line 1371

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_nestingObject



1588
1589
1590
1591
1592
# File 'ext/json/ext/generator/generator.c', line 1588

static VALUE cState_max_nesting(VALUE self)
{
    GET_STATE(self);
    return LONG2FIX(state->max_nesting);
}

#max_nesting=(depth) ⇒ Object



1619
1620
1621
1622
1623
1624
1625
# File 'ext/json/ext/generator/generator.c', line 1619

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_nlObject



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

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



1514
1515
1516
1517
1518
1519
1520
# File 'ext/json/ext/generator/generator.c', line 1514

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



1633
1634
1635
1636
1637
# File 'ext/json/ext/generator/generator.c', line 1633

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=



1645
1646
1647
1648
1649
1650
1651
# File 'ext/json/ext/generator/generator.c', line 1645

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



1633
1634
1635
1636
1637
# File 'ext/json/ext/generator/generator.c', line 1633

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

#sort_keysObject



1761
1762
1763
1764
1765
# File 'ext/json/ext/generator/generator.c', line 1761

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

#sort_keys=(value) ⇒ Object



1776
1777
1778
1779
1780
1781
1782
# File 'ext/json/ext/generator/generator.c', line 1776

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

#spaceObject



1452
1453
1454
1455
1456
# File 'ext/json/ext/generator/generator.c', line 1452

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



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

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_beforeObject



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

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



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

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

#strictObject



1661
1662
1663
1664
1665
# File 'ext/json/ext/generator/generator.c', line 1661

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

#strict=(enable) ⇒ Object



1677
1678
1679
1680
1681
1682
1683
# File 'ext/json/ext/generator/generator.c', line 1677

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

#strict?Object



1661
1662
1663
1664
1665
# File 'ext/json/ext/generator/generator.c', line 1661

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.



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