public void DeserializeUnknownType() { ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 }; var bytes = Serializer.Serialize(instance); bytes[25] = 54; Assert.Throws <TypeNotFoundException>(() => Deserializer.Deserialize <ClassWithDynamicProperty>(bytes)); }
public void UnexpectedVersionHeaderShouldThrowAnException() { ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 }; var bytes = Serializer.Serialize(instance); // this is a hackish way to change the version of a serialized object bytes[0] = (byte)(bytes[0] + 1); Assert.Throws <InvalidSerializationVersionException>(() => Deserializer.Deserialize <ClassWithDynamicProperty>(bytes)); }
private static ClassWithDynamicProperty GetExpected(ClassWithDynamicProperty expected) { if (expected == null) { return(null); } dynamic foo = new ExpandoObject(); foo.Bar = new ExpandoObject(); foo.Bar.Baz = expected.DynamicProperty.Bar.Baz; foo.Qux = expected.DynamicProperty.Qux; expected.DynamicProperty = foo; return(expected); }
public void DeserializeTypeThatWasModified() { ClassWithDynamicProperty instance = new ClassWithDynamicProperty { Value = 123 }; var bytes = Serializer.Serialize(instance); var needle = SerializedTypeResolver.GetShortNameFromType(typeof(ClassWithDynamicProperty)); var index = System.Text.Encoding.UTF8.GetString(bytes).IndexOf(needle); // this is a hackish way to change the hashcode of a serialized object // if the way/order (currently TypeName + Hash) that an object is serialized changes the line below will need to be modified to target a byte of the hashcode bytes[index + needle.Length + 1] = (bytes[index + needle.Length + 1] == 255) ? SerializerObjectTracker.Value0 : (byte)(bytes[index + needle.Length] + 1); // change the hashcode to something invalid Assert.Throws <TypeWasModifiedSinceSerializationException>(() => Deserializer.Deserialize <ClassWithDynamicProperty>(bytes)); }
public void ADynamicPropertyWithAnEmptyValueRoundTripsFromXmlCorrectly(bool shouldTreatEmptyElementAsString) { dynamic dynamicProperty = new ExpandoObject(); dynamicProperty.Qwerty = new object(); dynamicProperty.Uiop = ""; var instance = new ClassWithDynamicProperty { IntProperty = 123, DynamicProperty = dynamicProperty }; var serializer = new XmlSerializer <ClassWithDynamicProperty>(options => { if (shouldTreatEmptyElementAsString) { options.Indent().ShouldTreatEmptyElementAsString(); } else { options.Indent(); } }); var xml = serializer.Serialize(instance); var roundTripInstance = serializer.Deserialize(xml); var roundTripXml = serializer.Serialize(roundTripInstance); if (shouldTreatEmptyElementAsString) { Assert.That(roundTripInstance.IntProperty, Is.EqualTo(instance.IntProperty)); Assert.That(roundTripInstance.DynamicProperty.Uiop, Is.EqualTo(instance.DynamicProperty.Uiop)); // When we treat empty elements as strings, we won't deserialize as the original type. Assert.That(roundTripInstance.DynamicProperty.Qwerty, Is.Not.EqualTo(instance.DynamicProperty.Qwerty)); Assert.That(roundTripInstance.DynamicProperty.Qwerty, Is.EqualTo("")); } else { Assert.That(roundTripInstance, Has.PropertiesEqualTo(instance)); } Assert.That(roundTripXml, Is.EqualTo(xml)); }
public void Setup() { simpleClass = new SimpleClass() { Age = 62, Name = "Me" }; classWithMember = new ClassWithMember(); classWithCollection = new ClassWithCollection(); classWithDynamicProperty = new ClassWithDynamicProperty() { Id = Guid.NewGuid(), ADynamicPrioperty = new SimpleClass() }; recursiveClassPrincipal = new RecursiveClassPrincipal() { DependantOn = new RecursiveClassDependant() { PrincipalFrom = new RecursiveClassPrincipal(), Number = 2 }, Count = 1 }; }
public void ADynamicPropertyWithAZeroPaddedNumberValueRoundTripsFromXmlCorrectly() { dynamic dynamicProperty = new ExpandoObject(); dynamicProperty.Qwerty = "000123"; var instance = new ClassWithDynamicProperty { IntProperty = 789, DynamicProperty = dynamicProperty }; var serializer = new XmlSerializer <ClassWithDynamicProperty>(options => options.Indent()); var xml = serializer.Serialize(instance); var roundTripInstance = serializer.Deserialize(xml); var roundTripXml = serializer.Serialize(roundTripInstance); Assert.That(roundTripInstance.IntProperty, Is.EqualTo(instance.IntProperty)); Assert.That(roundTripInstance.DynamicProperty.Qwerty, Is.EqualTo(instance.DynamicProperty.Qwerty)); Assert.That(roundTripXml, Is.EqualTo(xml)); }
private static ClassWithDynamicProperty GetExpected(ClassWithDynamicProperty expected) { return(expected); }
protected virtual ClassWithDynamicProperty GetExpected(ClassWithDynamicProperty expected) { return(expected); }