We have JSON that we deserialize into a strongly typed graph of objects in C #. However, we have one problem: sometimes in JSON (for example, an empty string) there is an empty value in the property that maps to a Boolean value in our model.
In our case, we know that in 100% of cases we can translate these "empty" values ββinto Boolean false .
However, the JSON deserializers that I tried about are not aware of this (understandably).
I tried to find a way to intercept deserialization of each property and possibly override the output. Ie, if there was a "interceptor" method that I could register, it looked like this:
public Boolean JsonDeserializationInterceptor(String rawJson, System.Type targetType, out object result) { Boolean overrodeValue = false; result = null; if(targetType == typeof(System.Boolean)) { overrodeValue = true; // We'll pretend that we're interpreting the string "Yes" as // true, and all other values are false. if (rawJson != null && rawJson.ToLower() == "\"yes\"") result = true; else result = false; } return overrodeValue; }
This is just hypothetical, of course, but hopefully it gives you an idea of ββwhat I'm trying to accomplish.
In my research, I could not find a way to do this. I looked at Json.NET , System.Runtime.Serialization.Json.DataContractJsonSerializer and System.Web.Script.Serialization.JavaScriptSerializer . I bet there is a way to do this, and I just couldn't figure it out.
Change I think you could use JsonConverterAttribute , but so far I have not been able to do this.
source share