Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing null in JSON.NET
    primarykey
    data
    text
    <p>When serializing arbitrary data via JSON.NET, any property that is null is written to the JSON as </p> <blockquote> <p>"propertyName" : null</p> </blockquote> <p>This is correct, of course. </p> <p>However I have a requirement to automatically translate all nulls into the default empty value, e.g. null <code>string</code>s should become <code>String.Empty</code>, null <code>int?</code>s should become <code>0</code>, null <code>bool?</code>s should be <code>false</code>, and so on. </p> <p><code>NullValueHandling</code> is not helpful, since I dont want to <code>Ignore</code> nulls, but neither do I want to <code>Include</code> them (Hmm, new feature?). </p> <p>So I turned to implementing a custom <code>JsonConverter</code>.<br> While the implementation itself was a breeze, unfortunately this still didnt work - <code>CanConvert()</code> is never called for a property that has a null value, and therefore <code>WriteJson()</code> is not called either. Apparently nulls are automatically serialized directly into <code>null</code>, without the custom pipeline. </p> <p>For example, here is a sample of a custom converter for null strings:</p> <pre><code>public class StringConverter : JsonConverter { public override bool CanConvert(Type objectType) { return typeof(string).IsAssignableFrom(objectType); } ... public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { string strValue = value as string; if (strValue == null) { writer.WriteValue(String.Empty); } else { writer.WriteValue(strValue); } } } </code></pre> <p>Stepping through this in the debugger, I noted that neither of these methods are called for properties that have a null value.</p> <p>Delving into JSON.NET's sourcecode, I found that (apparently, I didnt go into a lot of depth) there is a special case checking for nulls, and explictly calling <code>.WriteNull()</code>.</p> <p>For what it's worth, I did try implementing a custom <code>JsonTextWriter</code> and overriding the default <code>.WriteNull()</code> implementation... </p> <pre><code>public class NullJsonWriter : JsonTextWriter { ... public override void WriteNull() { this.WriteValue(String.Empty); } } </code></pre> <p>However, this can't work well, since the <code>WriteNull()</code> method knows nothing about the underlying datatype. So sure, I can output <code>""</code> for any null, but that doesnt work well for e.g. int, bool, etc. </p> <p>So, my question - short of converting the entire data structure manually, is there any solution or workaround for this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload