0

I have a JSON file where on of the properties is:

"Location": {
  "Latitude": 38.7080404,
  "Longitude": -9.1436747
}

And I have a few classes with a Location property:

public class Example {
  public IPoint Location { get; set; }
}

A IPoint can be created as follows:

Example example = new Example();
example.Location = new Point(new Coordinate(latitude, longitude)) { SRID = 4326 };

How can I define a Newtonsoft JSon.Net rule to convert from JSON to any property of type IPoint?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • 1
    Minor issue, `IPoint` is an interface, JSON Deserializer will fail because it cannot instantiate an interface – Node.JS Feb 28 '19 at 20:25
  • But it can initialize a class and I mentioned that it should be created a Point instance. But if necessary I can change the property from IPoint to Pont – Miguel Moura Feb 28 '19 at 20:54
  • Assuming you always want to create a `Point` type for your `IPoint` property, you could use `ConcreteConverter` from [How to deserialize collection of interfaces when concrete classes contains other interfaces](https://stackoverflow.com/q/47939878), e.g. by adding it to `JsonSerializerSettings.Converters`. For other options see [Using Json.NET converters to deserialize properties](https://stackoverflow.com/q/2254872). In fact this may be a duplicate, agree? – dbc Feb 28 '19 at 21:19

0 Answers0