1

I am facing a case where in my JsonConverter I want the JsonWriter to do nothing.

My first attempt was to write:

public override void WriteJson(JsonWriter writer, IFoo<decimal> degrees, JsonSerializer serializer)
{
    var enumerable = Convert(degrees);
    if (enumerable != null)
        JToken.FromObject(enumerable).WriteTo(writer);
}

but it throw the following error

Token PropertyName in state Property would result in an invalid JSON object. Path 'foo[0].bar[1].'.

I can avoid the error by adding an else with a writer.WriteNull() like this:

public override void WriteJson(JsonWriter writer, IFoo<decimal> degrees, JsonSerializer serializer)
{
    var enumerable = Convert(degrees);
    if (enumerable != null)
        JToken.FromObject(enumerable).WriteTo(writer);
    else
        writer.WriteNull();
}

The json output is:

{
    "foo": null
}

But What I would like is an object free from foo: {}.

I know that I could set an attribute with NullValueHandling.Ignore, but there are some cases where I want to keep null.

I would like something like:

public override void WriteJson(JsonWriter writer, IFoo<decimal> degrees, JsonSerializer serializer)
{
    var enumerable = Convert(degrees);
    if (enumerable != null)
        JToken.FromObject(enumerable).WriteTo(writer);
    else
        writer.Skip(); // or writer.Ignore() or writer.DoNothing() or whatever
}

How to tell the JsonWriter to do nothing?

aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • Would some combination of `writer.WriteStartObject`, `WriteEndObject` or `WriteRaw` work? Also `WriteUndefined` might be an option. – Markus Deibel Apr 30 '19 at 13:23
  • `WriteUndefined()` writes `{"foo": undefined}`. I tried `WriteRaw(string.Empty)`, but it throws an error. StartObj and EndObj are going to create `{"foo": {}}`, arent they? – aloisdg Apr 30 '19 at 13:32
  • 1
    I don't think that's something a `JsonConverter` has any control over, it's only responsible for the value itself. The property name has already been written by the parent object at that point, which in turn doesn't have any knowledge about how the converter is going to behave. Have a look at the `ShouldSerializeX` mechanism. – kalimag Apr 30 '19 at 14:49
  • 2
    A `JsonConverter` cannot prevent the value from being written out because the property name has already been written. See [Custom Json.NET converter should not serialize a property](https://stackoverflow.com/q/52671471). Instead you can use conditional property serialization, see [Making a property deserialize but not serialize with json.net](https://stackoverflow.com/q/11564091). In fact I think your question is a duplicate of those two combined, agree? – dbc Apr 30 '19 at 20:12
  • @dbc those links are definitely helpful.The first one answer the question "why is it not possible` and why there is no such things as a function `Skip()` in the writer. The second one contains different ways to answer this problem. I am looking into it to check which one will be well-suited for this case. – aloisdg May 02 '19 at 09:08

0 Answers0