Sha256: 99d54a20959e74f81eee7e802b200e248e6edcbe2bab45d62ccb63bb34ed9641

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

#include "enumerable.h"

static VALUE cCircularDatastructure;

/**
 * Returns a JSON string for this object.
 * _state_ is a JSON::State object, that can also be used to configure the
 * produced JSON string output further.
 * _depth_ is used to find out nesting depth, to indent accordingly.
 */
static VALUE to_json(argc, argv, self)
  int argc;
  VALUE *argv;
  VALUE self;
{
  VALUE state, depth;
  rb_scan_args(argc, argv, "02", &state, &depth);
  if(depth == Qnil) depth = INT2NUM(0);

  state = rb_json_from_state(rb_cJsonState, state);
  return json_check_circular_and_transform(self, state, depth);
}

static VALUE json_check_circular_and_transform(self, state, depth)
  VALUE self, state, depth;
{
  VALUE call_args = rb_ary_new3(3, self, state, depth);
  return rb_ensure(json_check_circular_and_transform_call, call_args, json_check_circular_and_transform_ensure, call_args);
}

static VALUE json_check_circular_and_transform_call(args)
  VALUE args;
{
  VALUE self = rb_ary_entry(args, 0);
  VALUE state = rb_ary_entry(args, 1);
  VALUE depth = rb_ary_entry(args, 2);
  if(state != Qnil) {
    if(rb_json_state_seen(state, self) == Qtrue) {
      rb_raise(
        cCircularDatastructure,
        "circular data structures not supported!"
      );
    }
    rb_json_state_remember(state, self);
  }
  return json_transform(self, state, depth);
}

static VALUE json_check_circular_and_transform_ensure(args)
  VALUE args;
{
  VALUE self = rb_ary_entry(args, 0);
  VALUE state = rb_ary_entry(args, 1);
  if(state != Qnil) {
    rb_json_state_forget(state, self);
  }
}

static VALUE new_line(result, object_nl)
  VALUE result, object_nl;
{
  if(object_nl != Qnil) rb_str_append(result, object_nl);
}

static VALUE json_shift(json, state, depth)
  VALUE json, state, depth;
{
  rb_json_state_indent(state, json, NUM2LONG(depth) + 1);
  return Qnil;
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fjson-0.1.1 ext/extensions/enumerable/enumerable.c
fjson-0.1.2 ext/extensions/enumerable/enumerable.c