The behavior of the JsonTextWriter
in this instance is correct; to represent a null value in JSON, the expected token is null
. For example:
{
"Name" : "SomeGuy",
"Address" : null
}
From your question, it seems that what you're looking to do is write an actual empty string to indicate a complete lack of value. You could do that by calling jsonTextWriter.WriteRaw("")
. However, that will produce invalid JSON, as you'd end up with:
{
"Name" : "SomeGuy",
"Address" :
}
I'm guessing that isn't want you want, either. To emit an empty string that actually represents an empty string value in JSON, you'll want to do jsonTextWriter.WriteValue("")
That will produce the expected ""
token that I believe you are looking for, which would look like:
{
"Name" : "SomeGuy",
"Address" : ""
}
As a full example, to emit the object that I've been using would be something like:
var writer = new StringWriter();
var textWriter = (TextWriter)writer;
var jsonTextWriter = new JsonTextWriter(textWriter);
jsonTextWriter.WriteStartObject();
jsonTextWriter.WritePropertyName("Name");
jsonTextWriter.WriteValue("SomeGuy");
jsonTextWriter.WritePropertyName("Address");
jsonTextWriter.WriteValue("");
jsonTextWriter.WriteEndObject();
var data = writer.ToString();
Hope that helps.