Is it possible to have enumeration values serialised (to Json) as their as the corresponding ordinal values and not their string representations? If so, how can this be accomplished?
I'm using Asp.Net Core 2.2, Entity Framework Core 2.2.2 and Asp.Net Core OData 7.1.
I have an enumeration:
public enum MyEnumeration
{
Value1 = 10,
Value2 = 20
}
And a model class:
public class MyModel
{
public MyEnumeration MyProperty { get; set; }
}
When I query OData MyProperty will get serialised into Json with values "Value1" or "Value2". My goal is to get the values 10 and 20 instead.
I've tried to apply [EnumMember(Value = "10")] attributes to the enumeration values but without luck; I still get "Value1" or "Value2". I have also tried to create a custom JsonConverter and apply the [JsonConverter()] attribute to MyProperty; I've also tried to add the custom converter through services.AddJsonOptions(options => options.SerializerSettings.Converters.Add()) but no luck either; in both cases, the custom converter seems to be ignored by OData (or it simply picks StringEnumConverter as a better match for the conversion).
I stumbled upon this:
https://dotnetcoretutorials.com/2018/11/12/override-json-net-serialization-settings-back-to-default/
It explains how to "revert" the enumeration serialiser to the default, but the solution seems to be specific to Entity Framework without OData. I, at least, could not get it to work with OData on top.
One final note; I would very much like to avoid having two properties that represent the same field; i.e. something like:
public class MyClass
{
public int MyProperty { get; set; }
[NotMapped]
public MyEnumeration MySecondaryProperty
{
get {
return (MyEnueration)MyProperty;
}
set {
MyProperty = (int)value;
}
}
OData protocol says to serialize enum using the enum member string, not the enum member value. Would you please share us more about your use cases? And why do you need to serialize the num member value?
I'm extending an existing web service with a number of OData enabled endpoints; this service already exposes a number of endpoints that rely solely on Entity Framework. We have a client that currently requests data from the existing endpoints, but will be expanded to also request data from the OData enabled ones.
One of our issues is that Entity Framework provides ordinal values for enumerations when data is serialized; OData does not.
Our client expects integer values for enumerations; the domain models on the client are simply implemented this way. We would like to be able to re-use these models for the OData endpoints as well, without having to change them.
And why do you need to serialize the num member value?
We're just interested in - somehow - getting the ordinal values for enumerations; if that can be achieved by configuring OData, or by using serialization attributes, or whatever, really, that would be great. So it's not really about the member values per se, but trying to find a solution that works for our scenario.
As far I checked, theoretically that case could be solved by customizing ODataEnumSerializer:
https://github.com/OData/WebApi/blob/12d998c0e60ed9bcf1a45dbdc650a51a3578b763/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataEnumSerializer.cs#L83
It also can be required for deserializer:
https://github.com/OData/WebApi/blob/12d998c0e60ed9bcf1a45dbdc650a51a3578b763/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/EnumDeserializationHelpers.cs
Any progress on this?
@dariooo512 Does suggestion from @TheAifam5 work for you?
Hi, we have developed this nuget package. You can enable support of "int to enum" by doing
app.ApplicationServices.UseIntAsEnumODataUriResolver();
in your Startup class
Hi, we have developed this nuget package. You can enable support of "int to enum" by doing
app.ApplicationServices.UseIntAsEnumODataUriResolver();in your Startup class
Is this package source on GitHub?
Most helpful comment
As far I checked, theoretically that case could be solved by customizing
ODataEnumSerializer:https://github.com/OData/WebApi/blob/12d998c0e60ed9bcf1a45dbdc650a51a3578b763/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataEnumSerializer.cs#L83
It also can be required for deserializer:
https://github.com/OData/WebApi/blob/12d998c0e60ed9bcf1a45dbdc650a51a3578b763/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/EnumDeserializationHelpers.cs
https://github.com/OData/WebApi/blob/12d998c0e60ed9bcf1a45dbdc650a51a3578b763/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataEnumDeserializer.cs