Class: MessagePack::Unpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/unpacker.rb,
ext/msgpack/unpacker_class.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/msgpack/unpacker_class.c', line 68

VALUE MessagePack_Unpacker_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE io = Qnil;
    VALUE options = Qnil;

    if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
        /* Qnil */

    } else if(argc == 1) {
        VALUE v = argv[0];
        if(rb_type(v) == T_HASH) {
            options = v;
            if(rb_type(options) != T_HASH) {
                rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
            }
        } else {
            io = v;
        }

    } else if(argc == 2) {
        io = argv[0];
        options = argv[1];
        if(rb_type(options) != T_HASH) {
            rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
        }

    } else {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
    }

    UNPACKER(self, uk);

    msgpack_unpacker_ext_registry_init(&uk->ext_registry);
    uk->buffer_ref = MessagePack_Buffer_wrap(UNPACKER_BUFFER_(uk), self);

    MessagePack_Buffer_set_options(UNPACKER_BUFFER_(uk), io, options);

    if(options != Qnil) {
        VALUE v;

        v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
        msgpack_unpacker_set_symbolized_keys(uk, RTEST(v));

        v = rb_hash_aref(options, ID2SYM(rb_intern("allow_unknown_ext")));
        msgpack_unpacker_set_allow_unknown_ext(uk, RTEST(v));
    }

    return self;
}

Instance Method Details

#allow_unknown_ext?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
# File 'ext/msgpack/unpacker_class.c', line 124

static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
{
    UNPACKER(self, uk);
    return uk->allow_unknown_ext ? Qtrue : Qfalse;
}

#bufferObject



148
149
150
151
152
# File 'ext/msgpack/unpacker_class.c', line 148

static VALUE Unpacker_buffer(VALUE self)
{
    UNPACKER(self, uk);
    return uk->buffer_ref;
}

#eachObject



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/msgpack/unpacker_class.c', line 299

static VALUE Unpacker_each(VALUE self)
{
    UNPACKER(self, uk);

#ifdef RETURN_ENUMERATOR
    RETURN_ENUMERATOR(self, 0, 0);
#endif

    if(msgpack_buffer_has_io(UNPACKER_BUFFER_(uk))) {
        /* rescue EOFError only if io is set */
        return rb_rescue2(Unpacker_each_impl, self,
                Unpacker_rescue_EOFError, self,
                rb_eEOFError, NULL);
    } else {
        return Unpacker_each_impl(self);
    }
}

#feed(data) ⇒ Object

rb_define_method(cMessagePack_Unpacker, “peek_next_type”, Unpacker_peek_next_type, 0); // TODO



248
249
250
251
252
253
254
255
256
257
# File 'ext/msgpack/unpacker_class.c', line 248

static VALUE Unpacker_feed(VALUE self, VALUE data)
{
    UNPACKER(self, uk);

    StringValue(data);

    msgpack_buffer_append_string(UNPACKER_BUFFER_(uk), data);

    return self;
}

#feed_each(data) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
# File 'ext/msgpack/unpacker_class.c', line 317

static VALUE Unpacker_feed_each(VALUE self, VALUE data)
{
#ifdef RETURN_ENUMERATOR
    {
        VALUE argv[] = { data };
        RETURN_ENUMERATOR(self, sizeof(argv) / sizeof(VALUE), argv);
    }
#endif

    Unpacker_feed_reference(self, data);
    return Unpacker_each(self);
}

#feed_reference(data) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'ext/msgpack/unpacker_class.c', line 259

static VALUE Unpacker_feed_reference(VALUE self, VALUE data)
{
    UNPACKER(self, uk);

    StringValue(data);

    msgpack_buffer_append_string_reference(UNPACKER_BUFFER_(uk), data);

    return self;
}

#full_unpackObject

msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(s_unpacker), 0);



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/msgpack/unpacker_class.c', line 395

static VALUE Unpacker_full_unpack(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_read(uk, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    /* raise if extra bytes follow */
    size_t extra = msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(uk));
    if(extra > 0) {
        rb_raise(eMalformedFormatError, "%zd extra bytes after the deserialized object", extra);
    }

    return msgpack_unpacker_get_last_object(uk);
}

