How to localize when JSON-serializing?(JSON序列化时如何本地化?)
问题描述
我已经挣扎了几个小时了,没有好的结果.我正在尝试使用 .NET JSON 序列化程序将 JSON 从 UI 来回转换为对象.
I've been struggling for a few hours now with no good result. I'm trying to use .NET JSON Serializers for converting JSON back and forth from the UI into objects.
小数出现问题是因为我的文化标准使用,"作为小数分隔符,而不是.".我尝试实现一个自定义转换器(请参阅 this 问题),但效果不佳.
The problem arises with decimals because the standard for my culture has "," as decimal separator instead of ".". I've tried implementing a custom converter (see this question) with no good results.
我还检查了 NewtonSoft JSON.net,但没有更好的结果.到目前为止,与值类型的匹配似乎是文化不变的.我想覆盖这个行为,怎么做?
I've also checked out NewtonSoft JSON.net without better results. So far it seems that matching with value types is done culture-invariantly. I want to override this behavior, how to do it?
顺便说一句,我真的希望避免在 javascript 方面进行本地化.我绝对希望 .NET 来处理跨文化格式和本地化,我不认为应该有像我在这个序列化程序中发现的例外,我的猜测是应该有一个适当的方法来做到这一点.
BTW, I really wish to avoid localizing on the javascript side. I definitely want .NET to take care of cross-culture formatting and localizing, I don't think there should be exceptions like I'm finding with this serializers, my guess is that there should be a proper way to do this.
提前致谢.
推荐答案
序列化十进制值的 JSON 标准不提供本地化格式.(请参阅 JSON.org.)这就是为什么值总是使用 Invariant 文化进行格式化的原因.
The JSON standard for serializing decimal values does not provide for localized formatting. (See JSON.org.) This is why values are always formatted with the Invariant culture.
如果您需要本地化值,那么您需要为您选择的序列化程序创建一个自定义转换器,它将小数输出为预先格式化的字符串.在 Json.Net 中,这可以很容易地完成,如下所示:
If you need localized values, then you'll need to create a custom converter for your serializer of choice which outputs the decimals as pre-formatted strings instead. In Json.Net, this can be done easily as shown below:
class Program
{
static void Main(string[] args)
{
List<decimal> values = new List<decimal> { 1.1M, 3.14M, -0.9M, 1000.42M };
var converter = new FormattedDecimalConverter(CultureInfo.GetCultureInfo("fr-FR"));
string json = JsonConvert.SerializeObject(values, converter);
Console.WriteLine(json);
}
}
class FormattedDecimalConverter : JsonConverter
{
private CultureInfo culture;
public FormattedDecimalConverter(CultureInfo culture)
{
this.culture = culture;
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(decimal) ||
objectType == typeof(double) ||
objectType == typeof(float));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(Convert.ToString(value, culture));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
输出:
["1,1","3,14","-0,9","1000,42"]
这篇关于JSON序列化时如何本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON序列化时如何本地化?
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01