Newtonsoft.json: Allow custom `JsonConverter` implementations to serialize `null`

Created on 4 Aug 2013  路  11Comments  路  Source: JamesNK/Newtonsoft.Json

_Resubmitted on GitHub, originally submitted on CodePlex:_ https://json.codeplex.com/workitem/24577


Related to discussion here: https://json.codeplex.com/discussions/280134

JSON.NET will not currently allow custom JsonConverter implementations to serialize null. This is a problem for me (and several others, as evidenced by that discussion).

My suggestion is to either:

  1. Pass a Type of null to CanConvert. This may cause issues for existing custom JsonConverter implementations when upgrading to the latest JSON.NET if they are trying to get properties out of or call methods on the Type argument they're given rather than just comparing the Type for equality with some other Type (i.e. type == typeof(String)).
  2. Add a separate CanConvertNull method that a custom JsonConverter can override but that will return false by default if they don't override it. This preserves backward compatibility of existing custom converters.

Most helpful comment

@JamesNK Could we have an update on this? It's been 5 years.

All 11 comments

@JamesNK: If you can confirm which design you'd prefer, I'll try to put a PR together. Please let me know, thanks!

To me I prefer option 2 that support backward comparability.
Thank you so much for your great library to the world of JSON.

I think this is really cool feature, I found myself and other want to override JSON serialize to return empty string instead of null in some situation that client don't need to handle null value.

Thanks,

Any update on this issue? The problem still exists today with Newtonsoft.Json (7.0.1).

I took a brief attempt at implementing this but it proved more difficult/confusing than I expected given my lack of knowledge on the JSON.NET internals. :confused:

I am using F# and WebSharper and I need to send JSON data to the client, which will be interpreted by WebSharper.

WebSharper has its own JSON format for representing discriminated unions, so I need to serialize all union types with a custom converter. For my own union types, this is not a problem, but for the built-in option<'t> type, I cannot add the JsonConverter attribute, because it's not my source code.

So I created my own contract resolver, which sets a custom converter on the contract for any option<'t> type. But WriteJson only gets called for the Some case, and not the None case.

This is because option<'t> carries the attribute:

[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]

which means that None gets represented as null.

If the runtime value is null, we can't get the type from the value itself, but we could use the declared type of the field, assuming this is available. I think this is what has been suggested.

My workaround for the time being is to use my own equivalent of the option<'t> type, which does not use UseNullAsTrueValue. But this makes my code non-standard.

It would be nice if this was fixed.

@JamesMGreene how come this was a hard problem to solve?

Is this issue fixed? If not, I would like to try to fix it.

@JamesNK Could we have an update on this? It's been 5 years.

I just had a related issue and worked around it by using contract resolver to create a wrapped type that is never null (it does not resolve this issue but it might be of interest to some). https://stackoverflow.com/a/52528455/1094268

Was this page helpful?
0 / 5 - 0 ratings