the code below gives you json = ' {"Name": "Test", "Id": 1, "MyEnum": 3} ' if you have a non-zero value.
public enum SerializeObjTestClassEnum { one = 1, two, three, four } [Serializable] public class SerializeObjTestClass { public string Name { get; set; } public int Id { get; set; } public SerializeObjTestClassEnum MyEnum{ get; set; } } public void SerializeObject_Test_Basic_Object() { var obj = new SerializeObjTestClass { Id = 1, Name = "Test", MyEnum = SerializeObjTestClassEnum.three }; var json = (new JavaScriptSerializer()).Serialize(obj); }
this code gives you json = ' {"Name": "Test", "Id": 1, "MyEnum": 0} '
var obj = new SerializeObjTestClass { Id = 1, Name = "Test" };
Notice how the enumeration, when not set, is serialized to 0, while the enumeration itself starts at 1. So this is how the code can know that a NULL value was used for the enumeration.
if you want json to look like " {" Name ":" Test "," Id ": 1," MyEnum ": null} ', then you will need to fake it using the class wrapper around Enum.
DMCS
source share