msgpack 0.5.7 → 0.5.8

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ 2013-12-14 version 0.5.8:
3
+
4
+ * Fixed compatibility with Ruby 2.1.0
5
+ * Added :symbolize_keys option to MessagePack.load and Unpacker#initialize
6
+
7
+
2
8
  2013-10-12 version 0.5.7:
3
9
 
4
10
  * Added deserialization support for the new MessagePack spec
data/Rakefile CHANGED
@@ -52,7 +52,7 @@ else
52
52
  end
53
53
  end
54
54
 
55
- task :default => :build
55
+ task :default => [:spec, :build, :doc]
56
56
 
57
57
 
58
58
  ###
data/doclib/msgpack.rb CHANGED
@@ -3,53 +3,75 @@ module MessagePack
3
3
  #
4
4
  # Serializes an object into an IO or String.
5
5
  #
6
- # @overload dump(obj)
6
+ # @overload dump(obj, options={})
7
+ # @param obj [Object] object to be serialized
8
+ # @param options [Hash]
7
9
  # @return [String] serialized data
8
10
  #
9
- # @overload dump(obj, io)
11
+ # @overload dump(obj, io, options={})
12
+ # @param obj [Object] object to be serialized
13
+ # @param io [IO]
14
+ # @param options [Hash]
10
15
  # @return [IO]
11
16
  #
12
- def self.dump(arg)
17
+ # See Packer#initialize for supported options.
18
+ #
19
+ def self.dump(obj)
13
20
  end
14
21
 
15
22
  #
16
23
  # Serializes an object into an IO or String. Alias of dump.
17
24
  #
18
- # @overload dump(obj)
25
+ # @overload pack(obj, options={})
26
+ # @param obj [Object] object to be serialized
27
+ # @param options [Hash]
19
28
  # @return [String] serialized data
20
29
  #
21
- # @overload dump(obj, io)
30
+ # @overload pack(obj, io, options={})
31
+ # @param obj [Object] object to be serialized
32
+ # @param io [IO]
33
+ # @param options [Hash]
22
34
  # @return [IO]
23
35
  #
24
- def self.pack(arg)
36
+ # See Packer#initialize for supported options.
37
+ #
38
+ def self.pack(obj)
25
39
  end
26
40
 
27
41
  #
28
42
  # Deserializes an object from an IO or String.
29
43
  #
30
- # @overload load(string)
44
+ # @overload load(string, options={})
31
45
  # @param string [String] data to deserialize
46
+ # @param options [Hash]
32
47
  #
33
- # @overload load(io)
48
+ # @overload load(io, options={})
34
49
  # @param io [IO]
50
+ # @param options [Hash]
35
51
  #
36
52
  # @return [Object] deserialized object
37
53
  #
38
- def self.load(arg)
54
+ # See Unpacker#initialize for supported options.
55
+ #
56
+ def self.load(src, options={})
39
57
  end
40
58
 
41
59
  #
42
60
  # Deserializes an object from an IO or String. Alias of load.
43
61
  #
44
- # @overload unpack(string)
62
+ # @overload unpack(string, options={})
45
63
  # @param string [String] data to deserialize
64
+ # @param options [Hash]
46
65
  #
47
- # @overload unpack(io)
66
+ # @overload unpack(io, options={})
48
67
  # @param io [IO]
68
+ # @param options [Hash]
49
69
  #
50
70
  # @return [Object] deserialized object
51
71
  #
52
- def self.unpack(arg)
72
+ # See Unpacker#initialize for supported options.
73
+ #
74
+ def self.unpack(src, options={})
53
75
  end
54
76
  end
55
77
 
@@ -18,6 +18,8 @@ module MessagePack
18
18
  # This packer writes serialzied objects into the IO when the internal buffer is filled.
19
19
  # _io_ must respond to write(string) or append(string) method.
20
20
  #
21
+ # See also Buffer#initialize for options.
22
+ #
21
23
  def initialize(*args)
22
24
  end
23
25
 
@@ -17,7 +17,11 @@ module MessagePack
17
17
  # This unpacker reads data from the _io_ to fill the internal buffer.
18
18
  # _io_ must respond to readpartial(length [,string]) or read(length [,string]) method.
19
19
  #
20
- # See Buffer#initialize for supported options.
20
+ # Supported options:
21
+ #
22
+ # * *:symbolize_keys* deserialize keys of Hash objects as Symbol instead of String
23
+ #
24
+ # See also Buffer#initialize for other options.
21
25
  #
22
26
  def initialize(*args)
23
27
  end
@@ -79,19 +79,17 @@ static VALUE Packer_initialize(int argc, VALUE* argv, VALUE self)
79
79
  io = argv[0];
80
80
  options = argv[1];
81
81
  if(rb_type(options) != T_HASH) {
82
- rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
82
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
83
83
  }
84
84
 
85
85
  } else {
86
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
86
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
87
87
  }
88
88
 
89
89
  PACKER(self, pk);
