问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/8999731/how-to-customize-deserialization-of-a-json-enum-in-net