i'm using JSON.net (maybe v3.5ish? it's from oct. 2010). and i'm trying to deserialize some json into an enumeration:
geometryType: "esriGeometryPolygon"
i have this enumeration:
/// <summary>
/// The geometry type.
/// </summary>
[DataContract]
public enum GeometryType
{
/// <summary>
/// Refers to geometry type Envelope
/// </summary>
[EnumMember(Value = "esriGeometryEnvelope")]
Envelope,
/// <summary>
/// Refers to geometry type MultiPoint
/// </summary>
[EnumMember(Value = "esriGeometryMultipoint")]
MultiPoint,
/// <summary>
/// Refers to geometry type MapPoint
/// </summary>
[EnumMember(Value = "esriGeometryPoint")]
Point,
/// <summary>
/// Refers to geometry type Polygon
/// </summary>
[EnumMember(Value = "esriGeometryPolygon")]
Polygon,
/// <summary>
/// Refers to geometry type Polyline
/// </summary>
[EnumMember(Value = "esriGeometryPolyline")]
Polyline
}
but it throws an error saying "Error converting value "esriGeometryPolygon" to type '...GeometryType'.
what am i missing here?
is it because it's an old version (i'm using the monotouch port: https://github.com/chrisntr/Newtonsoft.Json which hasn't been updated in a year)? or did i get my datacontract wrong?
EDIT: i ported the latest JSON.NET to MT and i'm still getting the exact same error.
To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant's name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java. lang. Enum.
In C#, JSON serialization very often needs to deal with enum objects. By default, enums are serialized in their integer form. This often causes a lack of interoperability with consumer applications because they need prior knowledge of what those numbers actually mean.
All you have to do is create a static method annotated with @JsonCreator in your enum. This should accept a parameter (the enum value) and return the corresponding enum. This method overrides the default mapping of Enum name to a json attribute .
Converts an Enum to and from its name string value. Newtonsoft.Json. JsonConverter. Newtonsoft.Json.Converters.
According to JSON.NET documentation, default behavior is to use int value for Enums : http://james.newtonking.com/projects/json/help/SerializationGuide.html
You can change that by adding a JsonConverter attribute with StringEnumConverter on your enum...
/// <summary>
/// The geometry type.
/// </summary>
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum GeometryType
Documentation: Serialize with JsonConverters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With