I have a JSON file containing lines like this:
{"id":"0258","name":"Canterbury","coordinates":[1.07992,51.27904]}
The coordinates
item is the geographic coordinates of the city. However it is stored as an array which always contains the [longitude, latitude].
I want to deserialize these entries into C# POCO classes that are defined like this:
public sealed class City
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("coordinates")]
public GeographicLocation Location { get; set; }
}
public sealed class GeographicLocation
{
public GeographicLocation(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
public double Latitude { get; private set; }
public double Longitude { get; private set; }
}
I think you can see where I'm going with that.
Is there a way of annotating these classes so that the source JSON will deserialize correctly into my desired object graph?
I'm currently using System.Text.Json
but would switch to Newtonsoft.Json
in a heartbeat if it has a better solution.
Clarification
I know how to deserialize JSON to a class, where there's a correspondence between JSON elements and class properties. The essential problem here is that the source JSON file contains a 2-element array which I'd like to end up in properties of my deserialized class, rather than in a single array property. I don't know how to accomplish that.