JSON序列化时如何本地化

JSON序列化时如何本地化

本文介绍了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序列化时如何本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:36