90
- if(io != Qnil || options != Qnil) {
91
- MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
92
- }
90
+ MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
93
91
 
94
- // TODO options
92
+ // TODO MessagePack_Unpacker_initialize and options
95
93
 
96
94
  return self;
97
95
  }
@@ -194,20 +192,31 @@ static VALUE Packer_write_to(VALUE self, VALUE io)
194
192
 
195
193
  VALUE MessagePack_pack(int argc, VALUE* argv)
196
194
  {
197
- // TODO options
198
-
199
195
  VALUE v;
200
196
  VALUE io = Qnil;
197
+ VALUE options = Qnil;
201
198
 
202
- switch(argc) {
203
- case 2:
204
- io = argv[1];
205
- /* pass-through */
206
- case 1:
199
+ if(argc == 1) {
200
+ v = argv[0];
201
+
202
+ } else if(argc == 2) {
203
+ v = argv[0];
204
+ if(rb_type(argv[1]) == T_HASH) {
205
+ options = argv[1];
206
+ } else {
207
+ io = argv[1];
208
+ }
209
+
210
+ } else if(argc == 3) {
207
211
  v = argv[0];
208
- break;
209
- default:
210
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
212
+ io = argv[1];
213
+ options = argv[2];
214
+ if(rb_type(options) != T_HASH) {
215
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
216
+ }
217
+
218
+ } else {
219
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc);
211
220
  }
212
221
 
213
222
  VALUE self = Packer_alloc(cMessagePack_Packer);
@@ -215,9 +224,8 @@ VALUE MessagePack_pack(int argc, VALUE* argv)
215
224
  //msgpack_packer_reset(s_packer);
216
225
  //msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));
217
226
 
218
- if(io != Qnil) {
219
- MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, Qnil);
220
- }
227
+ MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
228
+ // TODO MessagePack_Unpacker_initialize and options
221
229
 
222
230
  msgpack_packer_write_value(pk, v);
223
231
 
@@ -607,7 +607,12 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
607
607
  top->type = STACK_TYPE_MAP_VALUE;
608
608
  break;
609
609
  case STACK_TYPE_MAP_VALUE:
610
- rb_hash_aset(top->object, top->key, uk->last_object);
610
+ if(uk->symbolize_keys && rb_type(top->key) == T_STRING) {
611
+ /* here uses rb_intern_str instead of rb_intern so that Ruby VM can GC unused symbols */
612
+ rb_hash_aset(top->object, ID2SYM(rb_intern_str(top->key)), uk->last_object);
613
+ } else {
614
+ rb_hash_aset(top->object, top->key, uk->last_object);
615
+ }
611
616
  top->type = STACK_TYPE_MAP_KEY;
612
617
  break;
613
618
  }
@@ -57,6 +57,9 @@ struct msgpack_unpacker_t {
57
57
  size_t reading_raw_remaining;
58
58
 
59
59
  VALUE buffer_ref;
60
+
61
+ /* options */
62
+ bool symbolize_keys;
60
63
  };
61
64
 
62
65
  #define UNPACKER_BUFFER_(uk) (&(uk)->buffer)
@@ -83,6 +86,11 @@ void msgpack_unpacker_mark(msgpack_unpacker_t* uk);
83
86
 
84
87
  void msgpack_unpacker_reset(msgpack_unpacker_t* uk);
85
88
 
89
+ static inline void msgpack_unpacker_set_symbolized_keys(msgpack_unpacker_t* uk, bool enable)
90
+ {
91
+ uk->symbolize_keys = enable;
92
+ }
93
+
86
94
 
87
95
  /* error codes */
88
96
  #define PRIMITIVE_CONTAINER_START 1
@@ -70,6 +70,9 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
70
70
  VALUE v = argv[0];
71
71
  if(rb_type(v) == T_HASH) {
72
72
  options = v;
73
+ if(rb_type(options) != T_HASH) {
74
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
75
+ }
73
76
  } else {
74
77
  io = v;
75
78
  }
@@ -78,7 +81,7 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
78
81
  io = argv[0];
79
82
  options = argv[1];
80
83
  if(rb_type(options) != T_HASH) {
81
- rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
84
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
82
85
  }
83
86
 
84
87
  } else {
@@ -86,15 +89,24 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
86
89
  }
87
90
 
88
91
  UNPACKER(self, uk);
89
- if(io != Qnil || options != Qnil) {
90
- MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, options);
91
- }
92
92
 
93
- // TODO options
93
+ MessagePack_Unpacker_initialize(uk, io, options);
94
94
 
95
95
  return self;
96
96
  }
97
97
 
98
+ void MessagePack_Unpacker_initialize(msgpack_unpacker_t* uk, VALUE io, VALUE options)
99
+ {
100
+ MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, options);
101
+
102
+ if(options != Qnil) {
103
+ VALUE v;
104
+
105
+ v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
106
+ msgpack_unpacker_set_symbolized_keys(uk, RTEST(v));
107
+ }
108
+ }
109
+
98
110
  static void raise_unpacker_error(int r)
