using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public MyData(string value, string startDate, string endDate)
this.StartDate = startDate;
public string Value { get; }
public string StartDate { get; }
public string EndDate { get; }
public static void Test()
var jsonFilePath = @"Question71709697.json";
File.WriteAllText(jsonFilePath, json);
var jsonSettings = new JsonSerializerSettings();
jsonSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
jsonSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
jsonSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
jsonSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
using (FileStream fileStream = File.OpenRead(jsonFilePath))
using (StreamReader streamReader = new StreamReader(fileStream))
using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
myData = (MyData)JsonSerializer.CreateDefault(jsonSettings).Deserialize(jsonReader, typeof(MyData));
Console.WriteLine($"myData.StartDate={myData.StartDate}, myData.EndDate={myData.EndDate}");
jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var json2 = JsonConvert.SerializeObject(myData, Formatting.Indented, jsonSettings);
Console.WriteLine("Re-serialized {0}:", myData);
Console.WriteLine(json2);
Assert.AreEqual(json, json2);
static string GetJson() => @"{
""startDate"": ""2021-01-16T00:00:00.000Z"",
""endDate"": ""2021-01-18T00:00:00.000Z""
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static partial class JsonExtensions
public static JsonReader AssertTokenType(this JsonReader reader, JsonToken tokenType) =>
reader.TokenType == tokenType ? reader : throw new JsonSerializationException(string.Format("Unexpected token {0}, expected {1}", reader.TokenType, tokenType));
public static JsonReader ReadToContentAndAssert(this JsonReader reader) =>
reader.ReadAndAssert().MoveToContentAndAssert();
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None)
while (reader.TokenType == JsonToken.Comment)
public static JsonReader ReadAndAssert(this JsonReader reader)
throw new ArgumentNullException();
throw new JsonReaderException("Unexpected end of JSON stream.");