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

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) new(opts = {})

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: ''),

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



# File 'ext/json/generator/generator.c'

static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE opts;
    GET_STATE(self);
    state->max_nesting = 19;
    rb_scan_args(argc, argv, "01", &opts);
    if (!NIL_P(opts)) cState_configure(self, opts);
    return self;
}

Class Method Details

+ (Object) from_state(opts)

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.



# File 'ext/json/generator/generator.c'

static VALUE cState_from_state_s(VALUE self, VALUE opts)
{
if (rb_obj_is_kind_of(opts, self)) {
    return opts;
}

Instance Method Details

- (Object) [](name)

Return the value returned by method name.



# File 'ext/json/generator/generator.c'

static VALUE cState_aref(VALUE self, VALUE name)
{
GET_STATE(self);
if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
    return rb_funcall(self, i_send, 1, name);
}

- (Object) allow_nan?

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



# File 'ext/json/generator/generator.c'

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

- (Object) array_nl

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



# File 'ext/json/generator/generator.c'

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

- (Object) array_nl=(array_nl)

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



# File 'ext/json/generator/generator.c'

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

- (Object) ascii_only?

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



# File 'ext/json/generator/generator.c'

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

- (Object) check_circular?

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



# File 'ext/json/generator/generator.c'

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

- (Object) configure(opts)

Configure this State instance with the Hash opts, and return itself.



# File 'ext/json/generator/generator.c'

static VALUE cState_configure(VALUE self, VALUE opts)
{
VALUE tmp;
GET_STATE(self);
tmp = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
if (NIL_P(tmp)) {
    rb_raise(rb_eArgError, "opts has to be hash like or convertable into a hash");
}

- (Object) depth

This integer returns the current depth of data structure nesting.



# File 'ext/json/generator/generator.c'

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

- (Object) depth=(depth)

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.



# File 'ext/json/generator/generator.c'

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

- (Object) generate(obj)

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.



# File 'ext/json/generator/generator.c'

static VALUE cState_generate(VALUE self, VALUE obj)
{
VALUE result = cState_partial_generate(self, obj);
VALUE re, args[2];
args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\}

- (Object) indent

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



# File 'ext/json/generator/generator.c'

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

- (Object) indent=(indent)

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



# File 'ext/json/generator/generator.c'

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

- (Object) initialize_copy(orig)

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



# File 'ext/json/generator/generator.c'

static VALUE cState_init_copy(VALUE obj, VALUE orig)
{
    JSON_Generator_State *objState, *origState;

    Data_Get_Struct(obj, JSON_Generator_State, objState);
    Data_Get_Struct(orig, JSON_Generator_State, 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);
    if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
    if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
    if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
    return obj;
}

- (Object) max_nesting

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



# File 'ext/json/generator/generator.c'

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

- (Object) max_nesting=(depth)

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.



# File 'ext/json/generator/generator.c'

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

- (Object) object_nl

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



# File 'ext/json/generator/generator.c'

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

- (Object) object_nl=(object_nl)

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



# File 'ext/json/generator/generator.c'

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

- (Object) space

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



# File 'ext/json/generator/generator.c'

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

- (Object) space=(space)

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



# File 'ext/json/generator/generator.c'

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

- (Object) space_before

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



# File 'ext/json/generator/generator.c'

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

- (Object) space_before=(space_before)

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



# File 'ext/json/generator/generator.c'

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

- (Object) to_h

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



# File 'ext/json/generator/generator.c'

static VALUE cState_to_h(VALUE self)
{
    VALUE result = rb_hash_new();
    GET_STATE(self);
    rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
    rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
    rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
    rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
    rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
    rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
    rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
    rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
    rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
    return result;
}