class JSON::Ext::Generator::State
公共类方法
源码
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);
}
}
从 opts 创建一个 State 对象,opts 应该是一个 Hash,用于创建一个由 opts 配置的新 State 实例;或者其他任何值,用于创建一个未配置的实例。如果 opts 是一个 State 对象,则直接返回它。
源码
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);
}
源码
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
rb_warn("The json gem extension was loaded with the stdlib ruby code. You should upgrade rubygems with `gem update --system`");
return self;
}
源码
# File ext/json/lib/json/ext/generator/state.rb, line 25 def initialize(opts = nil) if opts && !opts.empty? configure(opts) end end
实例化一个新的 State 对象,由 opts 配置。
opts 可以有以下键:
-
indent: 用于缩进级别的字符串(默认值:""),
-
space: 放在 : 或 , 分隔符之后的字符串(默认值:""),
-
space_before: 放在 : 对分隔符之前的字符串(默认值:""),
-
object_nl: 放在
JSON对象末尾的字符串(默认值:""), -
array_nl: 放在
JSON数组末尾的字符串(默认值:""), -
allow_nan: 如果应生成 NaN、Infinity 和 -Infinity,则为 true;否则,如果遇到这些值,则抛出异常。此选项默认为 false。
-
ascii_only: 如果只应生成 ASCII 字符,则为 true。此选项默认为 false。
-
buffer_initial_length: 设置生成器内部缓冲区的初始长度。
公共实例方法
源码
# File ext/json/lib/json/ext/generator/state.rb, line 94 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 返回的值。
源码
# File ext/json/lib/json/ext/generator/state.rb, line 106 def []=(name, value) if respond_to?(name_writer = "#{name}=") __send__ name_writer, value else instance_variable_set "@#{name}", value end end
将属性 name 设置为 value。
源码
static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->allow_nan = RTEST(enable);
return Qnil;
}
这设置是否序列化 NaN、Infinity 和 -Infinity
源码
static VALUE cState_allow_nan_p(VALUE self)
{
GET_STATE(self);
return state->allow_nan ? Qtrue : Qfalse;
}
如果应生成 NaN、Infinity 和 -Infinity,则返回 true;否则返回 false。
源码
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));
}
此字符串放在包含 JSON 数组的行的末尾。
源码
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;
}
此字符串放在包含 JSON 数组的行的末尾。
源码
static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->ascii_only = RTEST(enable);
return Qnil;
}
这设置是否只应生成 ASCII 字符。
源码
static VALUE cState_ascii_only_p(VALUE self)
{
GET_STATE(self);
return state->ascii_only ? Qtrue : Qfalse;
}
如果只应生成 ASCII 字符,则返回 true。否则返回 false。
源码
static VALUE cState_buffer_initial_length(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->buffer_initial_length);
}
此整数返回缓冲区的当前初始长度。
源码
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;
}
如果 length > 0,则将缓冲区的初始长度设置为 length,否则其值不会更改。
源码
static VALUE cState_check_circular_p(VALUE self)
{
GET_STATE(self);
return state->max_nesting ? Qtrue : Qfalse;
}
如果应检查循环数据结构,则返回 true;否则返回 false。
源码
# File ext/json/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
源码
static VALUE cState_depth(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->depth);
}
此整数返回数据结构嵌套的当前深度。
源码
static VALUE cState_depth_set(VALUE self, VALUE depth)
{
GET_STATE(self);
state->depth = long_config(depth);
return Qnil;
}
这设置生成的 JSON 中数据结构嵌套的最大级别为整数 depth,如果应检查没有最大值,则 max_nesting = 0。
源码
# File ext/json/lib/json/ext/generator/state.rb, line 57 def generate(obj, io = nil) _generate(obj, io) end
从对象 obj 生成有效的 JSON 文档并返回结果。如果无法创建有效的 JSON 文档,此方法会引发 GeneratorError 异常。
源码
static VALUE cState_indent(VALUE self)
{
GET_STATE(self);
return state->indent ? state->indent : rb_str_freeze(rb_utf8_str_new("", 0));
}
返回用于在 JSON 文本中缩进级别的字符串。
源码
static VALUE cState_indent_set(VALUE self, VALUE indent)
{
GET_STATE(self);
RB_OBJ_WRITE(self, &state->indent, string_config(indent));
return Qnil;
}
设置用于在 JSON 文本中缩进级别的字符串。
源码
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;
return obj;
}
如果可以复制/克隆 orig,则从此对象初始化此对象并返回它。
源码
static VALUE cState_max_nesting(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->max_nesting);
}
此整数返回生成的 JSON 中数据结构嵌套的最大级别,如果未检查最大值,则 max_nesting = 0。
源码
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
GET_STATE(self);
state->max_nesting = long_config(depth);
return Qnil;
}
这设置生成的 JSON 中数据结构嵌套的最大级别为整数 depth,如果应检查没有最大值,则 max_nesting = 0。
源码
源码
源码
static VALUE cState_script_safe(VALUE self)
{
GET_STATE(self);
return state->script_safe ? Qtrue : Qfalse;
}
如果此布尔值为 true,则正斜杠将在 json 输出中转义。
源码
static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->script_safe = RTEST(enable);
return Qnil;
}
这设置是否在 json 输出中转义正斜杠。
源码
static VALUE cState_space(VALUE self)
{
GET_STATE(self);
return state->space ? state->space : rb_str_freeze(rb_utf8_str_new("", 0));
}
返回用于在 JSON 字符串中的标记之间插入空格的字符串。
源码
static VALUE cState_space_set(VALUE self, VALUE space)
{
GET_STATE(self);
RB_OBJ_WRITE(self, &state->space, string_config(space));
return Qnil;
}
将 space 设置为用于在 JSON 字符串中的标记之间插入空格的字符串。
源码
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));
}
返回用于在 JSON 对象中在“:”之前插入空格的字符串。
源码
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;
}
设置用于在 JSON 对象中在“:”之前插入空格的字符串。
源码
static VALUE cState_strict(VALUE self)
{
GET_STATE(self);
return state->strict ? Qtrue : Qfalse;
}
如果此布尔值为 false,则 JSON 格式不支持的类型将序列化为字符串。如果此布尔值为 true,则 JSON 格式不支持的类型将引发 JSON::GeneratorError。
源码
static VALUE cState_strict_set(VALUE self, VALUE enable)
{
GET_STATE(self);
state->strict = RTEST(enable);
return Qnil;
}
这设置是否将 JSON 格式不支持的类型序列化为字符串。如果此布尔值为 false,则 JSON 格式不支持的类型将序列化为字符串。如果此布尔值为 true,则 JSON 格式不支持的类型将引发 JSON::GeneratorError。
如果此布尔值为 false,则 JSON 格式不支持的类型将序列化为字符串。如果此布尔值为 true,则 JSON 格式不支持的类型将引发 JSON::GeneratorError。
源码
# File ext/json/lib/json/ext/generator/state.rb, line 65 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
以哈希形式返回配置实例变量,可以将其传递给 configure 方法。
私有实例方法
源码
static VALUE cState_configure(VALUE self, VALUE opts)
{
GET_STATE(self);
configure_state(state, opts);
return self;
}
源码
static VALUE cState_generate(VALUE self, VALUE obj, VALUE io)
{
VALUE result = cState_partial_generate(self, obj, generate_json, io);
GET_STATE(self);
(void)state;
return result;
}