I am using Json.NET 7.0.1.
The documentation says that
Enum[is serialized as] Integer (can be the enum value name with StringEnumConverter)
In my Global.asax.cs, I specify the default settings as follows:
JsonConvert.DefaultSettings = (() =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter());
return settings;
});
However, in certain situations, I want Enums to be serialized as integers, for instance when I build a JSON which is to be stored in the database.
Here is how I went about it:
public class JsonSerializedType<T> : IUserType where T : class
{
private static readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
cmd.Parameters[index].Value = JsonConvert.SerializeObject(value as T, serializerSettings);
}
}
However, even in this case Enums are serialized as strings. I checked that serializerSettings has no Converters and its ContractResolver is null.
Why is that so?