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

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

Overview

This class is used to create State instances, that are use to hold data while generating a JSON text from a a Ruby data structure.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(opts = {}) ⇒ Object

Instantiates a new State object, configured by opts.

opts can have the following keys:

  • indent: a string used to indent levels (default: ”),

  • space: a string that is put after, a : or , delimiter (default: ”),

  • space_before: a string that is put before a : pair delimiter (default: ”),

  • object_nl: a string that is put at the end of a JSON object (default: ”),

  • array_nl: a string that is put at the end of a JSON array (default: ”),

  • check_circular: true if checking for circular data structures should be done, false (the default) otherwise.

  • allow_nan: true if NaN, Infinity, and -Infinity should be generated, otherwise an exception is thrown, if these values are encountered. This options defaults to false.



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'ext/json/ext/generator/generator.c', line 553

static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE opts;
    GET_STATE(self);

    rb_scan_args(argc, argv, "01", &opts);
    state->indent = rb_str_new2("");
    state->space = rb_str_new2("");
    state->space_before = rb_str_new2("");
    state->array_nl = rb_str_new2("");
    state->object_nl = rb_str_new2("");
    if (NIL_P(opts)) {
        state->check_circular = 1;
        state->allow_nan = 0;
        state->max_nesting = 19;
    } else {
        cState_configure(self, opts);
    }
    state->seen = rb_hash_new();
    state->memo = Qnil;
    state->depth = INT2FIX(0);
    return self;
}

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.



584
585
586
587
588
589
590
591
592
593
# File 'ext/json/ext/generator/generator.c', line 584

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_funcall(self, i_new, 0);
    }
}

Instance Method Details

#allow_nan?Boolean

Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.



758
759
760
761
762
# File 'ext/json/ext/generator/generator.c', line 758

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.



696
697
698
699
700
# File 'ext/json/ext/generator/generator.c', line 696

static VALUE cState_array_nl(VALUE self)
{
    GET_STATE(self);
    return state->array_nl;
}

#array_nl=(array_nl) ⇒ Object

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



707
708
709
710
711
712
# File 'ext/json/ext/generator/generator.c', line 707

static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
{
    GET_STATE(self);
    Check_Type(array_nl, T_STRING);
    return state->array_nl = array_nl;
}

#check_circular?Boolean

Returns true, if circular data structures should be checked, otherwise returns false.



720
721
722
723
724
# File 'ext/json/ext/generator/generator.c', line 720

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

#configureObject

#forget(object) ⇒ Object

Forget object for this generating run.



792
793
794
795
796
# File 'ext/json/ext/generator/generator.c', line 792

static VALUE cState_forget(VALUE self, VALUE object)
{
    GET_STATE(self);
    return rb_hash_delete(state->seen, rb_obj_id(object));
}

#indentObject

This string is used to indent levels in the JSON text.



600
601
602
603
604
# File 'ext/json/ext/generator/generator.c', line 600

static VALUE cState_indent(VALUE self)
{
    GET_STATE(self);
    return state->indent;
}

#indent=(indent) ⇒ Object

This string is used to indent levels in the JSON text.



611
612
613
614
615
616
# File 'ext/json/ext/generator/generator.c', line 611

static VALUE cState_indent_set(VALUE self, VALUE indent)
{
    GET_STATE(self);
    Check_Type(indent, T_STRING);
    return state->indent = indent;
}

#max_nestingObject

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



732
733
734
735
736
# File 'ext/json/ext/generator/generator.c', line 732

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.



744
745
746
747
748
749
750
# File 'ext/json/ext/generator/generator.c', line 744

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

#object_nlObject

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



672
673
674
675
676
# File 'ext/json/ext/generator/generator.c', line 672

static VALUE cState_object_nl(VALUE self)
{
    GET_STATE(self);
    return state->object_nl;
}

#object_nl=(object_nl) ⇒ Object

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



684
685
686
687
688
689
# File 'ext/json/ext/generator/generator.c', line 684

static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
{
    GET_STATE(self);
    Check_Type(object_nl, T_STRING);
    return state->object_nl = object_nl;
}

#remember(object) ⇒ Object

Remember object, to find out if it was already encountered (if a cyclic data structure is rendered).



781
782
783
784
785
# File 'ext/json/ext/generator/generator.c', line 781

static VALUE cState_remember(VALUE self, VALUE object)
{
    GET_STATE(self);
    return rb_hash_aset(state->seen, rb_obj_id(object), Qtrue);
}

#seen?(object) ⇒ Boolean

Returns true, if object was already seen during this generating run.



769
770
771
772
773
# File 'ext/json/ext/generator/generator.c', line 769

static VALUE cState_seen_p(VALUE self, VALUE object)
{
    GET_STATE(self);
    return rb_hash_aref(state->seen, rb_obj_id(object));
}

#spaceObject

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



624
625
626
627
628
# File 'ext/json/ext/generator/generator.c', line 624

static VALUE cState_space(VALUE self)
{
    GET_STATE(self);
    return state->space;
}

#space=(space) ⇒ Object

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



636
637
638
639
640
641
# File 'ext/json/ext/generator/generator.c', line 636

static VALUE cState_space_set(VALUE self, VALUE space)
{
    GET_STATE(self);
    Check_Type(space, T_STRING);
    return state->space = space;
}

#space_beforeObject

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



648
649
650
651
652
# File 'ext/json/ext/generator/generator.c', line 648

static VALUE cState_space_before(VALUE self)
{
    GET_STATE(self);
    return state->space_before;
}

#space_before=(space_before) ⇒ Object

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



659
660
661
662
663
664
# File 'ext/json/ext/generator/generator.c', line 659

static VALUE cState_space_before_set(VALUE self, VALUE space_before)
{
    GET_STATE(self);
    Check_Type(space_before, T_STRING);
    return state->space_before = space_before;
}

#to_hObject

Returns the configuration instance variables as a hash, that can be passed to the configure method.



519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'ext/json/ext/generator/generator.c', line 519

static VALUE cState_to_h(VALUE self)
{
    VALUE result = rb_hash_new();
    GET_STATE(self);
    rb_hash_aset(result, ID2SYM(i_indent), state->indent);
    rb_hash_aset(result, ID2SYM(i_space), state->space);
    rb_hash_aset(result, ID2SYM(i_space_before), state->space_before);
    rb_hash_aset(result, ID2SYM(i_object_nl), state->object_nl);
    rb_hash_aset(result, ID2SYM(i_array_nl), state->array_nl);
    rb_hash_aset(result, ID2SYM(i_check_circular), state->check_circular ? Qtrue : Qfalse);
    rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
    rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
    return result;
}