0

There is a Dictionary where some values are primitive boxed values (bool, int, decimal) and others are null and DBNull.Value. Is it possible to output serialized JSON with undefined keyword for the latter category of entries using JSON.NET?

I was trying to set NullValueHandling.Ignore but that removes null-ed property completely. An implementation of JsonConverter does not help as it is not used on null value.

To illustrate my question consider this object

var toSerialize = new Dictionary<string, object> = 
{
    {"key1": 1}, 
    {"key2": true}, 
    {"key3": DBNull.Value}, 
    {"key4": null}
};

The result should be as

{
  "key1": 1, 
  "key2": true, 
  "key3": undefined, 
  "key4": undefined
}

and not as

{
  "key1": 1, 
  "key2": true, 
  "key3": null, 
  "key4": null
}

the indentation is not important. Thanks.

6
  • 6
    undefined does not exist in json. Only numbers, floats, strings, booleans, objects, arrays and null are valid json entries Commented May 27, 2019 at 10:04
  • AFAIK, an empty object {} is considered as undefined. Commented May 27, 2019 at 10:10
  • {} does not considered as undefined by the test like typeof va === "undefined" Commented May 27, 2019 at 10:14
  • Does null count for that test? Commented May 27, 2019 at 10:29
  • And no, undefined is not legal json, you will not be able to serialize to that in C#. You will have to find some other way of reaching your goal. Commented May 27, 2019 at 10:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.