#readObject Also known as: unpack



154
155
156
157
158
159
160
161
162
163
164
# File 'ext/msgpack/unpacker_class.c', line 154

static VALUE Unpacker_read(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_read(uk, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    return msgpack_unpacker_get_last_object(uk);
}

#read_array_headerObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'ext/msgpack/unpacker_class.c', line 193

static VALUE Unpacker_read_array_header(VALUE self)
{
    UNPACKER(self, uk);

    uint32_t size;
    int r = msgpack_unpacker_read_array_header(uk, &size);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    return ULONG2NUM(size);
}

#read_map_headerObject



206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/msgpack/unpacker_class.c', line 206

static VALUE Unpacker_read_map_header(VALUE self)
{
    UNPACKER(self, uk);

    uint32_t size;
    int r = msgpack_unpacker_read_map_header(uk, &size);
    if(r < 0) {
        raise_unpacker_error((int)r);
    }

    return ULONG2NUM(size);
}

#register_type(*args) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'ext/msgpack/unpacker_class.c', line 353

static VALUE Unpacker_register_type(int argc, VALUE* argv, VALUE self)
{
    UNPACKER(self, uk);

    int ext_type;
    VALUE proc;
    VALUE arg;
    VALUE ext_module;

    switch (argc) {
    case 1:
        /* register_type(0x7f) {|data| block... } */
        rb_need_block();
#ifdef HAVE_RB_BLOCK_LAMBDA
        proc = rb_block_lambda();
#else
        /* MRI 1.8 */
        proc = rb_block_proc();
#endif
        arg = proc;
        ext_module = Qnil;
        break;
    case 3:
        /* register_type(0x7f, Time, :from_msgpack_ext) */
        ext_module = argv[1];
        arg = argv[2];
        proc = rb_obj_method(ext_module, arg);
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 3)", argc);
    }

    ext_type = NUM2INT(argv[0]);
    if(ext_type < -128 || ext_type > 127) {
        rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
    }

    msgpack_unpacker_ext_registry_put(&uk->ext_registry, ext_module, ext_type, proc, arg);

    return Qnil;
}

#registered_typesObject

see ext for other methods



5
6
7
8
9
10
11
12
13
# File 'lib/msgpack/unpacker.rb', line 5

def registered_types
  list = []

  registered_types_internal.each_pair do |type, ary|
    list << {type: type, class: ary[0], unpacker: ary[2]}
  end

  list.sort{|a, b| a[:type] <=> b[:type] }
end

#resetObject



330
331
332
333
334
335
336
337
# File 'ext/msgpack/unpacker_class.c', line 330

static VALUE Unpacker_reset(VALUE self)
{
    UNPACKER(self, uk);

    _msgpack_unpacker_reset(uk);

    return Qnil;
}

#skipObject



166
167
168
169
170
171
172
173
174
175
176
# File 'ext/msgpack/unpacker_class.c', line 166

static VALUE Unpacker_skip(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_skip(uk, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    return Qnil;
}

#skip_nilObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ext/msgpack/unpacker_class.c', line 178

static VALUE Unpacker_skip_nil(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_skip_nil(uk);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    if(r) {
        return Qtrue;
    }
    return Qfalse;
}

#symbolize_keys?Boolean

Returns:

  • (Boolean)


118
119
120
121
122
# File 'ext/msgpack/unpacker_class.c', line 118

static VALUE Unpacker_symbolized_keys_p(VALUE self)
{
    UNPACKER(self, uk);
    return uk->symbolize_keys ? Qtrue : Qfalse;
}

#type_registered?(klass_or_type) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/msgpack/unpacker.rb', line 15

def type_registered?(klass_or_type)
  case klass_or_type
  when Class
    klass = klass_or_type
    registered_types.any?{|entry| klass == entry[:class] }
  when Integer
    type = klass_or_type
    registered_types.any?{|entry| type == entry[:type] }
  else
    raise ArgumentError, "class or type id"
  end
end