skip to Main Content

I am trying to remove all indentation from my json :

    "date": {
        "start": "2020-10-23T15:30:00+00:00"
    }

I tried with this

    var freeContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(content));

But I got

    "date": {"start": "2020-10-23T13:30:00+00:00"}

As you can see, the date is modified while I am only expecting for a format change.

How can I remove all indentation from json without changing anything else?

2

Answers


  1. You need to use DateTimeOffset type to deserilize this json. Or the timezone infomation will be lost.

        public class MyModel
        {
            public Date date { get; set; }
        }
        public class Date
        {
            public DateTimeOffset start { get; set; }
        }
    

    Then Desirilize like

    var content = " {"date": { "start": "2020-10-23T15:30:00+00:00"}}";
    var freeContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<MyModel>(content));
    

    Test result
    enter image description here

    Login or Signup to reply.
  2. I can’t reproduce your exact problem, but if I assume you are currently in a GMT+2 time zone, I was able to reproduce a very similar problem in a fiddle here. The problem seems to be that, somewhere in the deserialization/serialization round trip, Json.NET converts the incoming time to your local time zone but later fails to convert it back to universal time consistently.

    The simplest way to solve the problem is to disable Json.NET’s automatic DateTime recognition entirely by setting DateParseHandling to DateParseHandling.None like so:

    var settings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
    var freeContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(content, settings), settings);
    

    This ensures your DateTime strings will remain unchanged. Demo fiddle #2 here.

    That being said, for large JSON files, it will be more efficiently to eliminate the intermediate JToken representation and stream directly from a TextReader to a TextWriter using the following extension methods:

    public static partial class JsonExtensions
    {
        public static string FormatJson(string json, Formatting formatting = Formatting.None, DateParseHandling dateParseHandling = DateParseHandling.None, FloatParseHandling floatParseHandling = default) =>
            FormatJson(new StringReader(json), new StringWriter(), formatting, dateParseHandling, floatParseHandling).ToString();
    
        public static TextWriter FormatJson(TextReader input, TextWriter output, Formatting formatting = Formatting.None, DateParseHandling dateParseHandling = DateParseHandling.None, FloatParseHandling floatParseHandling = default)
        {
            using var reader = new JsonTextReader(input) { CloseInput = false, DateParseHandling = dateParseHandling, FloatParseHandling = floatParseHandling };
            using (var jsonWriter = new JsonTextWriter(output) { CloseOutput = false, Formatting = formatting })
            {
                jsonWriter.WriteToken(reader);
            }
            return output;
        }
    }
    

    Then if you have some JSON string in memory you can do:

    var freeContent = JsonExtensions.FormatJson(content, Formatting.None, DateParseHandling.None);
    

    But if your JSON is in some file on disk, stream from a StreamReader to a StreamWriter.

    Demo fiddle #3 here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search