-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialise enum to ordinal value #1785
Comments
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.
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 WebApi/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataEnumSerializer.cs Line 83 in 12d998c
It also can be required for deserializer: |
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
in your Startup class |
Is this package source on GitHub? |
any news?
|
@raheph i cant find this in the protocol specification. can you tell me where to find this specifically? |
In my service every Enum is using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OData.Formatter.Serialization;
using Microsoft.OData;
using Microsoft.OData.Edm;
namespace WebApi.Serializers
{
public class IntegerEnumSerializer : IODataEdmTypeSerializer
{
private ODataEnumSerializer _innerSerializer;
public IntegerEnumSerializer(ODataEnumSerializer innerSerializer)
{
_innerSerializer = innerSerializer;
}
public ODataPrimitiveValue CreateODataEnumValue(object graph, IEdmEnumTypeReference enumType,
ODataSerializerContext writeContext)
{
if (graph == null)
{
return null;
}
// Serialize enum value as an integer
return new ODataPrimitiveValue(Convert.ToInt32(graph));
}
public Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
return _innerSerializer.WriteObjectAsync(graph, type, messageWriter, writeContext);
}
public ODataPayloadKind ODataPayloadKind => _innerSerializer.ODataPayloadKind;
public ODataValue CreateODataValue(object graph, IEdmTypeReference expectedType, ODataSerializerContext writeContext)
{
if (graph == null)
{
return null;
}
// Serialize enum value as an integer
return new ODataPrimitiveValue(Convert.ToInt32(graph));
// return _innerSerializer.CreateODataValue(graph, expectedType, writeContext);
}
public Task WriteObjectInlineAsync(object graph, IEdmTypeReference expectedType, ODataWriter writer,
ODataSerializerContext writeContext)
{
return _innerSerializer.WriteObjectInlineAsync(graph, expectedType, writer, writeContext);
}
}
} but i get the following error:
|
In case someone is still looking for a solution.
CustomSerializerContextWrapper:
Hope this solution works for you. |
I use a simpler method to achieve this by creating a custom
|
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.
Background:
I have an enumeration:
And a model class:
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 customJsonConverter
and apply the[JsonConverter()]
attribute toMyProperty
; I've also tried to add the custom converter throughservices.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 picksStringEnumConverter
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:
The text was updated successfully, but these errors were encountered: