Home > database >  On-the-fly formatting a stream of JSON using System.Text.Json
On-the-fly formatting a stream of JSON using System.Text.Json

Time:12-31

I've a Json string that is not indended, e.g.,:

{"hash":"123","id":456}

I want to indent the string and serialize it to a JSON file. Naively, I can indent the string using Newtonsoft as follows.

using Newtonsoft.Json.Linq;

JToken token = JToken.Parse(json);
var formattedJson = JObject.Parse(token.ToString()).ToString();

However, since I am using a decent number of large JSON strings, I am using System.Text.Json hoping for better performance. I first tried the following, but the output contains unexpected escapes.

JsonSerializer.Serialize(
    json, 
    new JsonSerializerOptions() { WriteIndented = true });
"{\u0022hash\u0022:\u123\u0022,\u0022}

Ideally, I'm interested in unescaped and formatted JSON (e.g., intended) without post-processing (e.g., using regex to unescape). Additionally, while parsing JSON string to an object and serializing the object to a file is a possible approach to achieve intended formatting, the size and the volume of input make it suboptimal for this application.

Therefore, I am mainly looking for minimal custom code and mainly leverage out-of-box functionality, ideally intercepting a stream of the input while it is written to storage (i.e., on-the-fly conversion).

CodePudding user response:

The easy way using System.Text.Json and friends:

using System;
using System.Text;
using System.Text.Json;
using System.IO;
                    
public class Program
{

  const string template = @"
Original:
---------
{0}
---------

Pretty:
---------
{1}
---------
";

  public static void Main()
  {
    var src  = "{\"hash\":\"123\",\"id\":456}";
    
    using ( var doc = JsonDocument.Parse(src, new JsonDocumentOptions{ AllowTrailingCommas = true }) )
    using ( var ms  = new MemoryStream() )
    using ( var jsonWriter = new Utf8JsonWriter( ms, new JsonWriterOptions{ Indented = true } ) )
    {
      
      doc.RootElement.WriteTo(jsonWriter);
      
      jsonWriter.Flush();
      ms.Flush();
      
      string pretty = Encoding.UTF8.GetString(ms.ToArray());
      
      Console.WriteLine( template , src, pretty );
      
    }
        
  }
}
               

Which produces the expected

Original:
---------
{"hash":"123","id":456}
---------
Pretty:
---------
{
  "hash": "123",
  "id": 456
}
---------

CodePudding user response:

I just tested and didn' t find any problem

var json="{\"hash\":\"123\",\"id\":456}";

var jsonObject=JsonDocument.Parse(json);

json =  System.Text.Json.JsonSerializer.Serialize(jsonObject, 
new JsonSerializerOptions() { WriteIndented = true });

test result

{
  "hash": "123",
  "id": 456
}
  • Related