public void ValueDeserializesWithUnderlyingType2() { AddConsoleListener(); var jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented, }; var jsonDeserializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented, Converters = new List <JsonConverter>() { new UnderlyingTypeConverter() } }; var dotColor = new ZoneProgramInput("DelayTime", typeof(int)); dotColor.SetValue(1); var serializedDotColor = JsonConvert.SerializeObject(dotColor, jsonSerializerSettings); var deserializedDotColor = JsonConvert.DeserializeObject <ZoneProgramInput>(serializedDotColor, jsonDeserializerSettings); Console.WriteLine(deserializedDotColor.Value.GetType()); Assert.True(deserializedDotColor.Value is int); }
public void ValueDeserializesWithUnderlyingType3() { AddConsoleListener(); var jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented, }; var jsonDeserializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented, Converters = new List <JsonConverter>() { new UnderlyingTypeConverter() } }; var dotColor = new ZoneProgramInput("DelayTime", typeof(ComplexClass)); dotColor.SetValue(new ComplexClass() { Name = "ComplexClass", SimpleClass = new SimpleClass() { Name = "SimpleClass", AppDomainSetups = new List <AppDomainSetup>() { new AppDomainSetup() { ApplicationName = "TestAppDomainSetup" } } } }); var serializedDotColor = JsonConvert.SerializeObject(dotColor, jsonSerializerSettings); var deserializedDotColor = JsonConvert.DeserializeObject <ZoneProgramInput>(serializedDotColor, jsonDeserializerSettings); Console.WriteLine(deserializedDotColor.Value.GetType()); Assert.True(deserializedDotColor.Value is ComplexClass); }
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var result = new ZoneProgramInput(); // Deserialize into a temporary JObject JObject obj = serializer.Deserialize <JObject>(reader); // Populate the ZoneProgramInput object with the contents serializer.Populate(obj.CreateReader(), result); // Overwrite the "Value" property with the correct value based on the // "Type" property. result.Value = obj.GetValue("value", StringComparison.OrdinalIgnoreCase) .ToObject(result.Type, serializer); return(result); }