using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace MMSL.DateTimeConverter { public class MicrosoftDateTimeConverter : JsonConverter { private static readonly Regex MicrosoftDateRegex = new Regex(@"\\/Date\((\d+)([-+]\d+)?\)\\/", RegexOptions.Compiled); public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { string value = reader.GetString(); if (value != null && value.StartsWith("/Date(") && value.EndsWith(")/")) { var milliseconds = long.Parse(value[6..^2]); return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds).UtcDateTime; } return DateTime.Parse(value); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { var milliseconds = new DateTimeOffset(value).ToUnixTimeMilliseconds(); writer.WriteStringValue($"/Date({milliseconds})/"); } } }