public void SerializeDeserialize_ComposedSerializableModel() { var customSerializableModelDetails = GetExampleCustomSerializableModel(); CustomSerializableModel custom = customSerializableModelDetails.Item1; string customExpectedSerializedStr = customSerializableModelDetails.Item2; var standardSerializableModelDetails = GetExampleNonCustomSerializableModel(); NonCustomSerializableModel standard = standardSerializableModelDetails.Item1; string standardExpectedSerializedStr = standardSerializableModelDetails.Item2; var toSerialize = new ComposedSerializableModel(); toSerialize.NormalProperty = "test_string_3"; toSerialize.CustomSerializableSubModel = custom; toSerialize.NonCustomSerializableSubModel = standard; string serialized = JsonConvert.SerializeObject(toSerialize); string expectedSerializedStr = "{ \"composed_serializable_model\": { " + "\"normal_property\": \"test_string_3\", " + "\"custom_serializable_sub_model\":" + customExpectedSerializedStr + ", " + "\"non_custom_serializable_sub_model\":" + standardExpectedSerializedStr + "} }"; TestUtilities.VerifyJsonIsEquivalent <ComposedSerializableModel>(serialized, expectedSerializedStr); var deserialized = JsonConvert.DeserializeObject <ComposedSerializableModel>(serialized); Assert.IsNotNull(deserialized); Assert.AreEqual(toSerialize.NormalProperty, deserialized.NormalProperty); Assert.IsNotNull(deserialized.CustomSerializableSubModel); Assert.AreEqual(toSerialize.CustomSerializableSubModel.PropertyA, deserialized.CustomSerializableSubModel.PropertyA); Assert.AreEqual(toSerialize.CustomSerializableSubModel.PropertyB, deserialized.CustomSerializableSubModel.PropertyB); Assert.AreEqual(toSerialize.CustomSerializableSubModel.PropertyC, deserialized.CustomSerializableSubModel.PropertyC); Assert.AreEqual(toSerialize.CustomSerializableSubModel.PropertyUnNamed, deserialized.CustomSerializableSubModel.PropertyUnNamed); Assert.AreEqual(deserialized.CustomSerializableSubModel.PropertyIgnored, default(int)); Assert.IsNotNull(deserialized.NonCustomSerializableSubModel); Assert.AreEqual(toSerialize.NonCustomSerializableSubModel.PropertyA, deserialized.NonCustomSerializableSubModel.PropertyA); Assert.AreEqual(toSerialize.NonCustomSerializableSubModel.PropertyB, deserialized.NonCustomSerializableSubModel.PropertyB); Assert.AreEqual(toSerialize.NonCustomSerializableSubModel.PropertyC, deserialized.NonCustomSerializableSubModel.PropertyC); Assert.AreEqual(toSerialize.NonCustomSerializableSubModel.PropertyUnNamed, deserialized.NonCustomSerializableSubModel.PropertyUnNamed); Assert.AreEqual(deserialized.NonCustomSerializableSubModel.PropertyIgnored, default(int)); }
public void SerializeDeserialize_CustomSerializableModel() { var customSerializableModel = GetExampleCustomSerializableModel(); CustomSerializableModel toSerialize = customSerializableModel.Item1; string expectedSerializedStr = customSerializableModel.Item2; var serialized = JsonConvert.SerializeObject(toSerialize); TestUtilities.VerifyJsonIsEquivalent <CustomSerializableModel>(serialized, expectedSerializedStr); var deserialized = JsonConvert.DeserializeObject <CustomSerializableModel>(serialized); Assert.IsNotNull(deserialized); Assert.AreEqual(toSerialize.PropertyA, deserialized.PropertyA); Assert.AreEqual(toSerialize.PropertyB, deserialized.PropertyB); Assert.AreEqual(toSerialize.PropertyC, deserialized.PropertyC); Assert.IsNull(deserialized.PropertyC); Assert.AreEqual(toSerialize.PropertyUnNamed, deserialized.PropertyUnNamed); Assert.AreEqual(deserialized.PropertyIgnored, default(int)); }
private static Tuple <CustomSerializableModel, string> GetExampleCustomSerializableModel() { var toSerialize = new CustomSerializableModel(); toSerialize.PropertyA = propA; toSerialize.PropertyB = propB; toSerialize.PropertyC = propC; toSerialize.PropertyIgnored = propIgnored; toSerialize.PropertyUnNamed = propUnNamed; string expectedSerializedStr = "{ " + "\"custom_serializable_model\": { " + "\"property_a\": \"" + propA + "\", " + "\"property_b\": " + propB + ", " + "\"property_c\": " + propCStr + ", " + "\"PropertyUnNamed\": " + propUnNamed + "} " + "}"; return(new Tuple <CustomSerializableModel, string>(toSerialize, expectedSerializedStr)); }