Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing enum values in JSON (C#) [duplicate]

Possible Duplicate:
JSON serialization of c# enum as string

I have two classes as follows:

Transaction
    int OrderNumber
    // ... snip ...
    IEnumerable<Item> Items

Item
    string Sku
    // ... snip ...
    ItemCategory Category

ItemCategory is an enum that looks like this:

[DataContract]
public enum ItemCategory
{
    [EnumMember(Value = "Category1")]
    Category1,

    [EnumMember(Value = "Category2")]
    Category2
}

My two classes are decorated with the DataContract and DataMember attributes as appropriate.

I am trying to get a JSON representation of the Transaction item. Within Transaction, I have a public method that looks like this:

public string GetJsonRepresentation()
{
    string jsonRepresentation = string.Empty;

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(this.GetType());
    using (MemoryStream memoryStream = new MemoryStream())
    {
        serializer.WriteObject(memoryStream, this);
        jsonRepresentation = Encoding.Default.GetString(memoryStream.ToArray());   
    }

    return jsonRepresentation;
}

This is returning a string that looks like this:

{
    "OrderNumber":123,
    "Items":[{"SKU": "SKU1","Category": 0}]
}

This is what I want, except for the fact that the "Category" enum value for each Item is being serialized as its integer value, instead of the value I am specifying in the EnumMember attribute. How can I get it so the JSON returned looks like "Category": "Category1" instead of "Category": 0?

like image 411
Andrew Keller Avatar asked Oct 31 '11 16:10

Andrew Keller


People also ask

Can JSON serialize enums?

In C#, JSON serialization very often needs to deal with enum objects. By default, enums are serialized in their integer form.

What is serializing in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How do you serialize an Enum?

In order to serialize Enum, we take the help of ObjectMapper class. We use the writeValueAsString() method of ObjectMapper class for serializing Enum. If we serialize Enum by using the writeValueAsString() method, it will represent Java Enums as a simple string.

How are enums represented in JSON?

JSON has no enum type. The two ways of modeling an enum would be: An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values.


1 Answers

Please take look at JSON serialization of enum as string in stack overflow. No there is no special attribute you can use. JavaScriptSerializer serializes enums to their numeric values and not their string representation. You would need to use custom serialization to serialize the enum as its name instead of numeric value.

like image 174
DeveloperX Avatar answered Sep 21 '22 03:09

DeveloperX