class MessagePack::Unpacker
Public Class Methods
Source
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_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;
}
Public Instance Methods
Source
static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
{
msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
return uk->allow_unknown_ext ? Qtrue : Qfalse;
}
Source
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;
}
Source
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);
}
}
Source
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;
}
Also aliased as: feed_reference
Source
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);
}
Source
static VALUE Unpacker_freeze_p(VALUE self)
{
msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
return uk->freeze ? Qtrue : Qfalse;
}
Source
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);
}
Source
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);
}
Also aliased as: unpack
Source
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
}
Source
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
}
Source
# 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
Source
# 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
Source
static VALUE Unpacker_reset(VALUE self)
{
msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
_msgpack_unpacker_reset(uk);
return Qnil;
}
Source
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;
}
Source
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;
}
Source
static VALUE Unpacker_symbolized_keys_p(VALUE self)
{
msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
return uk->symbolize_keys ? Qtrue : Qfalse;
}
Source
# 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
Private Instance Methods
Source
static VALUE Unpacker_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE ext_module, VALUE proc)
{
if (OBJ_FROZEN(self)) {
rb_raise(rb_eFrozenError, "can't modify frozen MessagePack::Unpacker");
}
int ext_type = NUM2INT(rb_ext_type);
if(ext_type < -128 || ext_type > 127) {
rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
}
msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
msgpack_unpacker_ext_registry_put(self, &uk->ext_registry, ext_module, ext_type, 0, proc);
return Qnil;
}
Source
static VALUE Unpacker_registered_types_internal(VALUE self)
{
msgpack_unpacker_t *uk = MessagePack_Unpacker_get(self);
VALUE mapping = rb_hash_new();
if (uk->ext_registry) {
for(int i=0; i < 256; i++) {
if(uk->ext_registry->array[i] != Qnil) {
rb_hash_aset(mapping, INT2FIX(i - 128), uk->ext_registry->array[i]);
}
}
}
return mapping;
}