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



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/msgpack/unpacker_class.c', line 91

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;
        } else {
            io = v;
        }

    } else if(argc == 2) {
        io = argv[0];
        options = argv[1];
        if(options != Qnil && 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);
    }

    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

    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, sym_symbolize_keys);
        msgpack_unpacker_set_symbolized_keys(uk, RTEST(v));

        v = rb_hash_aref(options, sym_freeze);
        msgpack_unpacker_set_freeze(uk, RTEST(v));

        v = rb_hash_aref(options, sym_allow_unknown_ext);
        msgpack_unpacker_set_allow_unknown_ext(uk, RTEST(v));
    }

    return self;
}

Instance Method Details

#allow_unknown_ext?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
# File 'ext/msgpack/unpacker_class.c', line 152

static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
    return uk->allow_unknown_ext ? Qtrue : Qfalse;
}

#bufferObject



177
178
179
180
181
# File 'ext/msgpack/unpacker_class.c', line 177

static VALUE Unpacker_buffer(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
    return uk->buffer_ref;
}

#eachObject



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

static VALUE Unpacker_each(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

#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

long at least 32 bits



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

static VALUE Unpacker_feed(VALUE self, VALUE data)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

    StringValue(data);

    msgpack_buffer_append_string(UNPACKER_BUFFER_(uk), data);

    return self;
}

#feed_each(data) ⇒ Object



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

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



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

static VALUE Unpacker_feed_reference(VALUE self, VALUE data)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

    StringValue(data);

    msgpack_buffer_append_string_reference(UNPACKER_BUFFER_(uk), data);

    return self;
}

#freeze?Boolean

Returns:

  • (Boolean)


146
147
148
149
150
# File 'ext/msgpack/unpacker_class.c', line 146

static VALUE Unpacker_freeze_p(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
    return uk->freeze ? Qtrue : Qfalse;
}

#full_unpackObject

msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(s_unpacker), 0);



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

static VALUE Unpacker_full_unpack(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

    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



183
184
185
186
187
188
189
190
191
192
193
# File 'ext/msgpack/unpacker_class.c', line 183

static VALUE Unpacker_read(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

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

    return msgpack_unpacker_get_last_object(uk);
}

#read_array_headerObject



222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/msgpack/unpacker_class.c', line 222

static VALUE Unpacker_read_array_header(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

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

    return ULONG2NUM(size); // long at least 32 bits
}

#read_map_headerObject

long at least 32 bits



235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/msgpack/unpacker_class.c', line 235

static VALUE Unpacker_read_map_header(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

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

    return ULONG2NUM(size); // long at least 32 bits
}

#register_type(*args) ⇒ Object



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
# File 'ext/msgpack/unpacker_class.c', line 357

static VALUE Unpacker_register_type(int argc, VALUE* argv, VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

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

    switch (argc) {
    case 1:
        /* register_type(0x7f) {|data| block... } */
        rb_need_block();
        proc = rb_block_lambda();
        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, 0, 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



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

static VALUE Unpacker_reset(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

    _msgpack_unpacker_reset(uk);

    return Qnil;
}

#skipObject



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

static VALUE Unpacker_skip(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

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

    return Qnil;
}

#skip_nilObject



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

static VALUE Unpacker_skip_nil(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);

    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)


140
141
142
143
144
# File 'ext/msgpack/unpacker_class.c', line 140

static VALUE Unpacker_symbolized_keys_p(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
    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