JsonSerializerSettings

JsonSerializerSettings

本文介绍了巨大的阅读与整数Json.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JSON巨大整数,在几百位的顺序。我想这些解析为BouncyCastle的的的BigInteger ()。

I've got some json with huge integers, in the order of a few hundred digits. I'd like to parse those as BouncyCastle's BigInteger (https://github.com/onovotny/BouncyCastle-PCL/blob/pcl/crypto/src/math/BigInteger.cs).

{
    "bigNumber":12093812947635091350945141034598534526723049126743245...
}

所以,我已经实现了一个转换器,使用默认设置合同解析器。

So I've implemented a converter, using a contract resolver in the default settings.

internal class BigIntegerConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteRawValue(value.ToString());
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken jToken = JToken.Load(reader);
        return new BigInteger(jToken.ToString());
    }

    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(BigInteger));
    }
}

public class BigIntegerContractResolver : DefaultContractResolver
{
    private static readonly JsonConverter bigIntegerConverter = new BigIntegerConverter();
    private static Type type = typeof(BigInteger);

    protected override JsonConverter ResolveContractConverter(Type objectType)
    {
        if (objectType == type)
        {
            return bigIntegerConverter;
        }
        return base.ResolveContractConverter(objectType);
    }
}

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
        {
            ContractResolver = new BigIntegerContractResolver()
        };



这位作家的作品,因为它应该写一个(大)的整数值,而不是类的BigInteger所有其属性等等。然而,读出失败。 。无论ReadJson也不CanConvert似乎被调用

The writer works as it should, writing a (large) integer value instead of the class BigInteger with all its properties etc. However, the reading fails. Neither ReadJson nor CanConvert appear to be invoked.

我得到了以下异常:

JsonReaderException:JSON整数340597435091750914358634185762341897561435984635897436598435643875643189576413589743659817456 ......过大或小的一个Int64。

JsonReaderException: JSON integer 340597435091750914358634185762341897561435984635897436598435643875643189576413589743659817456... is too large or small for an Int64.

我怎么Json.NET来解析这个数字作为一个字符串而不是整数?

How do I get Json.NET to parse this number as a string instead of an integer?

在理想情况下,我不希望有解析JSON字符串自己放在第一位,要加引号。

Ideally I don't want to have to parse the json string myself first, to add quotes.

推荐答案

如果您的大量不报价,Json.Net将反序列化作为 System.Numerics.BigInteger 。这种情况发生在 JsonTextReader 里面,之前还有转换器得到一个机会来处理它。所以如果你想你的结果类型为 Org.BouncyCastle.Math.BigInteger ,你需要从 System.Numerics.BigInteger 。 (似乎有点倒退,我所知道的另一种方法是创建自己的 JsonTextReader ,但很可能将是更多的麻烦比它的价值 - 大多数。现有的读者有用位在私有或内部方法,因此继承它是不实际的)

If your large number isn't quoted, Json.Net will deserialize it as a System.Numerics.BigInteger. This happens inside the JsonTextReader, well before the converter gets a chance to handle it. So if you want your result type to be Org.BouncyCastle.Math.BigInteger, you'll need to convert from System.Numerics.BigInteger. (Seems a little backwards, I know. The other alternative is to create your own JsonTextReader, but that is probably going to be more trouble than it is worth -- most of the useful bits of the existing reader are in private or internal methods, so subclassing it is not practical.)

我能得到这个转换器工作:

I was able to get this converter to work:

class BigIntegerConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Org.BouncyCastle.Math.BigInteger));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        System.Numerics.BigInteger big = (System.Numerics.BigInteger)reader.Value;
        return new Org.BouncyCastle.Math.BigInteger(big.ToString());
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteRawValue(value.ToString());
    }
}

下面是我使用的测试程序。请注意,我没有使用解析器。 JsonSerializerSettings 有一个转换收藏,所以我刚才添加的 BigIntegerConverter

Here is the test program I used. Note that I did not use a resolver. JsonSerializerSettings has a Converters collection, so I just added the BigIntegerConverter to that.

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""bigNumber"": 12093812947635091350945141034598534526723049126743245
        }";

        JsonConvert.DefaultSettings = () => new JsonSerializerSettings
        {
            Converters = new List<JsonConverter> { new BigIntegerConverter() }
        };

        Foo foo = JsonConvert.DeserializeObject<Foo>(json);
        Console.WriteLine(foo.BigNumber.ToString());
    }
}

class Foo
{
    public Org.BouncyCastle.Math.BigInteger BigNumber { get; set; }
}



输出:

Output:

12093812947635091350945141034598534526723049126743245

这篇关于巨大的阅读与整数Json.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:36