ServiceStack CSV序列化程序在序列化日期周围添加额外的引号

时间:2015-06-24 22:32:00

标签: csv servicestack servicestack-text

我在我的网站中使用ServiceStack,允许用户下载其中一个系统数据集的csv。在我的AppHost的配置方法中,我正在为DateTime提供自定义序列化程序。看起来像这样

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

使用csv格式时,所有日期都包含在结果中的额外引号中;例如结果是“”“06/24/2015 16:22:16”“”当我希望它是“06/24/2015 16:22:16”

在我看来,这肯定是ServiceStack序列化中的一个错误。有没有办法防止这种情况发生?以下是向/ 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
        };
    }
}

和Global.asax.cs

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        new AppHost().Init();
    }
}

1 个答案:

答案 0 :(得分:1)

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