99
111
  {
100
112
  switch(r) {
@@ -288,8 +300,15 @@ static VALUE Unpacker_reset(VALUE self)
288
300
  VALUE MessagePack_unpack(int argc, VALUE* argv)
289
301
  {
290
302
  VALUE src;
303
+ VALUE options = Qnil;
291
304
 
292
305
  switch(argc) {
306
+ case 2:
307
+ options = argv[1];
308
+ if(rb_type(options) != T_HASH) {
309
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
310
+ }
311
+ /* pass-through */
293
312
  case 1:
294
313
  src = argv[0];
295
314
  break;
@@ -297,27 +316,19 @@ VALUE MessagePack_unpack(int argc, VALUE* argv)
297
316
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
298
317
  }
299
318
 
300
- VALUE io = Qnil;
301
- if(rb_type(src) != T_STRING) {
302
- io = src;
303
- src = Qnil;
304
- }
305
-
306
319
  VALUE self = Unpacker_alloc(cMessagePack_Unpacker);
307
320
  UNPACKER(self, uk);
308
321
  //msgpack_unpacker_reset(s_unpacker);
309
322
  //msgpack_buffer_reset_io(UNPACKER_BUFFER_(s_unpacker));
310
323
 
311
- /* prefer reference than copying */
324
+ /* prefer reference than copying; see MessagePack_Unpacker_module_init */
312
325
  msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(uk), 0);
313
326
 
314
- if(io != Qnil) {
315
- MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, Qnil);
316
- }
317
-
318
- if(src != Qnil) {
319
- /* prefer reference than copying; see MessagePack_Unpacker_module_init */
327
+ if(rb_type(src) == T_STRING) {
328
+ MessagePack_Unpacker_initialize(uk, Qnil, options);
320
329
  msgpack_buffer_append_string(UNPACKER_BUFFER_(uk), src);
330
+ } else {
331
+ MessagePack_Unpacker_initialize(uk, src, options);
321
332
  }
322
333
 
323
334
  int r = msgpack_unpacker_read(uk, 0);
@@ -26,5 +26,7 @@ void MessagePack_Unpacker_module_init(VALUE mMessagePack);
26
26
 
27
27
  VALUE MessagePack_unpack(int argc, VALUE* argv);
28
28
 
29
+ void MessagePack_Unpacker_initialize(msgpack_unpacker_t* uk, VALUE io, VALUE options);
30
+
29
31
  #endif
30
32
 
data/lib/msgpack.rb CHANGED
@@ -1,9 +1,6 @@
1
- here = File.expand_path(File.dirname(__FILE__))
2
- require File.join(here, 'msgpack', 'version')
1
+ require "msgpack/version"
3
2
  begin
4
- m = /(\d+.\d+)/.match(RUBY_VERSION)
5
- ver = m[1]
6
- require File.join(here, 'msgpack', ver, 'msgpack')
3
+ require "msgpack/#{RUBY_VERSION[/\d+.\d+/]}/msgpack"
7
4
  rescue LoadError
8
- require File.join(here, 'msgpack', 'msgpack')
5
+ require "msgpack/msgpack"
9
6
  end
@@ -1,3 +1,3 @@
1
1
  module MessagePack
2
- VERSION = "0.5.7"
2
+ VERSION = "0.5.8"
3
3
  end
@@ -201,7 +201,7 @@ describe Buffer do
201
201
  fragments.each {|s|
202
202
  lambda {
203
203
  b.skip_all(s.size)
204
- }.should_not raise_error(EOFError)
204
+ }.should_not raise_error
205
205
  }
206
206
  b.empty?.should == true
207
207
  lambda {
@@ -230,6 +230,18 @@ describe Unpacker do
230
230
  parsed.should == true
231
231
  end
232
232
 
233
+ it 'MessagePack.unpack symbolize_keys' do
234
+ symbolized_hash = {:a => 'b', :c => 'd'}
235
+ MessagePack.load(MessagePack.pack(symbolized_hash), symbolize_keys: true).should == symbolized_hash
236
+ MessagePack.unpack(MessagePack.pack(symbolized_hash), symbolize_keys: true).should == symbolized_hash
237
+ end
238
+
239
+ it 'Unpacker#unpack symbolize_keys' do
240
+ unpacker = Unpacker.new(:symbolize_keys => true)
241
+ symbolized_hash = {:a => 'b', :c => 'd'}
242
+ unpacker.feed(MessagePack.pack(symbolized_hash)).read.should == symbolized_hash
243
+ end
244
+
233
245
  it "msgpack str 8 type" do
234
246
  MessagePack.unpack([0xd9, 0x00].pack('C*')).should == ""
235
247
  MessagePack.unpack([0xd9, 0x01].pack('C*') + 'a').should == "a"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.5.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-13 00:00:00.000000000 Z
12
+ date: 2013-12-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler