We've got some JSON that we are deserializing into a strongly-typed object graph in C#. However, we've got one issue: sometimes there is an "empty" value in the JSON (e.g., empty string) in a property that maps to a boolean value in our model.
In our case, we know that 100% of the time, we can translate these "blank" values to Boolean false.
However, the JSON deserializers I've tried don't know about this (understandably).
I've been trying to find a way to intercept the deserialization of each property, and optionally override the output. I.e., if there was an "interceptor" method I could register, that 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;
}
That's just hypothetical, of course, but hopefully it gives you an idea of what I'm trying to accomplish.
In my research I have not been able to figure out a way to do this. I've 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 haven't been able to figure it