In this lecture about Json Schemas we can validate that a json array has specific types for specific indices (it's called tuple there). For example a tuple for an street address:
[NUMBER, STREET_NAME, STREET_TYPE, DIRECTION] // Structure
[1600, "Pennsylvania", "Avenue", "NW"] // Example
where the different items in this (fixed length) array mean:
- (int) NUMBER: The address number.
- (string) STREET_NAME: The name of the street.
- (enum) STREET_TYPE: The type of street. Can be
Street
,Avenue
orBoulevard
. - (enum) DIRECTION: The city quadrant of the address. Can be
NW
,NE
,SW
orSE
.
What is a clean way of deserializing such an array using Newtonsofts Json.Net? The model should look like:
public class StreetAddress
{
public int Number { get; set; }
public string StreetName { get; set; }
public StreetType StreetType { get; set; }
public Direction Direction { get; set; }
}
public enum StreetType
{
Street,
Avenue,
Boulevard
}
public enum Direction
{
Nw,
Ne,
Sw,
Se
}
Are there attributes to define the ordinal indices for each property or an attribute for using an e.g. ArrayConverter
for the whole model? Well, we could just use a JArray
und assign each property from an index, but that does not feel very smooth.