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.



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'ext/json/ext/generator/generator.c', line 1003

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

Instance Method Details

#[](name) ⇒ Object

call-seq: [](name)

Returns the value returned by method name.



113
114
115
116
117
118
119
120
# File 'lib/json/ext/generator/state.rb', line 113

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.



125
126
127
128
129
130
131
# File 'lib/json/ext/generator/state.rb', line 125

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



1308
1309
1310
1311
1312
1313
# File 'ext/json/ext/generator/generator.c', line 1308

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)


1297
1298
1299
1300
1301
# File 'ext/json/ext/generator/generator.c', line 1297

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.



1166
1167
1168
1169
1170
# File 'ext/json/ext/generator/generator.c', line 1166

static VALUE cState_array_nl(VALUE self)
{
    GET_STATE(self);
    return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
}

#array_nl=(array_nl) ⇒ Object

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



1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'ext/json/ext/generator/generator.c', line 1177

static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(array_nl, T_STRING);
    len = RSTRING_LEN(array_nl);
    if (len == 0) {
        if (state->array_nl) {
            ruby_xfree(state->array_nl);
            state->array_nl = NULL;
        }
    } else {
        if (state->array_nl) ruby_xfree(state->array_nl);
        state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
        state->array_nl_len = len;
    }
    return Qnil;
}

#ascii_only=(enable) ⇒ Object

This sets whether only ASCII characters should be generated.



1332
1333
1334
1335
1336
1337
# File 'ext/json/ext/generator/generator.c', line 1332

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)


1321
1322
1323
1324
1325
# File 'ext/json/ext/generator/generator.c', line 1321

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.



1369
1370
1371
1372
1373
# File 'ext/json/ext/generator/generator.c', line 1369

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.



1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
# File 'ext/json/ext/generator/generator.c', line 1381

static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
{
    long initial_length;
    GET_STATE(self);
    Check_Type(buffer_initial_length, T_FIXNUM);
    initial_length = FIX2LONG(buffer_initial_length);
    if (initial_length > 0) {
        state->buffer_initial_length = initial_length;
    }
    return Qnil;
}

#check_circular?Boolean

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

Returns:

  • (Boolean)


1203
1204
1205
1206
1207
# File 'ext/json/ext/generator/generator.c', line 1203

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
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
72
73
74
75
76
# 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

  opts.each do |key, value|
    case key
    when :indent
      self.indent = value
    when :space
      self.space = value
    when :space_before
      self.space_before = value
    when :array_nl
      self.array_nl = value
    when :object_nl
      self.object_nl = value
    when :max_nesting
      self.max_nesting = value || 0
    when :depth
      self.depth = value
    when :buffer_initial_length
      self.buffer_initial_length = value
    when :allow_nan
      self.allow_nan = value
    when :ascii_only
      self.ascii_only = value
    when :script_safe, :escape_slash
      self.script_safe = value
    when :strict
      self.strict = value
    end
  end

  self
end

#depthObject

This integer returns the current depth of data structure nesting.



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

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.



1356
1357
1358
1359
1360
1361
1362
# File 'ext/json/ext/generator/generator.c', line 1356

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

#generate(obj) ⇒ 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.



958
959
960
961
962
963
964
# File 'ext/json/ext/generator/generator.c', line 958

static VALUE cState_generate(VALUE self, VALUE obj)
{
    VALUE result = cState_partial_generate(self, obj);
    GET_STATE(self);
    (void)state;
    return result;
}

#indentObject

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



1019
1020
1021
1022
1023
# File 'ext/json/ext/generator/generator.c', line 1019

static VALUE cState_indent(VALUE self)
{
    GET_STATE(self);
    return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
}

#indent=(indent) ⇒ Object

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



1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'ext/json/ext/generator/generator.c', line 1030

