Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing an enumeration in JSON.net

Tags:

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.

like image 372
bryan costanich Avatar asked Oct 17 '11 21:10

bryan costanich


People also ask

How do you serialize an enum?

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.

Can you serialize an enum C#?

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.

How can we send enum value in JSON?

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 .

What is Stringenumconverter?

Converts an Enum to and from its name string value. Newtonsoft.Json. JsonConverter. Newtonsoft.Json.Converters.


1 Answers

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

like image 169
Romain Meresse Avatar answered Sep 28 '22 03:09

Romain Meresse