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



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
139
140
141
142
143
144
145
146
# File 'ext/msgpack/unpacker_class.c', line 96

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 = Qnil;

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

    if(options != Qnil) {
        VALUE v;

        v = rb_hash_aref(options, sym_key_cache);
        msgpack_unpacker_set_key_cache(uk, RTEST(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)


160
161
162
163
164
# File 'ext/msgpack/unpacker_class.c', line 160

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



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

static VALUE Unpacker_buffer(VALUE self)
{
    msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
    if (!RTEST(uk->buffer_ref)) {
        uk->buffer_ref = MessagePack_Buffer_wrap(UNPACKER_BUFFER_(uk), self);
    }
    return uk->buffer_ref;
}

#eachObject



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

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 Also known as: feed_reference

long at least 32 bits



267
268
269
270
271
272
273
274
275
276
# File 'ext/msgpack/unpacker_class.c', line 267

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

#feed_each(data) ⇒ Object



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

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

#freeze?Boolean

Returns:

  • (Boolean)


154
155
156
157
158
# File 'ext/msgpack/unpacker_class.c', line 154

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

#full_unpackObject



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

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(uk, 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



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

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(uk, r);
    }

    return msgpack_unpacker_get_last_object(uk);
}

#read_array_headerObject



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

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(uk, r);
    }

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

#read_map_headerObject

long at least 32 bits



254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/msgpack/unpacker_class.c', line 254

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(uk, r);
    }

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

#register_type(type, klass = nil, method_name = nil, &block) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/msgpack/unpacker.rb', line 9

def register_type(type, klass = nil, method_name = nil, &block)
  if klass && method_name
    block = klass.method(method_name).to_proc
  elsif !block_given?
    raise ArgumentError, "register_type takes either 3 arguments or a block"
  end
  register_type_internal(type, klass, block)
end

#registered_typesObject



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

def registered_types
  list = []

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

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

#resetObject



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

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

    _msgpack_unpacker_reset(uk);

    return Qnil;
}

#skipObject



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

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(uk, r);
    }

    return Qnil;
}

#skip_nilObject



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

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(uk, r);
    }

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

#symbolize_keys?Boolean

Returns:

  • (Boolean)


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

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)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/msgpack/unpacker.rb', line 28

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