How to Customize Deserialization of a JSON enum in .NET?
Asked Answered
T

2

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?

Twohanded answered 25/1, 2012 at 8:32 Comment(3)
Have you tried to remove the 'private' modifier on the set method of class UNameIt? And try your test writing "bar" lowercase...Frangos
it's recommended to avoid expose enums in wcf data contracts because they create subtle backwards compatible problems. See https://mcmap.net/q/271398/-do-you-use-enum-types-in-your-wcf-web-servicesMaintenon
possible duplicate of DataContractJsonSerializer and EnumsIncapable
R
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.

Rene answered 25/1, 2012 at 9:19 Comment(0)
H
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.

Hygrometer answered 5/11, 2015 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.