1

In general I like to use the .NET System.Text.Json JsonSerializerOptions.WriteIndented = true; to have nice readable output of Json. However, for objects which have just one property I would like to override the serialization and make it less verbose. So for:

public class SimpleObj
{
    public string Text { get; set; }
}

instead of getting :

{
  "text": "this is the value"
}

I would like to have:

{ "text": "this is the value" }

If I use JsonSerializerOptions.WriteIndented = false; then my whole output is less verbose but not what I am looking for. Is there some kind of extension point or override I can use to achieve this locally for specific objects?

dbc
  • 104,963
  • 20
  • 228
  • 340
phil
  • 1,938
  • 4
  • 23
  • 33
  • Yes it is possible, but you would have to reimplement `Utf8JsonWriter`, removing the code from [`WriteNewLine`](https://github.com/dotnet/corefx/blob/b8b81a66738bb10ef0790023598396861d92b2c4/src/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs#L999) or changing when it is called, see link for full source code and note that it's a partial class in a number of files. Alternatively you could just `Replace(Environment.NewLine, "")` on the result to remove all newlines – Charlieface Jan 02 '22 at 15:16
  • You could create an extension method or some kind of wrapper that would switch the flags based on the number of properties. The number of properties could easly be found with reflection. – Robert Jan 02 '22 at 15:18
  • Json is a machine readable serialisation format, that is also farily human readable. Are you solving a real problem here, or just to make things look nice when it's not really necessary? – Neil Jan 02 '22 at 15:20
  • 1
    @Neil Well my use case requires a nice compact, yet fairly human readable output. Where I have several single property objects, removing a few line breaks improves readability and reduces white space. For Objects which have many properties, having line beaks and indentation is great for documentation and debugging. – phil Jan 03 '22 at 13:23
  • 1
    Related or duplicate: [In System.Text.Json is it possible to specify custom indentation rules?](https://stackoverflow.com/q/63376873/3744182). – dbc Jan 05 '22 at 23:26

1 Answers1

0

The solution I have chosen is to implement a class which implements JsonConverter<SimpleObj> which allows me to override the Read(...) and Write(...) methods called during serialization. Within Write() I can call Utf8JsonWriter.WriteRawValue() with a SimpleObj.ToString() override as a parameter which gives me exactly what I want.

public override void Write(Utf8JsonWriter writer, SimpleObj value, JsonSerializerOptions options)
{
  string ind = new String('\t', writer.CurrentDepth);
  writer.WriteRawValue(ind + value.ToString());
}
public class SimpleObj
{
  public string override ToString() => $"{{ \"text\": \"{text}\" }}";
}
phil
  • 1,938
  • 4
  • 23
  • 33
  • 1
    This won't work properly if `text` contains quotes or other special characters that JSON cares about. I would recommend *not* trying to do this kind of serialization yourself and instead enlist the help of the classes that know how to do it properly. – Lasse V. Karlsen Jan 03 '22 at 13:22
  • 1
    @LasseV.Karlsen Ah, good catch. Not insurmountable but it would've bitten me at some point no doubt. Add to unit tests - tick. – phil Jan 03 '22 at 14:12