Please consider this as an addition to the other answers.
As @Mansur Kurtov correctly points to, it depends on what you're using for deserializing your JSON.
Let's test this case using both Newtonsoft and System.Text.Json.JsonSerializer.
When using JsonConvert, DTO from @RonSplinter's (OP) answer will work, but will fail when using JsonSerializer straight-forward. When applying these options to JsonSerializer
, @RonSplinter's answer will work. Please note that this applies to .net Core 5 and above.
var serializeOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
To make tests run with both and without any further config, we'll have to do like in @Mansur Kurtov's answer.
Reason for this is that JsonSerializer is case-sensitive and "
-sensitive.
Class OrderBookUsingJsonPropertyName
below shows how to be conform with c# casing using JsonPropertyName
attributes (honoured by JsonSerializer only).
public class OrderBookUsingDouble
{
public long lastUpdateId { get; set; }
public double[][] Bids { get; set; }
public double[][] Asks { get; set; }
}
public class OrderBookUsingString
{
public long lastUpdateId { get; set; }
public string[][] bids { get; set; }
public string[][] asks { get; set; }
}
public class OrderBookUsingJsonPropertyName
{
[JsonPropertyName("lastUpdateId")]
public long LastUpdateId { get; set; }
[JsonPropertyName("bids")]
public string[][] Bids { get; set; }
[JsonPropertyName("asks")]
public string[][] Asks { get; set; }
}
public class JsonParserTests
{
private const string Json = @"
{
""lastUpdateId"": 1027024,
""bids"": [
[
""4.00000000"",
""431.00000000""
]
],
""asks"": [
[
""4.00000200"",
""12.00000000""
]
]
}";
[Fact]
public void JsonConvert_OrderBookUsingDouble()
{
var orderBook = JsonConvert.DeserializeObject<OrderBookUsingDouble>(Json);
Assert.Equal(2, orderBook.Bids[0].Length);
}
[Fact]
public void JsonConvert_OrderBookUsingString()
{
var orderBook = JsonConvert.DeserializeObject<OrderBookUsingString>(Json);
Assert.Equal(2, orderBook.bids[0].Length);
}
[Fact]
public void JsonConvert_OrderBookUsingJsonPropertyName()
{
var orderBook = JsonConvert.DeserializeObject<OrderBookUsingJsonPropertyName>(Json);
Assert.Equal(2, orderBook.Bids[0].Length);
}
[Fact]
public void JsonSerializer_OrderBookUsingDouble_UsingOptions()
{
var serializeOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
var orderBook = System.Text.Json.JsonSerializer.Deserialize<OrderBookUsingDouble>(Json, serializeOptions);
Assert.Equal(2, orderBook.Bids[0].Length);
}
[Fact]
public void JsonSerializer_OrderBookUsingString()
{
var orderBook = System.Text.Json.JsonSerializer.Deserialize<OrderBookUsingString>(Json);
Assert.Equal(2, orderBook.bids[0].Length);
}
[Fact]
public void JsonSerializer_OrderBookUsingJsonPropertyName()
{
var orderBook =
System.Text.Json.JsonSerializer.Deserialize<OrderBookUsingJsonPropertyName>(Json);
Assert.Equal(2, orderBook.Bids[0].Length);
}
}