Why is JsonConverter ignoring the WriteIndented option?

Viewed 438

I created a new .NET Core console app and changed the Program.cs file to the following:

using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JsonSerialization
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            var options = new JsonSerializerOptions {WriteIndented = true};

            using (var fs = new FileStream("output.json", FileMode.Create, FileAccess.Write, FileShare.None))
            using (var writer = new Utf8JsonWriter(fs))
            {
                JsonSerializer.Serialize(writer, new C(), options);
            }
        }
    }

    [JsonConverter(typeof(S))]
    class C
    {}

    class S: JsonConverter<C>
    {
        public override C Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            throw new NotImplementedException();
        }

        public override void Write(Utf8JsonWriter writer, C value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();

            writer.WriteString("a", "b");
            writer.WriteString("c", "d");

            writer.WriteEndObject();
        }
    }
}

When I run the program, a file named "output.json" is created in the directory containing the executable, as expected. The contents of the file are as follows:

{"a":"b","c":"d"}

I expected instead to see this:

{
    "a": "b",
    "c": "d"
}

The provided options setting (WriteIndented = true) is being ignored. Why is this?

I have debugged through the program and verified that my Write() implementation in S is being called (breakpoints set within the method are hit) and that the JsonSerializerOptions I passed in is the one available in the method (or at least, it has WriteIndented set to true, like the one I passed in).

It occurred to me that I might actually be required to do the indenting myself since I'm implementing a serializer. But I looked at the methods available on Utf8JsonWriter, and there doesn't appear to be a method that adds whitespace to the string being written. So I don't think that's what's wrong.

1 Answers

It occurred to me that I might actually be required to do the indenting myself since I'm implementing a serializer. But I looked at the methods available on Utf8JsonWriter, and there doesn't appear to be a method that adds whitespace to the string being written. So I don't think that's what's wrong.

I think this is indeed the case. Utf8JsonWriter has another constructor available that takes an instance of JsonWriterOptions. So your main function could be this instead:

public static void Main(string[] args)
{
    var options = new JsonWriterOptions { Indented = true };

    using (var fs = new FileStream("output.json", FileMode.Create, FileAccess.Write, FileShare.None))
    using (var writer = new Utf8JsonWriter(fs, options))
    {
        JsonSerializer.Serialize(writer, new C());
    }
}
Related