Search code examples
c#datetimeservicestackservicestack-text

ServiceStack.Text JsonSerializer - SerializeToString fails whit DateTime.Max


I'm using ServiceStack.Text JsonSerializer on a web application using c#. The problem is that for an specific functionality I need to serialize date values into a json but currently is failing when the values is DateTime.MaxValue.

Here is some code example to replicate this ( its a simple console application ) :

class Program
{
    static void Main(string[] args)
    {
        var jsonResultMin = JsonSerializer.SerializeToString<DateTime>(DateTime.MinValue);
        var deJsonMin = JsonSerializer.DeserializeFromString<DateTime>(jsonResultMin);
        Console.WriteLine("jsonResultMin" + jsonResultMin);
        Console.WriteLine("deJsonMin" + deJsonMin);

        var jsonResultMax = JsonSerializer.SerializeToString<DateTime>(DateTime.MaxValue);
        var deJsonMax = JsonSerializer.DeserializeFromString<DateTime>(jsonResultMax);
        Console.WriteLine("jsonResultMax" + jsonResultMax);
        Console.WriteLine("deJsonMax" + deJsonMax);

        Console.ReadLine();

    }
}

The stack trace is:

System.ArgumentOutOfRangeException was unhandled
  HResult=-2146233086
  Message=The added or subtracted value results in an un-representable DateTime.
Parameter name: value
  Source=mscorlib
  ParamName=value
  StackTrace:
       at System.DateTime.Subtract(TimeSpan value)
       at ServiceStack.Text.DateTimeExtensions.ToDateTimeSinceUnixEpoch(DateTime dateTime)
       at ServiceStack.Text.DateTimeExtensions.ToUnixTimeMs(DateTime     dateTime)
       at ServiceStack.Text.Common.DateTimeSerializer.WriteWcfJsonDate(TextWriter writer,     DateTime dateTime)
       at ServiceStack.Text.Json.JsonTypeSerializer.WriteDateTime(TextWriter writer, Object oDateTime)
       at ServiceStack.Text.Json.JsonWriter`1.WriteRootObject(TextWriter writer, Object value)
       at ServiceStack.Text.JsonSerializer.SerializeToString[T](T value)
       at ConsoleApplication1.Program.Main(String[] args) 
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

I understand that error is happening because of the date-time conversion that the ServiceStack library is doing internally.

Also, I found that if you subtract a day ( addDays(-1) ) from the max value, the library doesn't fail.

But is there a workaround to make this work without modifying the date?


Solution

  • The error is result from the Unix Time conversion, you can skip it if you set a DateHandler, like:

    JsConfig.DateHandler = DateHandler.ISO8601DateTime
    

    if you want to check all the possible conversions check the source code