4

I'm using ServiceStack in my site to allow users to download a csv of one of the system's datasets. In the configure method of my AppHost, I'm providing a custom serializer for DateTime. It looks like this

JsConfig<DateTime>.SerializeFn = time =>
  {
      var result = time;
      if (time.Kind == DateTimeKind.Unspecified)
      {
          result = DateTime.SpecifyKind(result, DateTimeKind.Local);
      }
          return result.ToString(CultureInfo.InvariantCulture);
  };

When using the csv format all dates are being wrapped in extra quotes in the result; e.g. Result is """06/24/2015 16:22:16""" when I'd expect it to be "06/24/2015 16:22:16"

It seems to me like this must be a bug in ServiceStack serialization. Is there a way to prevent this from happening? Below is a complete example that exhibits the problem when making a request to /csv/oneway/Request

public class AppHost : AppHostBase
{
    /// <summary>
    /// Default constructor.
    /// Base constructor requires a name and assembly to locate web service classes. 
    /// </summary>
    public AppHost()
        : base("CSVBug", typeof(MyService).Assembly)
    {

    }

    public override void Configure(Container container)
    {
        JsConfig<DateTime>.SerializeFn = time =>
        {
            var result = time;
            if (time.Kind == DateTimeKind.Unspecified)
            {
                result = DateTime.SpecifyKind(result, DateTimeKind.Local);
            }
            return result.ToString(CultureInfo.InvariantCulture);
        };
    }
}

public class Request
{

}

public class Response
{
    public DateTime DateTime { get; set; }
}

public class MyService : Service
{
    public object Any(Request reuqest)
    {
        return new Response()
        {
            DateTime = DateTime.Now
        };
    }
}

and Global.asax.cs

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        new AppHost().Init();
    }
}
Dustin Hodges
  • 4,110
  • 3
  • 26
  • 41

1 Answers1

1

This should now be resolved with this commit which is available from v4.0.41+ that's now available on MyGet.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks @mythz, this gives me the desired result. I'm still confused why a string value returned from my original function goes from 06/24/2015 16:22:16 to """06/24/2015 16:22:16""". I can understand guarding that value with quotes, e.g. "06/24/2015 16:22:16", but then it seems like somewhere down the line those get escaped and then it is quoted again. Is there documentation on what happens between the SerialFn call and what is sent down the wire? – Dustin Hodges Jun 25 '15 at 19:12
  • Actually this doesn't work for me. I get the desired result in the CSV format but then my JSON clients break. For example in csv I get DateTime 06/25/2015 14:44:41 but with json I get {"DateTime":06/25/2015 14:45:05} which is unparseable. – Dustin Hodges Jun 25 '15 at 20:44
  • Update did the trick. Getting desired results in both csv and JSON format. Thanks – Dustin Hodges Jun 26 '15 at 15:01