public void Initialize() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings( this.GetType().GetTypeInfo() .Assembly.GetTypes() .Where(t => !t.GetTypeInfo().GetCustomAttributes(typeof(NonDefaultTestMapperAttribute), false).Any()) .ToArray()); }
public void Test_Deserialize_Class_With_MultipleLevelDiscriminatorsDefined_SerializedNone() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMapBaseDiscriminator), typeof(EditorModelMapDoubleDiscriminator), typeof(TextBoxModelMap), }); Throws <Exception>( "Cannot create object of type `FluentJsonNet.Tests.Models.Controls.ControlModel`", () => JsonConvert.DeserializeObject <ControlModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\"}")); }
public void Test_Deserialize_SubclassMiddle_With_MultipleLevelDiscriminatorsDefined_ButSerializedFinalWrong() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMapBaseDiscriminator), typeof(EditorModelMapDoubleDiscriminator), typeof(TextBoxModelMap), }); Throws <Exception>( "Value of discriminator field not verified by any subclass map.", () => JsonConvert.DeserializeObject <EditorModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\",\"editor\":\"CheckBox\"}")); }
public void Test_Deserialize_SubclassMiddle_Without_Discriminator_NotSetNorDefined() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMap), typeof(EditorModelMapNoDiscriminator), typeof(TextBoxModelMapNoDiscriminator), }); Throws <Exception>( "Cannot create object of type `FluentJsonNet.Tests.Models.Controls.EditorModel`", () => JsonConvert.DeserializeObject <EditorModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\"}")); }
public void Test_Deserialize_SubclassMiddle_With_Discriminator_NotSetButDefined() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMap), typeof(EditorModelMap), typeof(TextBoxModelMapNoDiscriminator), }); Throws <Exception>( "Value of discriminator field not verified by any subclass map.", () => JsonConvert.DeserializeObject <EditorModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\",\"editor\":\"TextBox\"}")); }
public void Test_Serialize_Subclass_With_MultipleLevelDiscriminators() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMapBaseDiscriminator), typeof(EditorModelMapDoubleDiscriminator), typeof(TextBoxModelMap), }); var jsonTextBox = JsonConvert.SerializeObject(new TextBoxModel { Default = "A", Name = "N", Text = "T" }); AreEqual(jsonTextBox, "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\",\"editor\":\"TextBox\",\"type\":\"editor\"}"); }
public void Test_Serialize_Subclass_Without_Discriminator_NorDefinedNorSet() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMap), typeof(EditorModelMapNoDiscriminator), typeof(TextBoxModelMapNoDiscriminator), }); var jsonTextBox = JsonConvert.SerializeObject(new TextBoxModel { Default = "A", Name = "N", Text = "T" }); AreEqual(jsonTextBox, "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\"}"); }
public void Test_Deserialize_SubclassFinal_With_MultipleLevelDiscriminators() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMapBaseDiscriminator), typeof(EditorModelMapDoubleDiscriminator), typeof(TextBoxModelMap), }); var textBox = JsonConvert.DeserializeObject <TextBoxModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\",\"editor\":\"TextBox\",\"type\":\"editor\"}"); AreEqual("T", textBox.Text); AreEqual("A", textBox.Default); AreEqual("N", textBox.Name); }
public void Test_Serialize_Subclass_With_Discriminator_DefinedInMiddleClass_ButNotSet() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMap), typeof(EditorModelMap), typeof(TextBoxModelMapNoDiscriminator), }); Throws <Exception>( "Value of discriminator field not defined by subclass map.", () => JsonConvert.SerializeObject(new TextBoxModel { Default = "A", Name = "N", Text = "T" })); }
public void Test_Serialize_Subclass_With_Discriminator_DefinedNowhere() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMap), typeof(EditorModelMapNoDiscriminator), typeof(TextBoxModelMap), }); Throws <Exception>( "Discriminating value not set to any field.", () => JsonConvert.SerializeObject(new TextBoxModel { Default = "A", Name = "N", Text = "T" })); }
public void Test_Deserialize_SubclassFinal_Without_Discriminator_SetButNotDefined() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMap), typeof(EditorModelMapNoDiscriminator), typeof(TextBoxModelMap), }); var textBox = JsonConvert.DeserializeObject <TextBoxModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\"}"); AreEqual("T", textBox.Text); AreEqual("A", textBox.Default); AreEqual("N", textBox.Name); }
public void Test_Deserialize_Class_With_MultipleLevelDiscriminatorsDefined_SerializedMultiple() { JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(new[] { typeof(ControlModelMapBaseDiscriminator), typeof(EditorModelMapDoubleDiscriminator), typeof(TextBoxModelMap), }); var control = JsonConvert.DeserializeObject <ControlModel>( "{\"text\":\"T\",\"default\":\"A\",\"name\":\"N\",\"editor\":\"TextBox\",\"type\":\"editor\"}"); IsInstanceOfType(control, typeof(TextBoxModel)); var textBox = (TextBoxModel)control; AreEqual("T", textBox.Text); AreEqual("A", textBox.Default); AreEqual("N", textBox.Name); }
public static void InitMapping() { var mappings = ReflectiveEnumerator.GetSubclasses <JsonMap>(true); JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(mappings.ToArray()); }