如何在JSON序列化定位? [英] How to localize when JSON-serializing?

查看:249
本文介绍了如何在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.

该问题出现在小数,因为我的文化的标准有,作为小数点分隔符,而不是。我试图实现自定义转换器(见这个问题),没有较好的效果。

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 。)这就是为什么值始终格式化为不变的文化。

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.

如果您需要本地化的值,那么你需要创建一个自定义转换器为您选择的序列化器,其输出的小数位为pre格式的字符串代替。在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序列化定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