gpt4 book ai didi

csv - ServiceStack CSV 序列化程序在序列化日期周围放置额外的引号

转载 作者:行者123 更新时间:2023-12-01 16:09:19 27 4
gpt4 key购买 nike

我在我的站点中使用 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();
}
}

最佳答案

现在应该解决此问题 with this commit可从 v4.0.41+ 获得,现在是 available on MyGet .

关于csv - ServiceStack CSV 序列化程序在序列化日期周围放置额外的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31038268/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com