title: Serializing enum values in JSON (C#)
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2441290/json-serialization-of-c-sharp-enum-as-string">JSON serialization of c# enum as string</a>  </p>
  </blockquote>
  
  
  
  <p>I have two classes as follows:</p>
  
  <pre><code>Transaction
      int OrderNumber
      // ... snip ...
      IEnumerable&lt;Item&gt; Items
  
  Item
      string Sku
      // ... snip ...
      ItemCategory Category
  </code></pre>
  
  <p>ItemCategory is an enum that looks like this:</p>
  
  <pre><code>[DataContract]
  public enum ItemCategory
  {
      [EnumMember(Value = "Category1")]
      Category1,
  
      [EnumMember(Value = "Category2")]
      Category2
  }
  </code></pre>
  
  <p>My two classes are decorated with the DataContract and DataMember attributes as appropriate.</p>
  
  <p>I am trying to get a JSON representation of the Transaction item.  Within Transaction, I have a public method that looks like this:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>This is returning a string that looks like this:</p>
  
  <pre><code>{
      "OrderNumber":123,
      "Items":[{"SKU": "SKU1","Category": 0}]
  }
  </code></pre>
  
  <p>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?</p>