static VALUE cState_indent_set(VALUE self, VALUE indent)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(indent, T_STRING);
    len = RSTRING_LEN(indent);
    if (len == 0) {
        if (state->indent) {
            ruby_xfree(state->indent);
            state->indent = NULL;
            state->indent_len = 0;
        }
    } else {
        if (state->indent) ruby_xfree(state->indent);
        state->indent = fstrndup(RSTRING_PTR(indent), len);
        state->indent_len = len;
    }
    return Qnil;
}

#initialize_copy(orig) ⇒ Object

Initializes this object from orig if it can be duplicated/cloned and returns it.



978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# File 'ext/json/ext/generator/generator.c', line 978

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 = fstrndup(origState->indent, origState->indent_len);
    objState->space = fstrndup(origState->space, origState->space_len);
    objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
    objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
    objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
    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.



1215
1216
1217
1218
1219
# File 'ext/json/ext/generator/generator.c', line 1215

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.



1227
1228
1229
1230
1231
1232
1233
# File 'ext/json/ext/generator/generator.c', line 1227

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



1130
1131
1132
1133
1134
# File 'ext/json/ext/generator/generator.c', line 1130

static VALUE cState_object_nl(VALUE self)
{
    GET_STATE(self);
    return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
}

#object_nl=(object_nl) ⇒ Object

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



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'ext/json/ext/generator/generator.c', line 1142

static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(object_nl, T_STRING);
    len = RSTRING_LEN(object_nl);
    if (len == 0) {
        if (state->object_nl) {
            ruby_xfree(state->object_nl);
            state->object_nl = NULL;
        }
    } else {
        if (state->object_nl) ruby_xfree(state->object_nl);
        state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
        state->object_nl_len = len;
    }
    return Qnil;
}

#script_safeObject Also known as: escape_slash

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



1241
1242
1243
1244
1245
# File 'ext/json/ext/generator/generator.c', line 1241

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.



1253
1254
1255
1256
1257
1258
# File 'ext/json/ext/generator/generator.c', line 1253

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)


1241
1242
1243
1244
1245
# File 'ext/json/ext/generator/generator.c', line 1241

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.



1056
1057
1058
1059
1060
# File 'ext/json/ext/generator/generator.c', line 1056

static VALUE cState_space(VALUE self)
{
    GET_STATE(self);
    return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
}

#space=(space) ⇒ Object

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



1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
# File 'ext/json/ext/generator/generator.c', line 1068

static VALUE cState_space_set(VALUE self, VALUE space)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(space, T_STRING);
    len = RSTRING_LEN(space);
    if (len == 0) {
        if (state->space) {
            ruby_xfree(state->space);
            state->space = NULL;
            state->space_len = 0;
        }
    } else {
        if (state->space) ruby_xfree(state->space);
        state->space = fstrndup(RSTRING_PTR(space), len);
        state->space_len = len;
    }
    return Qnil;
}

#space_beforeObject

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



1093
1094
1095
1096
1097
# File 'ext/json/ext/generator/generator.c', line 1093

static VALUE cState_space_before(VALUE self)
{
    GET_STATE(self);
    return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
}

#space_before=(space_before) ⇒ Object

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



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'ext/json/ext/generator/generator.c', line 1104

static VALUE cState_space_before_set(VALUE self, VALUE space_before)
{
    unsigned long len;
    GET_STATE(self);
    Check_Type(space_before, T_STRING);
    len = RSTRING_LEN(space_before);
    if (len == 0) {
        if (state->space_before) {
            ruby_xfree(state->space_before);
            state->space_before = NULL;
            state->space_before_len = 0;
        }
    } else {
        if (state->space_before) ruby_xfree(state->space_before);
        state->space_before = fstrndup(RSTRING_PTR(space_before), len);
        state->space_before_len = len;
    }
    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.



1268
1269
1270
1271
1272
# File 'ext/json/ext/generator/generator.c', line 1268

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.



1284
1285
1286
1287
1288
1289
# File 'ext/json/ext/generator/generator.c', line 1284

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)


1268
1269
1270
1271
1272
# File 'ext/json/ext/generator/generator.c', line 1268

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/json/ext/generator/state.rb', line 84

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