Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Customize Deserialization of a JSON enum in .NET?
    primarykey
    data
    text
    <p>I have the following sample C# code that is auto-genereated from an xsd using the svcutils.exe application.</p> <pre><code> [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; } } </code></pre> <p>The following is a unit test that attempts to deserialise a sample JSON document to the UNameIt class. </p> <pre><code> [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); } } </code></pre> <p>Unfortunately, the test fails giving the following reason:</p> <blockquote> <p>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'.</p> </blockquote> <p>The test will pass if I update my JSON string to replace the string specifier for the Enum to an integer e.g.</p> <pre><code>public const string JsonData = @"{ ""id"":123456, ""name"":""John Doe"", ""foo"":""1""}"; </code></pre> <p>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. </p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload