Class: PicojsonRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/picojson_ruby.rb,
lib/picojson_ruby/version.rb,
ext/picojson_ruby/picojson_ruby.cpp

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.append(base, addingObj) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'ext/picojson_ruby/picojson_ruby.cpp', line 6

static VALUE rb_picojson_merge(VALUE self, VALUE base, VALUE addingObj) {
  picojson::value v;
  std::string err;
  const char* json = StringValuePtr(base);
  picojson::parse(v, json, json + strlen(json), &err);
  if (!err.empty()) {
    rb_raise(rb_eTypeError, err.c_str());
  }

  picojson::object &obj = v.get<picojson::object>();
  switch (TYPE(addingObj)) {
    case T_HASH:
      break;
    default:
      /* 例外を発生させる */
      rb_raise(rb_eTypeError, "not valid value");
      break;
  }
  VALUE ary = rb_funcall(addingObj, rb_intern("to_a"), 0);
  if (TYPE(ary) != T_ARRAY) {
    rb_raise(rb_eTypeError, "Can not execute to_a");
  }

  long i;
  for (i = 0; i < RARRAY_LEN(ary); ++i) {
    VALUE entry = rb_ary_entry(ary, i);
    if (TYPE(entry) != T_ARRAY || RARRAY_LEN(entry) != 2) {
      rb_raise(rb_eTypeError, "Not A Hash");
    }
    VALUE key = rb_ary_entry(entry, 0);
    VALUE val = rb_ary_entry(entry, 1);
    const char* inserting_key = StringValuePtr(key);
    switch (TYPE(val)) {
      case T_FIXNUM:
        obj.insert(std::map<std::string, double>::value_type(inserting_key, FIX2INT(val)));
        break;
      case T_STRING:
        obj.insert(std::make_pair(inserting_key, StringValuePtr(val)));
        break;
      default:
        /* 例外を発生させる */
        rb_raise(rb_eTypeError, "not valid value");
        break;
    }
  }

  std::string new_json = v.serialize();
  const char* new_json_char = new_json.c_str();
  return rb_str_new(new_json_char, strlen(new_json_char));
}