1

When using Json.NET, I am trying to create a JSON structure dynamically using the "JSON to LINQ" support.

In the following, jObject is a JObject and JObject.Add takes (string, JToken). However, I cannot find out how to add either an Undefined or a Null token - nor can I find out how to create JValues with the appropriate Null/Undefined type.

string value = GetValue();
if (value == "undefined") {
  jObject.Add(key, /* how to add "Undefined" token? */);
} else if (value == "null") {
  jObject.Add(key, /* how to add "Null" token? */);
} else {
  jObject.Add(key, new JToken(value));  /* String value/token */
}

How do I explicitly add a JToken/JValue for a JSON Undefined? How about for a JSON Null?

user2864740
  • 60,010
  • 15
  • 145
  • 220

3 Answers3

10

Setting a property to undefined is functionally equivalent to not setting the property at all. So, if the property is undefined, you just don't add any properties to your JObject. (And this is probably the reason why undefined isn't supported by JSON — it's completely redundant.)

Null is represented by new JValue((object)null), if I remember correctly.

P.S. With your code, you won't be able to tell the difference between a string "undefined" and an undefined value, as well as between "null" and null. You probably need to rethink your design.

Athari
  • 33,702
  • 16
  • 105
  • 146
  • Oh, my apologizes: `undefined` is *not* valid JSON. This is *the* point, not a secondary point of "functionally equivalent". It simply is invalid in context (probably preserved for read-*in* compatibility) and my assumptions were wrong. Using `new JValue((object)null))` works as I expect here - to emit a `null`, naturally. – user2864740 Feb 17 '14 at 05:54
  • As @Rohit says, JValue has methods for create special values, such as JValue.CreateUndefined() and JValue.CreateNull() – marbel82 Nov 28 '18 at 10:16
3
jObject.Add(key, /* how to add "Undefined" token? */);

jObject.Add(key,JValue.CreateUndefined());
Rohit
  • 3,610
  • 7
  • 45
  • 76
  • It looks like it is an alias for the most part: "CreateUndefined(): Creates a JValue *null* value." I wonder if there is a difference/use-case for such, going back my incorrect assumption that undefined values were allowed in JSON. The corresponding CreateNull() method answers the other question well. – user2864740 Nov 22 '15 at 20:25
  • 1
    there is a different JTokenType, so unlikely is same thing. https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Linq/JValue.cs – Rohit Nov 22 '15 at 20:32
2

According to this question, it doesn't support undefined.

After searching the docs, I found 2 references to "Undefined", but no explanation on how they might be used:

Community
  • 1
  • 1
Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • I suspect that they can't be used properly. The only way to use that is to use a custom `JsonConverter` and call `WriteUndefined` in `WriteJson`. Useless for `JObject` construction and reading JSON. – Athari Feb 17 '14 at 05:49
  • (Thanks for the linked post. I read "..undefined is NOT listed. Object does also not count as undefined" and slapped my head.) – user2864740 Feb 17 '14 at 06:02