/* * Document-method: Hash#to_msgpack * * call-seq: * hash.to_msgpack(out = '') -> String * * Serializes the Hash into raw bytes. * This calls to_msgpack method reflectively for internal keys and values. */ static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_map(out, RHASH_SIZE(self)); rb_hash_foreach(self, MessagePack_Hash_to_msgpack_foreach, out); return out; }
/* * Document-method: String#to_msgpack * * call-seq: * string.to_msgpack(out = '') -> String * * Serializes the String into raw bytes. */ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_raw(out, RSTRING_LEN(self)); msgpack_pack_raw_body(out, RSTRING_PTR(self), RSTRING_LEN(self)); return out; }
/* * Document-method: Hash#to_msgpack * * call-seq: * hash.to_msgpack(out = '') -> String * * Serializes the Hash into raw bytes. * This calls to_msgpack method reflectively for internal keys and values. */ static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); // FIXME check sizeof(st_index_t) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX msgpack_pack_map(out, (unsigned int)RHASH_SIZE(self)); rb_hash_foreach(self, MessagePack_Hash_to_msgpack_foreach, out); return out; }
/* * Document-method: Symbol#to_msgpack * * call-seq: * symbol.to_msgpack(out = '') -> String * * Serializes the Symbol into raw bytes. */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); const char* name = rb_id2name(SYM2ID(self)); size_t len = strlen(name); msgpack_pack_raw(out, len); msgpack_pack_raw_body(out, name, len); return out; }
/* * Document-method: Bignum#to_msgpack * * call-seq: * bignum.to_msgpack(out = '') -> String * * Serializes the Bignum into raw bytes. */ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); if(RBIGNUM_POSITIVE_P(self)) { msgpack_pack_uint64(out, rb_big2ull(self)); } else { msgpack_pack_int64(out, rb_big2ll(self)); } return out; }
/* * Document-method: Fixnum#to_msgpack * * call-seq: * fixnum.to_msgpack(out = '') -> String * * Serializes the Fixnum into raw bytes. */ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); #ifdef JRUBY msgpack_pack_long(out, FIXNUM_P(self) ? FIX2LONG(self) : rb_num2ll(self)); #else msgpack_pack_long(out, FIX2LONG(self)); #endif return out; }
/* * Document-method: Array#to_msgpack * * call-seq: * array.to_msgpack(out = '') -> String * * Serializes the Array into raw bytes. * This calls to_msgpack method reflectively for internal elements. */ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_array(out, RARRAY_LEN(self)); VALUE* p = RARRAY_PTR(self); VALUE* const pend = p + RARRAY_LEN(self); for(; p != pend; ++p) { rb_funcall(*p, s_to_msgpack, 1, out); } return out; }
/* * Document-method: Bignum#to_msgpack * * call-seq: * bignum.to_msgpack(out = '') -> String * * Serializes the Bignum into raw bytes. */ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); // FIXME bignum if(RBIGNUM_SIGN(self)) { // positive msgpack_pack_uint64(out, rb_big2ull(self)); } else { // negative msgpack_pack_int64(out, rb_big2ll(self)); } return out; }
/* * Document-method: Array#to_msgpack * * call-seq: * array.to_msgpack(out = '') -> String * * Serializes the Array into raw bytes. * This calls to_msgpack method reflectively for internal elements. */ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); // FIXME check sizeof(long) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX msgpack_pack_array(out, (unsigned int)RARRAY_LEN(self)); VALUE* p = RARRAY_PTR(self); VALUE* const pend = p + RARRAY_LEN(self); for(;p != pend; ++p) { rb_funcall(*p, s_to_msgpack, 1, out); } return out; }
/* * Document-method: Symbol#to_msgpack * * call-seq: * symbol.to_msgpack(out = '') -> String * * Serializes the Symbol into raw bytes. */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { #ifdef COMPAT_HAVE_ENCODING return MessagePack_String_to_msgpack(argc, argv, rb_id2str(SYM2ID(self))); #else ARG_BUFFER(out, argc, argv); const char* name = rb_id2name(SYM2ID(self)); size_t len = strlen(name); msgpack_pack_raw(out, len); msgpack_pack_raw_body(out, name, len); return out; #endif }
/* * Document-method: Array#to_msgpack * * call-seq: * array.to_msgpack(out = '') -> String * * Serializes the Array into raw bytes. * This calls to_msgpack method reflectively for internal elements. */ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); // FIXME check sizeof(long) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX unsigned int ary_length = (unsigned int)RARRAY_LEN(self); unsigned int i = 0; msgpack_pack_array(out, ary_length); for(; i < ary_length; ++i) { VALUE p = rb_ary_entry(self, i); rb_funcall(p, s_to_msgpack, 1, out); } return out; }
/* * Document-method: String#to_msgpack * * call-seq: * string.to_msgpack(out = '') -> String * * Serializes the String into raw bytes. */ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); #ifdef COMPAT_HAVE_ENCODING int enc = ENCODING_GET(self); if(enc != s_enc_utf8 && enc != s_enc_ascii8bit && enc != s_enc_usascii) { if(!ENC_CODERANGE_ASCIIONLY(self)) { self = rb_str_encode(self, s_enc_utf8_value, 0, Qnil); } } #endif msgpack_pack_raw(out, RSTRING_LEN(self)); msgpack_pack_raw_body(out, RSTRING_PTR(self), RSTRING_LEN(self)); return out; }
/* * Document-method: FalseClass#to_msgpack * * call-seq: * false.to_msgpack(out = '') -> String * * Serializes false into raw bytes. */ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_false(out); return out; }
/* * Document-method: Float#to_msgpack * * call-seq: * float.to_msgpack(out = '') -> String * * Serializes the Float into raw bytes. */ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_double(out, rb_num2dbl(self)); return out; }
/* * Document-method: Fixnum#to_msgpack * * call-seq: * fixnum.to_msgpack(out = '') -> String * * Serializes the Fixnum into raw bytes. */ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); msgpack_pack_long(out, FIX2LONG(self)); return out; }