smgtkn
smgtkn

Reputation: 130

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"}]}]}

Upvotes: 1

Views: 653

Answers (1)

Serge
Serge

Reputation: 43860

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; }
}

Upvotes: 3

Related Questions