Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Deserialize a JSON Object With a Very Large Interger Value in .NET C#

I am trying to deserialize a JSON response from https://api.huobi.pro/market/history/trade?symbol=btcusdt endpoint. However, the response includes a very big number which exceeds the capacity of ulong in C#, and I am getting exceptions while deserializing with System.Text.Json.JsonSerializer.Deserialize (also with Newtonsoft.Json). The problematic filed is the innermost "id" field of the response. I tried using BigNumber on my data model but didn’t help. How can I deserialize this object?

The response structure:

{"ch":"market.btcusdt.trade.detail","status":"ok","ts":1675717627951,"data":[{"id":161853359895,"ts":1675717624799,"data":[{"id":161853359895718430859199824,"ts":1675717624799,"trade-id":102753924495,"amount":0.01998,"price":22983.98,"direction":"buy"}]}]}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

just use a BigInteger property.This code was tested and working properly

using Newtonsoft.Json;
using System.Numerics;

Data data = JsonConvert.DeserializeObject<Data>(json);


public class Datum
{
    public BigInteger id { get; set; }
    public long ts { get; set; }
    public List<Datum> data { get; set; }

    [JsonProperty("trade-id")]
    public long tradeid { get; set; }
    public double amount { get; set; }
    public double price { get; set; }
    public string direction { get; set; }
}

public class Data
{
    public string ch { get; set; }
    public string status { get; set; }
    public long ts { get; set; }
    public List<Datum> data { get; set; }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading