edit: Sorry just got up no coffee :(
Here is the code to do what you want to do with the Json Serializer, not the DataContractJsonSerializer.
I haven't done any work with DataContractJsonSerializer yet but after quickly scanning the docs, I am rather disappointed in MS. They obviously went to extremes to make WCF very extensible, but with the DataContractJsonSerializer there is no extensibility. You have to use MS flavored JSON with it. SUPER lame of MS ... already messing up WCF.
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
Some Test Objects & Enum:
public enum SomeSillyEnum
{
Foo,Bar,Doo,Daa,Dee
}
public class UseSillyEnum
{
public SomeSillyEnum PublicEnum { get; set; }
public string SomeOtherProperty { get; set; }
public UseSillyEnum()
{
PublicEnum = SomeSillyEnum.Foo;
SomeOtherProperty = "Testing";
}
}
JavaScriptConverters. One for all enums, and one for an object using an enum.
public class EnumStringConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
foreach(string key in dictionary.Keys)
{
try { return Enum.Parse(type, dictionary[key].ToString(), false); }
catch(Exception ex) { throw new SerializationException("Problem trying to deserialize enum from JSON.",ex); }
}
return Activator.CreateInstance(type);
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
Dictionary<string,object> objs = new Dictionary<string, object>();
objs.Add(obj.ToString(), ((Enum)obj).ToString("D"));
return objs;
}
public override IEnumerable<Type> SupportedTypes{get {return new Type[] {typeof (Enum)};}}
}
public class SillyConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
UseSillyEnum se = new UseSillyEnum();
foreach (string key in dictionary.Keys)
{
switch(key)
{
case "PublicEnum":
se.PublicEnum = (SomeSillyEnum) Enum.Parse(typeof (SomeSillyEnum), dictionary[key].ToString(), false);
break;
case "SomeOtherProperty":
se.SomeOtherProperty = dictionary[key].ToString();
break;
}
}
return se;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
UseSillyEnum se = (UseSillyEnum)obj;
Dictionary<string, object> objs = new Dictionary<string, object>();
objs.Add("PublicEnum", se.PublicEnum);
objs.Add("SomeOtherProperty", se.SomeOtherProperty);
return objs;
}
public override IEnumerable<Type> SupportedTypes { get { return new Type[] { typeof(UseSillyEnum) }; } }
}
And using it inside a page:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* Handles ALL Enums
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
jsonSer.RegisterConverters( new JavaScriptConverter[] { new EnumStringConverter() } );
string json = jsonSer.Serialize(new UseSillyEnum());
Response.Write(json);
UseSillyEnum obj = jsonSer.Deserialize<UseSillyEnum>(json);
Response.Write(obj.PublicEnum);
*/
/* Handles Object that uses an enum */
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
jsonSer.RegisterConverters( new JavaScriptConverter[] { new SillyConverter() } );
string json = jsonSer.Serialize(new UseSillyEnum());
Response.Write(json);
UseSillyEnum obj = jsonSer.Deserialize<UseSillyEnum>(json);
Response.Write(obj.PublicEnum);
}
}