8

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?

Ajay
  • 18,086
  • 12
  • 59
  • 105
user989046
  • 589
  • 2
  • 7
  • 11
  • Have you tried to remove the 'private' modifier on the set method of class UNameIt? And try your test writing "bar" lowercase... – Massimo Zerbini Jan 25 '12 at 09:12
  • it's recommended to avoid expose enums in wcf data contracts because they create subtle backwards compatible problems. See http://stackoverflow.com/a/788281/52277 – Michael Freidgeim Jul 21 '13 at 10:49
  • possible duplicate of [DataContractJsonSerializer and Enums](http://stackoverflow.com/questions/794838/datacontractjsonserializer-and-enums) – nawfal Jul 17 '14 at 08:31

2 Answers2

6

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.

Community
  • 1
  • 1
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
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.

david
  • 170
  • 10