public void CanSerializeAndDeserializeComplexHierarchies() { var complexHierarchy = ComplexSerializationHierarchy.CreateComplexNonCatelHierarchy(); TestSerializationOnAllSerializers((serializer, description) => { var deserializedObject = SerializationTestHelper.SerializeAndDeserialize(complexHierarchy, serializer); Assert.AreEqual(complexHierarchy.LastName, deserializedObject.LastName, description); Assert.AreEqual(complexHierarchy.Persons.Count, deserializedObject.Persons.Count, description); for (int i = 0; i < deserializedObject.Persons.Count; i++) { var expectedPerson = complexHierarchy.Persons[i]; var actualPerson = deserializedObject.Persons[i]; Assert.AreEqual(expectedPerson.Gender, actualPerson.Gender, description); Assert.AreEqual(expectedPerson.FirstName, actualPerson.FirstName, description); Assert.AreEqual(expectedPerson.LastName, actualPerson.LastName, description); } }); }
public void CanSerializeDictionary() { var dictionary = new Dictionary <string, int>(); dictionary.Add("skip", 1); dictionary.Add("take", 2); dictionary.Add("some other string", 3); TestSerializationOnAllSerializers((serializer, description) => { var deserializedObject = SerializationTestHelper.SerializeAndDeserialize(dictionary, serializer); Assert.AreEqual(dictionary.Count, deserializedObject.Count, description); Assert.IsTrue(deserializedObject.ContainsKey("skip")); Assert.AreEqual(1, deserializedObject["skip"]); Assert.IsTrue(deserializedObject.ContainsKey("take")); Assert.AreEqual(2, deserializedObject["take"]); Assert.IsTrue(deserializedObject.ContainsKey("some other string")); Assert.AreEqual(3, deserializedObject["some other string"]); }); }
private static void TestSerializationOnAllSerializers(Action <ISerializer, string> action, bool testWithoutGraphIdsAsWell = true, ISerializationManager serializationManager = null) { if (serializationManager == null) { serializationManager = new SerializationManager(); } var serializers = new List <ISerializer>(); serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.Performance, serializationManager)); serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.PrettyXml, serializationManager)); serializers.Add(SerializationTestHelper.GetBinarySerializer(serializationManager)); serializers.Add(SerializationTestHelper.GetJsonSerializer(serializationManager)); if (testWithoutGraphIdsAsWell) { var basicJsonSerializer = SerializationTestHelper.GetJsonSerializer(serializationManager); basicJsonSerializer.PreserveReferences = false; basicJsonSerializer.WriteTypeInfo = false; serializers.Add(basicJsonSerializer); } foreach (var serializer in serializers) { var typeName = serializer.GetType().GetSafeFullName(false); Log.Info(); Log.Info(); Log.Info(); Log.Info("=== TESTING SERIALIZER: {0} ===", typeName); Log.Info(); Log.Info(); Log.Info(); action(serializer, typeName); } }
public void SerializesModelsWithOnlyAttributes(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.ModelsWithAttributesOnly.Add(new XmlModelWithAttributesOnly { FirstName = "Geert", }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(), new XmlSerializationConfiguration { OptimalizationMode = mode }); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.ModelsWithAttributesOnly.Count); var newModelWithAttributesOnly = newFamily.ModelsWithAttributesOnly.First(); Assert.AreEqual(family.ModelsWithAttributesOnly[0].FirstName, newModelWithAttributesOnly.FirstName); }
public void RespectsTheXmlAttributeAttributeOnRootElements(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.Persons.Add(new XmlPerson { FirstName = "Geert", LastName = family.LastName, Gender = Gender.Male }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(mode)); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.Persons.Count); var newPerson = newFamily.Persons.First(); Assert.AreEqual(family.Persons[0].FirstName, newPerson.FirstName); Assert.AreEqual(family.Persons[0].LastName, newPerson.LastName); Assert.AreEqual(family.Persons[0].Gender, newPerson.Gender); }
public void CorrectlySerializesObjectsWithFormattedIndents() { var serviceLocator = ServiceLocator.Default; var serializer = serviceLocator.ResolveType <IJsonSerializer>(); var model = new CustomJsonSerializationModel { FirstName = "Geert" }; var configuration = new JsonSerializationConfiguration { Formatting = Newtonsoft.Json.Formatting.Indented }; var clonedModel = SerializationTestHelper.SerializeAndDeserialize(model, serializer, configuration); // Note: yes, the *model* is serialized, the *clonedModel* is deserialized Assert.IsTrue(model.IsCustomSerialized); Assert.IsTrue(clonedModel.IsCustomDeserialized); Assert.AreEqual(model.FirstName, clonedModel.FirstName); }
public void SupportsNestedHierarchySerialization() { var serializer = SerializationFactory.GetXmlSerializer(); var root = new ModelBaseFacts.Group() { Name = "myRoot" }; var child = new ModelBaseFacts.Group() { Name = "myChild" }; root.Items = new ObservableCollection <ModelBaseFacts.Item>(); root.Items.Add(child); var newRoot = SerializationTestHelper.SerializeAndDeserialize(root, serializer); Assert.IsNotNull(newRoot); Assert.AreEqual("myRoot", newRoot.Name); Assert.AreEqual(1, newRoot.Items.Count); Assert.AreEqual("myChild", newRoot.Items[0].Name); }
public void CorrectlySerializesCustomizedModels() { var testModel = new TestModel(); testModel._excludedField = "excluded"; testModel._includedField = "included"; testModel.ExcludedRegularProperty = "excluded"; testModel.IncludedRegularProperty = "included"; testModel.ExcludedCatelProperty = "excluded"; testModel.IncludedCatelProperty = "included"; var clonedModel = SerializationTestHelper.SerializeAndDeserialize(testModel, SerializationFactory.GetBinarySerializer()); Assert.AreEqual(null, clonedModel._excludedField); Assert.AreEqual("included", clonedModel._includedField); Assert.AreEqual(null, clonedModel.ExcludedRegularProperty); Assert.AreEqual("included", clonedModel.IncludedRegularProperty); Assert.AreEqual(null, clonedModel.ExcludedCatelProperty); Assert.AreEqual("included", clonedModel.IncludedCatelProperty); }
private static void TestSerializationOnAllSerializers(Action <ISerializer, ISerializationConfiguration, string> action, bool testWithoutGraphIdsAsWell = true, ISerializationManager serializationManager = null) { if (serializationManager == null) { serializationManager = new SerializationManager(); } var serializerConfigurations = new Dictionary <Type, List <ISerializationConfiguration> >(); serializerConfigurations[typeof(XmlSerializer)] = new List <ISerializationConfiguration>(new[] { new SerializationConfiguration() }); serializerConfigurations[typeof(BinarySerializer)] = new List <ISerializationConfiguration>(new[] { new SerializationConfiguration() }); serializerConfigurations[typeof(JsonSerializer)] = new List <ISerializationConfiguration>(new[] { new JsonSerializationConfiguration { UseBson = false }, new JsonSerializationConfiguration { UseBson = true }, }); var serializers = new List <ISerializer>(); serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.Performance, serializationManager)); serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.PrettyXml, serializationManager)); serializers.Add(SerializationTestHelper.GetBinarySerializer(serializationManager)); serializers.Add(SerializationTestHelper.GetJsonSerializer(serializationManager)); if (testWithoutGraphIdsAsWell) { var basicJsonSerializer = SerializationTestHelper.GetJsonSerializer(serializationManager); basicJsonSerializer.PreserveReferences = false; basicJsonSerializer.WriteTypeInfo = false; serializers.Add(basicJsonSerializer); } foreach (var serializer in serializers) { var type = serializer.GetType(); var typeName = type.GetSafeFullName(false); var configurations = serializerConfigurations[type]; foreach (var configuration in configurations) { Log.Info(); Log.Info(); Log.Info(); Log.Info("=== TESTING SERIALIZER: {0} ===", typeName); Log.Info(); Log.Info(); Log.Info(); action(serializer, configuration, typeName); } } }