msgpack 1.2.6 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yaml +56 -0
- data/.gitignore +3 -1
- data/.rubocop.yml +4 -1
- data/ChangeLog +59 -0
- data/Gemfile +3 -0
- data/README.md +242 -0
- data/Rakefile +3 -8
- data/doclib/msgpack/factory.rb +1 -0
- data/doclib/msgpack/packer.rb +20 -0
- data/doclib/msgpack/time.rb +22 -0
- data/doclib/msgpack/timestamp.rb +44 -0
- data/doclib/msgpack.rb +2 -2
- data/ext/java/org/msgpack/jruby/Buffer.java +21 -16
- data/ext/java/org/msgpack/jruby/Decoder.java +29 -10
- data/ext/java/org/msgpack/jruby/Encoder.java +38 -19
- data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +9 -9
- data/ext/java/org/msgpack/jruby/ExtensionValue.java +5 -8
- data/ext/java/org/msgpack/jruby/Factory.java +8 -3
- data/ext/java/org/msgpack/jruby/Packer.java +31 -8
- data/ext/java/org/msgpack/jruby/Unpacker.java +40 -27
- data/ext/msgpack/buffer.c +4 -16
- data/ext/msgpack/buffer.h +60 -5
- data/ext/msgpack/compat.h +1 -12
- data/ext/msgpack/extconf.rb +39 -7
- data/ext/msgpack/factory_class.c +10 -5
- data/ext/msgpack/packer.c +18 -5
- data/ext/msgpack/packer.h +0 -16
- data/ext/msgpack/packer_class.c +21 -9
- data/ext/msgpack/packer_ext_registry.c +0 -22
- data/ext/msgpack/unpacker.c +41 -49
- data/ext/msgpack/unpacker.h +8 -0
- data/ext/msgpack/unpacker_class.c +23 -13
- data/lib/msgpack/symbol.rb +14 -4
- data/lib/msgpack/time.rb +29 -0
- data/lib/msgpack/timestamp.rb +76 -0
- data/lib/msgpack/version.rb +4 -7
- data/lib/msgpack.rb +8 -10
- data/msgpack.gemspec +3 -7
- data/spec/cruby/buffer_spec.rb +6 -1
- data/spec/factory_spec.rb +17 -0
- data/spec/msgpack_spec.rb +44 -1
- data/spec/packer_spec.rb +54 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/timestamp_spec.rb +161 -0
- data/spec/unpacker_spec.rb +113 -1
- metadata +19 -51
- data/.travis.yml +0 -41
- data/README.rdoc +0 -201
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c57015d343570a2bccf9f0cb8877bf7e0a83cffa478fa4a99816dc1bb4e5f3d0
|
4
|
+
data.tar.gz: 13e5f99e0b73c98071e8b0bb8bf841a52283ef9f1502ea578732a4baf1f4d9de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c048635dc9d9451ace0aff991a3ea92ccd7bbc397cfa4431c6428a07149fcdf9b31c1b1cd9392aca806669839236b26f90234cc99b8232061b3441707adc409
|
7
|
+
data.tar.gz: 63af2e6f9f11f8a96115d81aa9d8754220b44cdda069c0094ac5c635c01df75e84ac1e76dd3c2e9e81c7b3d6f5a1ba66f9006c620ec30ef047bfc7c857ffec0d
|
@@ -0,0 +1,56 @@
|
|
1
|
+
name: ci
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches: '*'
|
6
|
+
push:
|
7
|
+
branches:
|
8
|
+
- master
|
9
|
+
- main
|
10
|
+
- 'release-*'
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
mri:
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
os: [ubuntu, macos, windows]
|
17
|
+
ruby: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1']
|
18
|
+
runs-on: ${{ matrix.os }}-latest
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v2
|
21
|
+
- uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby }}
|
24
|
+
bundler-cache: true # 'bundle install' and cache
|
25
|
+
- run: bundle exec rake
|
26
|
+
|
27
|
+
jruby:
|
28
|
+
strategy:
|
29
|
+
matrix:
|
30
|
+
os: [ubuntu]
|
31
|
+
# TODO: update to 9.3.3.0 once supported
|
32
|
+
# https://github.com/ruby/setup-ruby#supported-versions
|
33
|
+
ruby: ['jruby-9.2.19.0', 'jruby-9.3.2.0']
|
34
|
+
runs-on: ${{ matrix.os }}-latest
|
35
|
+
steps:
|
36
|
+
- uses: actions/checkout@v2
|
37
|
+
- uses: ruby/setup-ruby@v1
|
38
|
+
with:
|
39
|
+
ruby-version: ${{ matrix.ruby }}
|
40
|
+
bundler-cache: true # 'bundle install' and cache
|
41
|
+
- run: bundle exec rake
|
42
|
+
|
43
|
+
head-versions:
|
44
|
+
continue-on-error: true
|
45
|
+
strategy:
|
46
|
+
matrix:
|
47
|
+
os: [ubuntu]
|
48
|
+
ruby: ['ruby-head', 'jruby-head', 'truffleruby']
|
49
|
+
runs-on: ${{ matrix.os }}-latest
|
50
|
+
steps:
|
51
|
+
- uses: actions/checkout@v2
|
52
|
+
- uses: ruby/setup-ruby@v1
|
53
|
+
with:
|
54
|
+
ruby-version: ${{ matrix.ruby }}
|
55
|
+
bundler-cache: true # 'bundle install' and cache
|
56
|
+
- run: bundle exec rake || echo "failed, but ignore it"
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -5,6 +5,9 @@
|
|
5
5
|
# Note that changes in the inspected code, or installation of new
|
6
6
|
# versions of RuboCop, may require this file to be generated again.
|
7
7
|
|
8
|
+
AllCops:
|
9
|
+
TargetRubyVersion: 2.4
|
10
|
+
|
8
11
|
# Offense count: 3
|
9
12
|
Lint/AmbiguousOperator:
|
10
13
|
Enabled: false
|
@@ -15,7 +18,7 @@ Lint/AssignmentInCondition:
|
|
15
18
|
Enabled: false
|
16
19
|
|
17
20
|
# Offense count: 1
|
18
|
-
|
21
|
+
Security/Eval:
|
19
22
|
Enabled: false
|
20
23
|
|
21
24
|
# Offense count: 3
|
data/ChangeLog
CHANGED
@@ -1,3 +1,62 @@
|
|
1
|
+
2022-01-22 version 1.4.4:
|
2
|
+
|
3
|
+
* Specify the build option --platform=8 for older Java platforms
|
4
|
+
|
5
|
+
2022-01-20 version 1.4.3:
|
6
|
+
|
7
|
+
* Optimize serialization/deserialization of Symbols
|
8
|
+
* Support registering ext types for objects of subclasses of primitive types (like Hash)
|
9
|
+
* Add optimized_symbols_parsing option to Factory#register_type on MRI implementation
|
10
|
+
* Optimize to deduplicate Hash keys on JRuby
|
11
|
+
* Support JRuby 9.3 (and drop 9.1)
|
12
|
+
|
13
|
+
2021-02-01 version 1.4.2:
|
14
|
+
|
15
|
+
* Add the required Ruby version (>= 2.4) to avoid compilation errors on older Ruby runtimes
|
16
|
+
* Drop the support of old Ruby versions explicitly (1.8, 1.9, 2.0, 2.1, 2.2, 2.3)
|
17
|
+
|
18
|
+
2021-01-27 version 1.4.1:
|
19
|
+
|
20
|
+
* Bugfix about the wrong string encoding longer than 256 bytes (#200)
|
21
|
+
|
22
|
+
2021-01-27 version 1.4.0:
|
23
|
+
|
24
|
+
* Introduce the optimization to use frozen/deduped keys for map objects
|
25
|
+
* Stop releasing fat gem (pre-built binaries) for mswin32 arch environments
|
26
|
+
|
27
|
+
2020-02-05 version 1.3.3:
|
28
|
+
|
29
|
+
* Hotfix release for Windows environments: 1.3.2 missed including binaries
|
30
|
+
|
31
|
+
2020-02-04 version 1.3.2:
|
32
|
+
|
33
|
+
* Add Ruby 2.7.0 binary in gem releases for Windows
|
34
|
+
|
35
|
+
2019-08-05 version 1.3.1:
|
36
|
+
|
37
|
+
* Fix timestamp ext type bug about timestamps with seconds larger than 32bit int (after 2106-02-07 06:28:16 UTC)
|
38
|
+
|
39
|
+
2019-06-20 verison 1.3.0:
|
40
|
+
|
41
|
+
* Add timestamp ext type (id:-1) support
|
42
|
+
|
43
|
+
2019-04-19 version 1.2.10:
|
44
|
+
|
45
|
+
* Optimze MessagePack.unpack not to copy source string
|
46
|
+
|
47
|
+
2019-03-13 version 1.2.9:
|
48
|
+
|
49
|
+
* Hotfix release only for JRuby: 1.2.8-java was built incorrectly
|
50
|
+
|
51
|
+
2019-03-11 version 1.2.8:
|
52
|
+
|
53
|
+
* Fix a regression that MessagePack#unpack raises error if IO is assigned as the (only) argument
|
54
|
+
* Improve compatibility that MessagePack#pack returns nil if IO is assigned as 2nd argument
|
55
|
+
|
56
|
+
2019-03-01 version 1.2.7:
|
57
|
+
|
58
|
+
* Add Packer#write_bin and Packer#write_bin_header methods
|
59
|
+
|
1
60
|
2019-01-08 verison 1.2.6:
|
2
61
|
|
3
62
|
* Update Ruby version 2.6 dependency (especially for Windows environment)
|
data/Gemfile
CHANGED
data/README.md
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
# MessagePack
|
2
|
+
|
3
|
+
[MessagePack](http://msgpack.org) is an efficient binary serialization format.
|
4
|
+
It lets you exchange data among multiple languages like JSON but it's faster and smaller.
|
5
|
+
For example, small integers (like flags or error code) are encoded into a single byte,
|
6
|
+
and typical short strings only require an extra byte in addition to the strings themselves.
|
7
|
+
|
8
|
+
If you ever wished to use JSON for convenience (storing an image with metadata) but could
|
9
|
+
not for technical reasons (binary data, size, speed...), MessagePack is a perfect replacement.
|
10
|
+
|
11
|
+
require 'msgpack'
|
12
|
+
msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
|
13
|
+
MessagePack.unpack(msg) #=> [1,2,3]
|
14
|
+
|
15
|
+
Use RubyGems to install:
|
16
|
+
|
17
|
+
gem install msgpack
|
18
|
+
|
19
|
+
or build msgpack-ruby and install:
|
20
|
+
|
21
|
+
bundle
|
22
|
+
rake
|
23
|
+
gem install --local pkg/msgpack
|
24
|
+
|
25
|
+
|
26
|
+
## Use cases
|
27
|
+
|
28
|
+
* Create REST API returing MessagePack using Rails + [RABL](https://github.com/nesquena/rabl)
|
29
|
+
* Store objects efficiently serialized by msgpack on memcached or Redis
|
30
|
+
* In fact Redis supports msgpack in [EVAL-scripts](http://redis.io/commands/eval)
|
31
|
+
* Upload data in efficient format from mobile devices such as smartphones
|
32
|
+
* MessagePack works on iPhone/iPad and Android. See also [Objective-C](https://github.com/msgpack/msgpack-objectivec) and [Java](https://github.com/msgpack/msgpack-java) implementations
|
33
|
+
* Design a portable protocol to communicate with embedded devices
|
34
|
+
* Check also [Fluentd](http://fluentd.org/) which is a log collector which uses msgpack for the log format (they say it uses JSON but actually it's msgpack, which is compatible with JSON)
|
35
|
+
* Exchange objects between software components written in different languages
|
36
|
+
* You'll need a flexible but efficient format so that components exchange objects while keeping compatibility
|
37
|
+
|
38
|
+
## Portability
|
39
|
+
|
40
|
+
MessagePack for Ruby should run on x86, ARM, PowerPC, SPARC and other CPU architectures.
|
41
|
+
|
42
|
+
And it works with MRI (CRuby) and Rubinius.
|
43
|
+
Patches to improve portability is highly welcomed.
|
44
|
+
|
45
|
+
|
46
|
+
## Serializing objects
|
47
|
+
|
48
|
+
Use `MessagePack.pack` or `to_msgpack`:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'msgpack'
|
52
|
+
msg = MessagePack.pack(obj) # or
|
53
|
+
msg = obj.to_msgpack
|
54
|
+
```
|
55
|
+
|
56
|
+
### Streaming serialization
|
57
|
+
|
58
|
+
Packer provides advanced API to serialize objects in streaming style:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# serialize a 2-element array [e1, e2]
|
62
|
+
pk = MessagePack::Packer.new(io)
|
63
|
+
pk.write_array_header(2).write(e1).write(e2).flush
|
64
|
+
```
|
65
|
+
|
66
|
+
See [API reference](http://ruby.msgpack.org/MessagePack/Packer.html) for details.
|
67
|
+
|
68
|
+
## Deserializing objects
|
69
|
+
|
70
|
+
Use `MessagePack.unpack`:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
require 'msgpack'
|
74
|
+
obj = MessagePack.unpack(msg)
|
75
|
+
```
|
76
|
+
|
77
|
+
### Streaming deserialization
|
78
|
+
|
79
|
+
Unpacker provides advanced API to deserialize objects in streaming style:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
# deserialize objects from an IO
|
83
|
+
u = MessagePack::Unpacker.new(io)
|
84
|
+
u.each do |obj|
|
85
|
+
# ...
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
or event-driven style which works well with EventMachine:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
# event-driven deserialization
|
93
|
+
def on_read(data)
|
94
|
+
@u ||= MessagePack::Unpacker.new
|
95
|
+
@u.feed_each(data) {|obj|
|
96
|
+
# ...
|
97
|
+
}
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
See [API reference](http://ruby.msgpack.org/MessagePack/Unpacker.html) for details.
|
102
|
+
|
103
|
+
## Serializing and deserializing symbols
|
104
|
+
|
105
|
+
By default, symbols are serialized as strings:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
packed = :symbol.to_msgpack # => "\xA6symbol"
|
109
|
+
MessagePack.unpack(packed) # => "symbol"
|
110
|
+
```
|
111
|
+
|
112
|
+
This can be customized by registering an extension type for them:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
MessagePack::DefaultFactory.register_type(0x00, Symbol)
|
116
|
+
|
117
|
+
# symbols now survive round trips
|
118
|
+
packed = :symbol.to_msgpack # => "\xc7\x06\x00symbol"
|
119
|
+
MessagePack.unpack(packed) # => :symbol
|
120
|
+
```
|
121
|
+
|
122
|
+
The extension type for symbols is configurable like any other extension type.
|
123
|
+
For example, to customize how symbols are packed you can just redefine
|
124
|
+
Symbol#to_msgpack_ext. Doing this gives you an option to prevent symbols from
|
125
|
+
being serialized altogether by throwing an exception:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
class Symbol
|
129
|
+
def to_msgpack_ext
|
130
|
+
raise "Serialization of symbols prohibited"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
MessagePack::DefaultFactory.register_type(0x00, Symbol)
|
135
|
+
|
136
|
+
[1, :symbol, 'string'].to_msgpack # => RuntimeError: Serialization of symbols prohibited
|
137
|
+
```
|
138
|
+
|
139
|
+
## Serializing and deserializing Time instances
|
140
|
+
|
141
|
+
There are the timestamp extension type in MessagePack,
|
142
|
+
but it is not registered by default.
|
143
|
+
|
144
|
+
To map Ruby's Time to MessagePack's timestamp for the default factory:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
MessagePack::DefaultFactory.register_type(
|
148
|
+
MessagePack::Timestamp::TYPE, # or just -1
|
149
|
+
Time,
|
150
|
+
packer: MessagePack::Time::Packer,
|
151
|
+
unpacker: MessagePack::Time::Unpacker
|
152
|
+
)
|
153
|
+
```
|
154
|
+
|
155
|
+
See [API reference](http://ruby.msgpack.org/) for details.
|
156
|
+
|
157
|
+
## Extension Types
|
158
|
+
|
159
|
+
Packer and Unpacker support [Extension types of MessagePack](https://github.com/msgpack/msgpack/blob/master/spec.md#types-extension-type).
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
# register how to serialize custom class at first
|
163
|
+
pk = MessagePack::Packer.new(io)
|
164
|
+
pk.register_type(0x01, MyClass1, :to_msgpack_ext) # equal to pk.register_type(0x01, MyClass)
|
165
|
+
pk.register_type(0x02, MyClass2){|obj| obj.how_to_serialize() } # blocks also available
|
166
|
+
|
167
|
+
# almost same API for unpacker
|
168
|
+
uk = MessagePack::Unpacker.new()
|
169
|
+
uk.register_type(0x01, MyClass1, :from_msgpack_ext)
|
170
|
+
uk.register_type(0x02){|data| MyClass2.create_from_serialized_data(data) }
|
171
|
+
```
|
172
|
+
|
173
|
+
`MessagePack::Factory` is to create packer and unpacker which have same extension types.
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
factory = MessagePack::Factory.new
|
177
|
+
factory.register_type(0x01, MyClass1) # same with next line
|
178
|
+
factory.register_type(0x01, MyClass1, packer: :to_msgpack_ext, unpacker: :from_msgpack_ext)
|
179
|
+
pk = factory.packer(options_for_packer)
|
180
|
+
uk = factory.unpacker(options_for_unpacker)
|
181
|
+
```
|
182
|
+
|
183
|
+
For `MessagePack.pack` and `MessagePack.unpack`, default packer/unpacker refer `MessagePack::DefaultFactory`. Call `MessagePack::DefaultFactory.register_type` to enable types process globally.
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
MessagePack::DefaultFactory.register_type(0x03, MyClass3)
|
187
|
+
MessagePack.unpack(data_with_ext_typeid_03) #=> MyClass3 instance
|
188
|
+
```
|
189
|
+
|
190
|
+
## Buffer API
|
191
|
+
|
192
|
+
MessagePack for Ruby provides a buffer API so that you can read or write data by hand, not via Packer or Unpacker API.
|
193
|
+
|
194
|
+
This [MessagePack::Buffer](http://ruby.msgpack.org/MessagePack/Buffer.html) is backed with a fixed-length shared memory pool which is very fast for small data (<= 4KB),
|
195
|
+
and has zero-copy capability which significantly affects performance to handle large binary data.
|
196
|
+
|
197
|
+
## How to build and run tests
|
198
|
+
|
199
|
+
Before building msgpack, you need to install bundler and dependencies.
|
200
|
+
|
201
|
+
gem install bundler
|
202
|
+
bundle install
|
203
|
+
|
204
|
+
Then, you can run the tasks as follows:
|
205
|
+
|
206
|
+
### Build
|
207
|
+
|
208
|
+
bundle exec rake build
|
209
|
+
|
210
|
+
### Run tests
|
211
|
+
|
212
|
+
bundle exec rake spec
|
213
|
+
|
214
|
+
### Generating docs
|
215
|
+
|
216
|
+
bundle exec rake doc
|
217
|
+
|
218
|
+
## How to build -java rubygems
|
219
|
+
|
220
|
+
To build -java gems for JRuby, run:
|
221
|
+
|
222
|
+
rake build:java
|
223
|
+
|
224
|
+
If this directory has Gemfile.lock (generated with MRI), remove it beforehand.
|
225
|
+
|
226
|
+
## Updating documents
|
227
|
+
|
228
|
+
Online documents (http://ruby.msgpack.org) is generated from gh-pages branch.
|
229
|
+
Following commands update documents in gh-pages branch:
|
230
|
+
|
231
|
+
bundle exec rake doc
|
232
|
+
git checkout gh-pages
|
233
|
+
cp doc/* ./ -a
|
234
|
+
|
235
|
+
## Copyright
|
236
|
+
|
237
|
+
* Author
|
238
|
+
* Sadayuki Furuhashi <frsyuki@gmail.com>
|
239
|
+
* Copyright
|
240
|
+
* Copyright (c) 2008-2015 Sadayuki Furuhashi
|
241
|
+
* License
|
242
|
+
* Apache License, Version 2.0
|
data/Rakefile
CHANGED
@@ -34,8 +34,7 @@ if RUBY_PLATFORM =~ /java/
|
|
34
34
|
jars = ["#{jruby_home}/lib/jruby.jar"]
|
35
35
|
ext.classpath = jars.map { |x| File.expand_path(x) }.join(':')
|
36
36
|
ext.lib_dir = File.join(*['lib', 'msgpack', ENV['FAT_DIR']].compact)
|
37
|
-
ext.
|
38
|
-
ext.target_version = '1.6'
|
37
|
+
ext.release = '8'
|
39
38
|
end
|
40
39
|
else
|
41
40
|
require 'rake/extensiontask'
|
@@ -62,12 +61,8 @@ RSpec::Core::RakeTask.new(:spec) do |t|
|
|
62
61
|
end
|
63
62
|
|
64
63
|
namespace :build do
|
65
|
-
desc 'Build
|
66
|
-
task :
|
67
|
-
require 'rake_compiler_dock'
|
68
|
-
# See RUBY_CC_VERSION in https://github.com/rake-compiler/rake-compiler-dock/blob/master/Dockerfile
|
69
|
-
RakeCompilerDock.sh 'bundle && gem i json && rake cross native gem RUBY_CC_VERSION=2.2.2:2.3.0:2.4.0:2.5.0:2.6.0'
|
70
|
-
end
|
64
|
+
desc 'Build gem for JRuby after cleaning'
|
65
|
+
task :java => [:clean, :spec, :build]
|
71
66
|
end
|
72
67
|
|
73
68
|
CLEAN.include('lib/msgpack/msgpack.*')
|
data/doclib/msgpack/factory.rb
CHANGED
@@ -75,6 +75,7 @@ module MessagePack
|
|
75
75
|
#
|
76
76
|
# * *:packer* specify symbol or proc object for packer
|
77
77
|
# * *:unpacker* specify symbol or proc object for unpacker
|
78
|
+
# * *:optimized_symbols_parsing* specify true to use the optimized symbols parsing (not supported on JRuby now)
|
78
79
|
#
|
79
80
|
def register_type(type, klass, options={})
|
80
81
|
end
|
data/doclib/msgpack/packer.rb
CHANGED
@@ -91,6 +91,12 @@ module MessagePack
|
|
91
91
|
def write_nil
|
92
92
|
end
|
93
93
|
|
94
|
+
#
|
95
|
+
# Serializes a string object as binary data. Same as write("string".encode(Encoding::BINARY)).
|
96
|
+
#
|
97
|
+
def write_bin(obj)
|
98
|
+
end
|
99
|
+
|
94
100
|
#
|
95
101
|
# Write a header of an array whose size is _n_.
|
96
102
|
# For example, write_array_header(1).write(true) is same as write([ true ]).
|
@@ -109,6 +115,20 @@ module MessagePack
|
|
109
115
|
def write_map_header(n)
|
110
116
|
end
|
111
117
|
|
118
|
+
#
|
119
|
+
# Write a header of a binary string whose size is _n_. Useful if you want to append large binary data without loading it into memory at once.
|
120
|
+
# For example,
|
121
|
+
# MessagePack::Packer.new(io).write_bin_header(12).flush
|
122
|
+
# io.write('chunk1')
|
123
|
+
# io.write('chunk2')
|
124
|
+
# is the same as
|
125
|
+
# write('chunk1chunk2'.encode(Encoding::BINARY)).
|
126
|
+
#
|
127
|
+
# @return [Packer] self
|
128
|
+
#
|
129
|
+
def write_bin_header(n)
|
130
|
+
end
|
131
|
+
|
112
132
|
#
|
113
133
|
# Serializes _value_ as 32-bit single precision float into internal buffer.
|
114
134
|
# _value_ will be approximated with the nearest possible single precision float, thus
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MessagePack
|
2
|
+
|
3
|
+
# MessagePack::Time provides packer and unpacker functions for a timestamp type.
|
4
|
+
# @example Setup for DefaultFactory
|
5
|
+
# MessagePack::DefaultFactory.register_type(
|
6
|
+
# MessagePack::Timestamp::TYPE,
|
7
|
+
# Time,
|
8
|
+
# packer: MessagePack::Time::Packer,
|
9
|
+
# unpacker: MessagePack::Time::Unpacker
|
10
|
+
# )
|
11
|
+
class Time
|
12
|
+
# A packer function that packs a Time instance to a MessagePack timestamp.
|
13
|
+
Packer = lambda { |payload|
|
14
|
+
# ...
|
15
|
+
}
|
16
|
+
|
17
|
+
# An unpacker function that unpacks a MessagePack timestamp to a Time instance.
|
18
|
+
Unpacker = lambda { |time|
|
19
|
+
# ...
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module MessagePack
|
2
|
+
# A utility class for MessagePack timestamp type
|
3
|
+
class Timestamp
|
4
|
+
#
|
5
|
+
# The timestamp extension type defined in the MessagePack spec.
|
6
|
+
#
|
7
|
+
# See https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type for details.
|
8
|
+
#
|
9
|
+
TYPE = -1
|
10
|
+
|
11
|
+
# @return [Integer] Second part of the timestamp.
|
12
|
+
attr_reader :sec
|
13
|
+
|
14
|
+
# @return [Integer] Nanosecond part of the timestamp.
|
15
|
+
attr_reader :nsec
|
16
|
+
|
17
|
+
# @param [Integer] sec
|
18
|
+
# @param [Integer] nsec
|
19
|
+
def initialize(sec, nsec)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @example An unpacker implementation for the Time class
|
23
|
+
# lambda do |payload|
|
24
|
+
# tv = MessagePack::Timestamp.from_msgpack_ext(payload)
|
25
|
+
# Time.at(tv.sec, tv.nsec, :nanosecond)
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @param [String] data
|
29
|
+
# @return [MessagePack::Timestamp]
|
30
|
+
def self.from_msgpack_ext(data)
|
31
|
+
end
|
32
|
+
|
33
|
+
# @example A packer implementation for the Time class
|
34
|
+
# unpacker = lambda do |time|
|
35
|
+
# MessagePack::Timestamp.to_msgpack_ext(time.tv_sec, time.tv_nsec)
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# @param [Integer] sec
|
39
|
+
# @param [Integer] nsec
|
40
|
+
# @return [String]
|
41
|
+
def self.to_msgpack_ext(sec, nsec)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/doclib/msgpack.rb
CHANGED
@@ -12,7 +12,7 @@ module MessagePack
|
|
12
12
|
# @param obj [Object] object to be serialized
|
13
13
|
# @param io [IO]
|
14
14
|
# @param options [Hash]
|
15
|
-
# @return [
|
15
|
+
# @return [nil]
|
16
16
|
#
|
17
17
|
# See Packer#initialize for supported options.
|
18
18
|
#
|
@@ -31,7 +31,7 @@ module MessagePack
|
|
31
31
|
# @param obj [Object] object to be serialized
|
32
32
|
# @param io [IO]
|
33
33
|
# @param options [Hash]
|
34
|
-
# @return [
|
34
|
+
# @return [nil]
|
35
35
|
#
|
36
36
|
# See Packer#initialize for supported options.
|
37
37
|
#
|
@@ -21,6 +21,7 @@ import org.jcodings.Encoding;
|
|
21
21
|
|
22
22
|
@JRubyClass(name="MessagePack::Buffer")
|
23
23
|
public class Buffer extends RubyObject {
|
24
|
+
private static final long serialVersionUID = 8441244627425629412L;
|
24
25
|
private IRubyObject io;
|
25
26
|
private ByteBuffer buffer;
|
26
27
|
private boolean writeMode;
|
@@ -49,7 +50,7 @@ public class Buffer extends RubyObject {
|
|
49
50
|
}
|
50
51
|
this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
|
51
52
|
this.writeMode = true;
|
52
|
-
this.binaryEncoding = ctx.
|
53
|
+
this.binaryEncoding = ctx.runtime.getEncodingService().getAscii8bitEncoding();
|
53
54
|
return this;
|
54
55
|
}
|
55
56
|
|
@@ -87,17 +88,17 @@ public class Buffer extends RubyObject {
|
|
87
88
|
writeMode = true;
|
88
89
|
}
|
89
90
|
buffer.clear();
|
90
|
-
return ctx.
|
91
|
+
return ctx.runtime.getNil();
|
91
92
|
}
|
92
93
|
|
93
94
|
@JRubyMethod(name = "size")
|
94
95
|
public IRubyObject size(ThreadContext ctx) {
|
95
|
-
return ctx.
|
96
|
+
return ctx.runtime.newFixnum(rawSize());
|
96
97
|
}
|
97
98
|
|
98
99
|
@JRubyMethod(name = "empty?")
|
99
100
|
public IRubyObject isEmpty(ThreadContext ctx) {
|
100
|
-
return rawSize() == 0 ? ctx.
|
101
|
+
return rawSize() == 0 ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
|
101
102
|
}
|
102
103
|
|
103
104
|
private IRubyObject bufferWrite(ThreadContext ctx, IRubyObject str) {
|
@@ -105,7 +106,7 @@ public class Buffer extends RubyObject {
|
|
105
106
|
int length = bytes.length();
|
106
107
|
ensureRemainingCapacity(length);
|
107
108
|
buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
|
108
|
-
return ctx.
|
109
|
+
return ctx.runtime.newFixnum(length);
|
109
110
|
|
110
111
|
}
|
111
112
|
|
@@ -131,19 +132,19 @@ public class Buffer extends RubyObject {
|
|
131
132
|
length = (int) args[0].convertToInteger().getLongValue();
|
132
133
|
}
|
133
134
|
if (raiseOnUnderflow && rawSize() < length) {
|
134
|
-
throw ctx.
|
135
|
+
throw ctx.runtime.newEOFError();
|
135
136
|
}
|
136
137
|
int readLength = Math.min(length, rawSize());
|
137
138
|
if (readLength == 0 && length > 0) {
|
138
|
-
return ctx.
|
139
|
+
return ctx.runtime.getNil();
|
139
140
|
} else if (readLength == 0) {
|
140
|
-
return ctx.
|
141
|
+
return ctx.runtime.newString();
|
141
142
|
} else {
|
142
143
|
ensureReadMode();
|
143
144
|
byte[] bytes = new byte[readLength];
|
144
145
|
buffer.get(bytes);
|
145
146
|
ByteList byteList = new ByteList(bytes, binaryEncoding);
|
146
|
-
return ctx.
|
147
|
+
return ctx.runtime.newString(byteList);
|
147
148
|
}
|
148
149
|
}
|
149
150
|
|
@@ -161,12 +162,12 @@ public class Buffer extends RubyObject {
|
|
161
162
|
feed(ctx);
|
162
163
|
int length = (int) _length.convertToInteger().getLongValue();
|
163
164
|
if (raiseOnUnderflow && rawSize() < length) {
|
164
|
-
throw ctx.
|
165
|
+
throw ctx.runtime.newEOFError();
|
165
166
|
}
|
166
167
|
ensureReadMode();
|
167
168
|
int skipLength = Math.min(length, rawSize());
|
168
169
|
buffer.position(buffer.position() + skipLength);
|
169
|
-
return ctx.
|
170
|
+
return ctx.runtime.newFixnum(skipLength);
|
170
171
|
}
|
171
172
|
|
172
173
|
@JRubyMethod(name = "skip")
|
@@ -179,28 +180,32 @@ public class Buffer extends RubyObject {
|
|
179
180
|
return skipCommon(ctx, length, true);
|
180
181
|
}
|
181
182
|
|
183
|
+
public boolean hasIo() {
|
184
|
+
return io != null;
|
185
|
+
}
|
186
|
+
|
182
187
|
@JRubyMethod(name = "to_s", alias = {"to_str"})
|
183
188
|
public IRubyObject toS(ThreadContext ctx) {
|
184
189
|
ensureReadMode();
|
185
190
|
int length = buffer.limit() - buffer.position();
|
186
191
|
ByteList str = new ByteList(buffer.array(), buffer.position(), length, binaryEncoding, true);
|
187
|
-
return ctx.
|
192
|
+
return ctx.runtime.newString(str);
|
188
193
|
}
|
189
194
|
|
190
195
|
@JRubyMethod(name = "to_a")
|
191
196
|
public IRubyObject toA(ThreadContext ctx) {
|
192
|
-
return ctx.
|
197
|
+
return ctx.runtime.newArray(toS(ctx));
|
193
198
|
}
|
194
199
|
|
195
200
|
@JRubyMethod(name = "io")
|
196
201
|
public IRubyObject getIo(ThreadContext ctx) {
|
197
|
-
return io == null ? ctx.
|
202
|
+
return io == null ? ctx.runtime.getNil() : io;
|
198
203
|
}
|
199
204
|
|
200
205
|
@JRubyMethod(name = "flush")
|
201
206
|
public IRubyObject flush(ThreadContext ctx) {
|
202
207
|
if (io == null) {
|
203
|
-
return ctx.
|
208
|
+
return ctx.runtime.getNil();
|
204
209
|
} else {
|
205
210
|
return io.callMethod(ctx, "flush");
|
206
211
|
}
|
@@ -209,7 +214,7 @@ public class Buffer extends RubyObject {
|
|
209
214
|
@JRubyMethod(name = "close")
|
210
215
|
public IRubyObject close(ThreadContext ctx) {
|
211
216
|
if (io == null) {
|
212
|
-
return ctx.
|
217
|
+
return ctx.runtime.getNil();
|
213
218
|
} else {
|
214
219
|
return io.callMethod(ctx, "close");
|
215
220
|
}
|