2

I am trying to deserialize GeoJSON using the JSON.net library. The geometry component of each feature can be of many different types based on the "type" attribute value.

I need to deserialize the geometry component of this GeoJSON into a geometry object model like so:

public abstract class Geometry { ... }

public class Point : Geometry { ... }

public class LineString : Geometry { ... }

public class Polygon : Geometry { ... }

So based on the value of the "type" attribute, it will deserialize into the corresponding .net concrete type, but accessed via its base Geometry class.

Does the JSON.net library offer anything similar to the KnownTypeAttribute in WCF or XmlElementAttribute in XML Serialization that allows me to deserialize JSON to a base class with a set of known derived classes?

1 Answer 1

4

Documentation here shows this example:

    [JsonObject(MemberSerialization.OptIn)]
    public class Person
    {
      // "John Smith"
      [JsonProperty]
      public string Name { get; set; }

      // "2000-12-15T22:11:03"
      [JsonProperty]
      [JsonConverter(typeof(IsoDateTimeConverter))]
      public DateTime BirthDate { get; set; }

      // new Date(976918263055)
      [JsonProperty]
      [JsonConverter(typeof(JavaScriptDateTimeConverter))]
      public DateTime LastModified { get; set; }

      // not serialized
      public string Department { get; set; }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

It's all about the JsonConverter as I discovered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.