Sha256: f7097baed6b9081edbd9cbf28fc86ccc76cb39d40d4c64cdeb61f33cccb2d8ba

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

#include "array_ext.h"
#include "enumerable.c"

static VALUE mJSON;
static VALUE cCircularDatastructure;

void Init_array_ext() {
  mJSON = rb_const_get(rb_cObject, rb_intern("JSON"));
  rb_cJsonState = rb_const_get(mJSON, rb_intern("State"));
  cCircularDatastructure = rb_const_get(mJSON, rb_intern("CircularDatastructure"));
  rb_define_method(rb_cArray, "to_json", (VALUE(*)(ANYARGS)) &to_json, -1);
}

/** private **/

static VALUE json_transform(self, state, depth)
  VALUE self, state, depth;
{
  VALUE delim = rb_str_new2(",");
  VALUE array_nl = Qnil;
  if(state != Qnil) array_nl = rb_funcall(state, rb_intern("array_nl"), 0);
  new_line(delim, array_nl);

  VALUE json = rb_str_new2("[");
  new_line(json, array_nl);

  process_internal_json(self, json, state, depth, delim, array_nl);

  new_line(json, array_nl);
  if(rb_json_should_shift(state, array_nl) == Qtrue) json_shift(json, state, depth);
  rb_str_cat2(json, "]");
  return json;
}

static VALUE process_internal_json(self, json, state, depth, delim, array_nl)
  VALUE self, json, state, depth, delim, array_nl;
{
  VALUE next_depth = LONG2NUM(NUM2LONG(depth) + 1);

  VALUE current_value;
  int i;
  for(i = 0; i < RARRAY(self)->len; i++) {
    if(i > 0) {
      rb_str_concat(json, delim);
    }
    current_value = rb_ary_entry(self, i);
    if(rb_json_should_shift(state, array_nl) == Qtrue) json_shift(json, state, depth);
    rb_str_append(json, rb_to_json(current_value, state, depth));
  }
}

Version data entries

2 entries across 2 versions & 1 rubygems

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