class RocketAMF::Ext::Serializer

Private Class Methods

new(p1) click to toggle source

Initializer

static VALUE ser_initialize(VALUE self, VALUE class_mapper) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);

    ser->class_mapper = class_mapper;
    ser->depth = 0;
    ser->stream = rb_str_buf_new(0);

    return self;
}

Private Instance Methods

serialize(amf_ver, obj) → string click to toggle source

Serialize the given object to the current stream and returns the stream

VALUE ser_serialize(VALUE self, VALUE ver, VALUE obj) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);

    // Process version
    int int_ver = FIX2INT(ver);
    if(int_ver != 0 && int_ver != 3) rb_raise(rb_eArgError, "unsupported version %d", int_ver);
    ser->version = int_ver;

    // Initialize caches
    if(ser->depth == 0) {
        ser->obj_cache = st_init_numtable();
        ser->obj_index = 0;
        if(ser->version == 3) {
            ser->str_cache = st_init_strtable();
            ser->str_index = 0;
            ser->trait_cache = st_init_strtable();
            ser->trait_index = 0;
        }
    }
    ser->depth++;

    // Perform serialization
    if(ser->version == 0) {
        ser0_serialize(self, obj);
    } else {
        ser3_serialize(self, obj);
    }

    // Clean up
    ser->depth--;
    if(ser->depth == 0) ser_free_cache(ser);

    return ser->stream;
}
stream → string click to toggle source

Returns the string that the serializer is writing to

static VALUE ser_stream(VALUE self) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);
    return ser->stream;
}
version → int click to toggle source

Returns the serializer version number, so that a custom encode_amf method knows which version to encode for

static VALUE ser_version(VALUE self) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);
    return INT2FIX(ser->version);
}
write_array(ary) → ser click to toggle source

Serializes the given array to the serializer stream

static VALUE ser_write_array(VALUE self, VALUE ary) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);
    if(ser->version == 0) {
        ser0_write_array(self, ary);
    } else {
        ser3_write_array(self, ary);
    }
    return self;
}
write_object(obj, props=nil) → ser click to toggle source
write_object(obj, props=nil, traits=nil) → ser

Serializes the given object or hash to the serializer stream using the proper serializer version. If given a props hash, uses that instead of using the class mapper to calculate it. If given a traits hash for AMF3, uses that instead of the default dynamic traits with the mapped class name.

static VALUE ser_write_object(int argc, VALUE *argv, VALUE self) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);

    // Check args and call implementation
    VALUE obj;
    VALUE props = Qnil;
    VALUE traits = Qnil;
    if(ser->version == 0) {
        rb_scan_args(argc, argv, "11", &obj, &props);
        ser0_write_object(self, obj, props);
    } else {
        rb_scan_args(argc, argv, "12", &obj, &props, &traits);
        ser3_write_object(self, obj, props, traits);
    }

    return self;
}