picojson_ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 137d421ec982c80068225b39ea31706d95098a51
4
- data.tar.gz: 86986ee4df601fef4bd285e69dc2070912963880
3
+ metadata.gz: 9ebf8f8e1be7d3af34e13747ee921ed88f0fa075
4
+ data.tar.gz: 6c1adc24d16457e375d28a387200eed0b7e64c27
5
5
  SHA512:
6
- metadata.gz: f340245f8205f688e265fa8973e7ad48f4a1ba5d087e7debb954483957f63661cef078a4b0ba273ec17f2ad2e71ac5ff39b223a59edf81ab3b6768492c84bc4c
7
- data.tar.gz: 32d3658da12a13a56eebbad841542c537fd243126d1527d96c3345641faa509ed0bd8e30aae7c830642c7cf8b110ddf73a97ba7b11c119d67b1a09b8b7d26298
6
+ metadata.gz: 8de85cb0c6155df52b06b1e8a1028f5b0e04278fed238265abc7026ce6cc85b50e0be6fd98b3effc4eea88544b735a800d163b03e73d9fb460c9942d4ac30f84
7
+ data.tar.gz: 236fdcd19eed20938adf0ef769ae6e63bf7daf95f20b7647ed864d1baab6fee9543da1bca7a72c40e01088dd02e7e38d3d7d0d7ed63b32d5457b21fd023cb848
@@ -3,7 +3,7 @@
3
3
 
4
4
  VALUE rb_cPicojsonRuby;
5
5
 
6
- static VALUE rb_picojson_merge(VALUE self, VALUE base, VALUE key, VALUE value) {
6
+ static VALUE rb_picojson_merge(VALUE self, VALUE base, VALUE addingObj) {
7
7
  picojson::value v;
8
8
  std::string err;
9
9
  const char* json = StringValuePtr(base);
@@ -12,21 +12,43 @@ static VALUE rb_picojson_merge(VALUE self, VALUE base, VALUE key, VALUE value) {
12
12
  rb_raise(rb_eTypeError, err.c_str());
13
13
  }
14
14
 
15
- const char* inserting_key = StringValuePtr(key);
16
15
  picojson::object &obj = v.get<picojson::object>();
17
-
18
- switch (TYPE(value)) {
19
- case T_FIXNUM:
20
- obj.insert(std::map<std::string, double>::value_type(inserting_key, FIX2INT(value)));
21
- break;
22
- case T_STRING:
23
- obj.insert(std::make_pair(inserting_key, StringValuePtr(value)));
16
+ switch (TYPE(addingObj)) {
17
+ case T_HASH:
24
18
  break;
25
19
  default:
26
20
  /* 例外を発生させる */
27
21
  rb_raise(rb_eTypeError, "not valid value");
28
22
  break;
29
23
  }
24
+ VALUE ary = rb_funcall(addingObj, rb_intern("to_a"), 0);
25
+ if (TYPE(ary) != T_ARRAY) {
26
+ rb_raise(rb_eTypeError, "Can not execute to_a");
27
+ }
28
+
29
+ long i;
30
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
31
+ VALUE entry = rb_ary_entry(ary, i);
32
+ if (TYPE(entry) != T_ARRAY || RARRAY_LEN(entry) != 2) {
33
+ rb_raise(rb_eTypeError, "Not A Hash");
34
+ }
35
+ VALUE key = rb_ary_entry(entry, 0);
36
+ VALUE val = rb_ary_entry(entry, 1);
37
+ const char* inserting_key = StringValuePtr(key);
38
+ switch (TYPE(val)) {
39
+ case T_FIXNUM:
40
+ obj.insert(std::map<std::string, double>::value_type(inserting_key, FIX2INT(val)));
41
+ break;
42
+ case T_STRING:
43
+ obj.insert(std::make_pair(inserting_key, StringValuePtr(val)));
44
+ break;
45
+ default:
46
+ /* 例外を発生させる */
47
+ rb_raise(rb_eTypeError, "not valid value");
48
+ break;
49
+ }
50
+ }
51
+
30
52
  std::string new_json = v.serialize();
31
53
  const char* new_json_char = new_json.c_str();
32
54
  return rb_str_new(new_json_char, strlen(new_json_char));
@@ -36,5 +58,5 @@ extern "C" void
36
58
  Init_picojson_ruby(void)
37
59
  {
38
60
  rb_cPicojsonRuby = rb_define_class("PicojsonRuby", rb_cObject);
39
- rb_define_singleton_method(rb_cPicojsonRuby, "append", RUBY_METHOD_FUNC(rb_picojson_merge), 3);
61
+ rb_define_singleton_method(rb_cPicojsonRuby, "append", RUBY_METHOD_FUNC(rb_picojson_merge), 2);
40
62
  }
@@ -1,3 +1,3 @@
1
1
  class PicojsonRuby
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -4,23 +4,23 @@ require "picojson_ruby"
4
4
  describe PicojsonRuby do
5
5
  describe ".append" do
6
6
  context 'valid appending string value' do
7
- subject { PicojsonRuby.append("{}", "test", "test") }
7
+ subject { PicojsonRuby.append("{}", {"test" => "test"}) }
8
8
  it { expect(subject).to eq "{\"test\":\"test\"}" }
9
9
  end
10
10
  context 'valid appending string value 2' do
11
- subject { PicojsonRuby.append("{}", "test", "1234567890") }
11
+ subject { PicojsonRuby.append("{}", {"test" => "1234567890"}) }
12
12
  it { expect(subject).to eq "{\"test\":\"1234567890\"}" }
13
13
  end
14
14
  context 'valid appending string fixnum' do
15
- subject { PicojsonRuby.append("{}", "test", 1234567890) }
15
+ subject { PicojsonRuby.append("{}", {"test" => 1234567890}) }
16
16
  it { expect(subject).to eq "{\"test\":1234567890}" }
17
17
  end
18
18
  context 'invalid with Array' do
19
- subject { PicojsonRuby.append("{}", "test", []) }
19
+ subject { PicojsonRuby.append("{}", "test") }
20
20
  it { expect { subject }.to raise_error TypeError }
21
21
  end
22
22
  context 'invalid with invalid json' do
23
- subject { PicojsonRuby.append("{test]", "test", "test") }
23
+ subject { PicojsonRuby.append("{hoge]", "test") }
24
24
  it { expect { subject }.to raise_error TypeError }
25
25
  end
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picojson_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - SpringMT
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-26 00:00:00.000000000 Z
11
+ date: 2015-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,4 +102,3 @@ summary: Memory saving JSON merge.
102
102
  test_files:
103
103
  - spec/picojson_ruby_spec.rb
104
104
  - spec/spec_helper.rb
105
- has_rdoc: