I'm trying to write a custom JsonConverter for NetTopologySuite geometries.
My model:
public class MyModel
{
public string myName { get; set; }
[JsonConverter(typeof(MyPolygonConverter))]
public Polygon myPolygon { get; set; }
}
My converter:
public class MyPolygonConverter : JsonConverter<Polygon>
{
public override Polygon ReadJson(JsonReader reader, Type objectType, Polygon existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
var geoJson = (string)reader.Value;
// ... not relevant, because reader.Value is null.
}
}
What I'm doing:
var deSerialized = JsonConvert.DeserializeObject<MyModel>("{\"myName\":\"My Name\",\"myPolygon\":{\"type\":\"Polygon\",\"coordinates\":[[[-100.0,45.0],[-98.0,45.0],[-99.0,46.0],[-100.0,45.0]]]}}");
And what's happening:
My converter is being called, but reader.Value is null.
The examples I've seen online use (string)reader.Value to access the json string that needs to be converted. But in my case reader.Value is null.
How am I supposed to access the JSON string I need to convert?
To be clear - what I have is a model class that contains a property of a class - Polygon - that I convert to GeoJson on serialization that I need to convert back to that class, from the GeoJson.
So, my starting json is:
{
"myName" : "My Name",
"myPolygon" : {
"type" : "Polygon",
"coordinates" : [
[
[-100.0, 45.0],
[-98.0, 45.0],
[-99.0, 46.0],
[-100.0, 45.0]
]
]
}
}
And I need to take everything that a child of "myPolygon" and hand it off to my conversion code. In the simple examples I've seen, reader.Value provided the value as a string. In this case it does not, most likely because "myPolygon"'s child isn't a single value, but is instead a complex json object.
I already have code for parsing the value when provided as a single json string. So how can I get the child, as a single json string?