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



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

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);
    }

    UNPACKER(self, uk);

    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)


132
133
134
135
136
# File 'ext/msgpack/unpacker_class.c', line 132

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

#bufferObject



157
158
159
160
161
# File 'ext/msgpack/unpacker_class.c', line 157

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

#eachObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/msgpack/unpacker_class.c', line 281

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

long at least 32 bits



229
230
231
232
233
234
235
236
237
238
# File 'ext/msgpack/unpacker_class.c', line 229

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



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

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



240
241
242
243
244
245
246
247
248
249
# File 'ext/msgpack/unpacker_class.c', line 240

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;
}

#freeze?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'ext/msgpack/unpacker_class.c', line 126

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

#full_unpackObject

msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(s_unpacker), 0);



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'ext/msgpack/unpacker_class.c', line 374

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



163
164
165
166
167
168
169
170
171
172
173
# File 'ext/msgpack/unpacker_class.c', line 163

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



202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/msgpack/unpacker_class.c', line 202

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); // long at least 32 bits
}

#read_map_headerObject

long at least 32 bits



215
216
217
218
219
220
221
222
223
224
225
226
# File 'ext/msgpack/unpacker_class.c', line 215

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); // long at least 32 bits
}

#register_type(*args) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'ext/msgpack/unpacker_class.c', line 337

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();
        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



312
313
314
315
316
317
318
319
# File 'ext/msgpack/unpacker_class.c', line 312

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

    _msgpack_unpacker_reset(uk);

    return Qnil;
}

#skipObject



175
176
177
178
179
180
181
182
183
184
185
# File 'ext/msgpack/unpacker_class.c', line 175

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/msgpack/unpacker_class.c', line 187

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)


120
121
122
123
124
# File 'ext/msgpack/unpacker_class.c', line 120

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