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.2"

Class Method Summary collapse

Class Method Details

.append(base, key, value) ⇒ 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
# File 'ext/picojson_ruby/picojson_ruby.cpp', line 6

static VALUE rb_picojson_merge(VALUE self, VALUE base, VALUE key, VALUE value) {
  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());
  }

  const char* inserting_key = StringValuePtr(key);
  picojson::object &obj = v.get<picojson::object>();

  switch (TYPE(value)) {
    case T_FIXNUM:
      obj.insert(std::map<std::string, double>::value_type(inserting_key, FIX2INT(value)));
      break;
    case T_STRING:
      obj.insert(std::make_pair(inserting_key, StringValuePtr(value)));
      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));
}