9

I have the following sample C# code that is auto-genereated from an xsd using the svcutils.exe application.

 [DataContract] public enum Foo { [EnumMember(Value = "bar")] Bar = 1, [EnumMember(Value = "baz")] Baz = 2 } [DataContract] public class UNameIt { [DataMember(Name = "id")] public long Id { get; private set; } [DataMember(Name = "name")] public string Name { get; private set; } [DataMember(Name = "foo")] public Foo Foo { get; private set; } } 

The following is a unit test that attempts to deserialise a sample JSON document to the UNameIt class.

 [TestClass] public class JsonSerializer_Fixture { public const string JsonData = @"{ ""id"":123456, ""name"":""John Doe"", ""foo"":""Bar""}"; [TestMethod] public void DataObjectSimpleParseTest() { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UNameIt)); MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(JsonData)); UNameIt dataObject = serializer.ReadObject(ms) as UNameIt; Assert.IsNotNull(dataObject); Assert.AreEqual(123456, dataObject.Id); Assert.AreEqual(Foo.Baz, dataObject.Foo); } } 

Unfortunately, the test fails giving the following reason:

System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type MyNamespace.Units.UNameIt. The value 'Bar' cannot be parsed as the type 'Int64'.

The test will pass if I update my JSON string to replace the string specifier for the Enum to an integer e.g.

public const string JsonData = @"{ ""id"":123456, ""name"":""John Doe"", ""foo"":""1""}"; 

I do not have the flexibility to the change the supplied JSON so I have to figure out how to convert the string Enum representation perhaps on serialisation. Ideally, I would like to facilitate this without having to change my autogenerate class because once I re-generate the class I would loose my changes.

I am wondering if it would be possible to extend the DataContractJsonSerializer to custom handle Enumerations? Or perhaps there is better way to do this?

3
  • Have you tried to remove the 'private' modifier on the set method of class UNameIt? And try your test writing "bar" lowercase... Commented Jan 25, 2012 at 9:12
  • it's recommended to avoid expose enums in wcf data contracts because they create subtle backwards compatible problems. See stackoverflow.com/a/788281/52277 Commented Jul 21, 2013 at 10:49
  • possible duplicate of DataContractJsonSerializer and Enums Commented Jul 17, 2014 at 8:31

2 Answers 2

7

This behavior is by design. Here's a quote from the Enumerations and JSON paragraph on MSDN:

Enumeration member values are treated as numbers in JSON, which is different from how they are treated in data contracts, where they are included as member names.

Moreover the DataContractJsonSerializer will automatically serialize all enumerations, so the EnumMemberAttribute is actually ignored.

For a workaround, take a look at this answer on SO.

Sign up to request clarification or add additional context in comments.

Comments

0

This is work :

var ret = new JavaScriptSerializer().Deserialize<tblGridProjects>(retApi.Item2); 

But you can't use datamembers attributes, so can't rename properties. You must set the name of the property like Json sended.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.