Class: RocketAMF::Ext::Serializer

Inherits:
Object
  • Object
show all
Defined in:
ext/rocketamf_ext/serializer.c

Instance Method Summary collapse

Constructor Details

#initialize(class_mapper) ⇒ Object

Initializer



688
689
690
691
692
693
694
695
696
697
# File 'ext/rocketamf_ext/serializer.c', line 688

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

Instance Method Details

#serialize(amf_ver, obj) ⇒ String

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

Returns:

  • (String)


730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'ext/rocketamf_ext/serializer.c', line 730

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

#streamString

Returns the string that the serializer is writing to

Returns:

  • (String)


718
719
720
721
722
# File 'ext/rocketamf_ext/serializer.c', line 718

static VALUE ser_stream(VALUE self) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);
    return ser->stream;
}

#versionInteger

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

Returns:

  • (Integer)


706
707
708
709
710
# File 'ext/rocketamf_ext/serializer.c', line 706

static VALUE ser_version(VALUE self) {
    AMF_SERIALIZER *ser;
    Data_Get_Struct(self, AMF_SERIALIZER, ser);
    return INT2FIX(ser->version);
}

#write_array(ary) ⇒ Object

Serializes the given array to the serializer stream



772
773
774
775
776
777
778
779
780
781
# File 'ext/rocketamf_ext/serializer.c', line 772

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) ⇒ Object #write_object(obj, props = nil, traits = nil) ⇒ Object

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.



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'ext/rocketamf_ext/serializer.c', line 794

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