示例#1
0
        public void Should_Deserialize_Value_Type_Properties()
        {
            var mapping = ObjectSerializationMapper.Create()
                          .FromType <ValueTypePropertyTestClass>()
                          .PublicProperties()
                          .Map();

            var deserializationOps = ObjectSerializerCompiler.CompileDeserializationOps(mapping).ToList().AsReadOnly();

            var context = ObjectSerializerInterpreter.InterpretObjectSerializationNullContext(
                typeof(ValueTypePropertyTestClass),
                deserializationOps,
                ObjectSerializerProgram.GetOpValueSerializerTypes(deserializationOps).ToList().AsReadOnly()
                );

            var deserializeDelegate = ObjectSerializerInterpreter.InterpretDynamicDeserializeDelegate(
                context,
                typeof(ValueTypePropertyTestClass),
                deserializationOps
                );

            var buffer = new byte[32];

            MemoryMapper.WriteInt(300, buffer, 0);
            MemoryMapper.WriteInt(400, buffer, sizeof(int));

            var results = (ValueTypePropertyTestClass)deserializeDelegate(context, buffer, 0);

            Assert.Equal(300, results.X);
            Assert.Equal(400, results.Y);
        }
示例#2
0
        public void Should_Deserialize_Non_Value_Type_Fields()
        {
            var mapping = ObjectSerializationMapper.Create()
                          .FromType <NonValueTypeFieldTestClass>()
                          .PublicFields()
                          .Map();

            var deserializationOps = ObjectSerializerCompiler.CompileDeserializationOps(mapping).ToList().AsReadOnly();

            var context = ObjectSerializerInterpreter.InterpretObjectSerializationNullContext(
                typeof(NonValueTypeFieldTestClass),
                deserializationOps,
                ObjectSerializerProgram.GetOpValueSerializerTypes(deserializationOps).ToList().AsReadOnly()
                );

            var deserializeDelegate = ObjectSerializerInterpreter.InterpretDynamicDeserializeDelegate(
                context,
                typeof(NonValueTypeFieldTestClass),
                deserializationOps
                );

            var stringSerializer   = new StringSerializer();
            var bitFieldSerializer = new BitFieldSerializer();
            var nullMask           = new BitField(1);

            nullMask.SetBit(0, true);

            var buffer = new byte[64];
            var offset = 0;

            bitFieldSerializer.Serialize(nullMask, buffer, offset);
            offset += bitFieldSerializer.GetSizeFromValue(nullMask);

            // X.
            MemoryMapper.WriteInt(25, buffer, offset);
            offset += sizeof(int);

            // Y.
            MemoryMapper.WriteInt(50, buffer, offset);
            offset += sizeof(int);

            // S2.
            stringSerializer.Serialize("hello!", buffer, offset);

            var results = (NonValueTypeFieldTestClass)deserializeDelegate(context, buffer, 0);

            Assert.Equal(25, results.X);
            Assert.Equal(50, results.Y);
            Assert.Equal("hello!", results.S2);

            Assert.Null(results.S1);

            Assert.True(string.IsNullOrEmpty(results.Null));
        }
示例#3
0
        public void Should_Deserialize_Nullable_Properties()
        {
            var mapping = ObjectSerializationMapper.Create()
                          .FromType <NullablePropertyTestClass>()
                          .PublicProperties()
                          .Map();

            var deserializationOps = ObjectSerializerCompiler.CompileDeserializationOps(mapping).ToList().AsReadOnly();

            var context = ObjectSerializerInterpreter.InterpretObjectSerializationNullContext(
                typeof(NullablePropertyTestClass),
                deserializationOps,
                ObjectSerializerProgram.GetOpValueSerializerTypes(deserializationOps).ToList().AsReadOnly()
                );

            var deserializeDelegate = ObjectSerializerInterpreter.InterpretDynamicDeserializeDelegate(
                context,
                typeof(NullablePropertyTestClass),
                deserializationOps
                );

            var bitFieldSerializer = new BitFieldSerializer();
            var nullMask           = new BitField(1);

            nullMask.SetBit(0, true);

            var buffer = new byte[32];
            var offset = 0;

            bitFieldSerializer.Serialize(nullMask, buffer, offset);
            offset += bitFieldSerializer.GetSizeFromValue(nullMask);

            MemoryMapper.WriteInt(50, buffer, offset);
            offset += sizeof(int);

            MemoryMapper.WriteInt(75, buffer, offset);
            offset += sizeof(int);

            MemoryMapper.WriteInt(100, buffer, offset);

            var results = (NullablePropertyTestClass)deserializeDelegate(context, buffer, 0);

            Assert.Equal(50, results.I);
            Assert.Equal(75, results.X);
            Assert.Equal(100, results.J);
        }
示例#4
0
        public void Should_Deserialize_Objects_Containing_Value_Type_Fields_And_Properties_With_Parametrized_Constructor()
        {
            var mapping = ObjectSerializationMapper.Create()
                          .FromType <ValueTypePropertyAndFieldWithParametrizedConstructorTestClass>()
                          .PublicProperties()
                          .ParametrizedActivation(ObjectActivationHint.Field("x", "X"),
                                                  ObjectActivationHint.Field("y", "Y"),
                                                  ObjectActivationHint.Property("x2", "X2"),
                                                  ObjectActivationHint.Property("y2", "Y2"))
                          .Map();

            var deserializationOps = ObjectSerializerCompiler.CompileDeserializationOps(mapping).ToList().AsReadOnly();

            var context = ObjectSerializerInterpreter.InterpretObjectSerializationNullContext(
                typeof(ValueTypePropertyAndFieldWithParametrizedConstructorTestClass),
                deserializationOps,
                ObjectSerializerProgram.GetOpValueSerializerTypes(deserializationOps).ToList().AsReadOnly()
                );

            var deserializeDelegate = ObjectSerializerInterpreter.InterpretDynamicDeserializeDelegate(
                context,
                typeof(ValueTypePropertyAndFieldWithParametrizedConstructorTestClass),
                deserializationOps
                );

            var buffer = new byte[32];

            MemoryMapper.WriteInt(10, buffer, 0);
            MemoryMapper.WriteInt(20, buffer, sizeof(int));
            MemoryMapper.WriteInt(30, buffer, sizeof(int) * 2);
            MemoryMapper.WriteInt(40, buffer, sizeof(int) * 3);

            var results = (ValueTypePropertyAndFieldWithParametrizedConstructorTestClass)deserializeDelegate(context, buffer, 0);

            Assert.Equal(10, results.X);
            Assert.Equal(20, results.Y);
            Assert.Equal(30, results.X2);
            Assert.Equal(40, results.Y2);
        }