0

Is it possible to use the EnumMemberAttribute when deserializing JSON in a .Net AWS Lambda? I'm trying to deserialize the following payload into the following object.

Payload:

{
    "Option" : "Option 01"
}

Object definition:

public enum Option
{
    [EnumMember(Value = "Option 01")]
    One, 

    [EnumMember(Value = "Option 02")]
    Two
}

public class Payload
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public Option Option { get; set; }
}

But this throws an exception suggesting that the DefaultLambdaJsonSerializer doesnt support the EnumMemberAttribute:

Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException: Error converting the Lambda event JSON payload to type MyProject.Payload: The JSON value could not be converted to MyProject.Option. Path: $.Option | LineNumber: 1 | BytePositionInLine: 26. at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)

With the current object definition above, the the values One and Two can be deserialized (along with any int value).


For some more context, the Payload object is an input parameter to my lambda, so my function handler is as follows, and I'm just pasting the JSON payload into the mock lambda test tool while debugging the lambda in Visual Studio.

public void FunctionHandler(Payload payload, ILambdaContext context)
{
    // ...
}
devklick
  • 2,000
  • 3
  • 30
  • 47
  • 1
    [`JsonStringEnumConverter`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter?view=net-5.0) from [tag:system.text.json] does not support renaming of enums. See: [System.Text.Json: How do I specify a custom name for an enum value?](https://stackoverflow.com/a/59061296/3744182). In fact this looks like a duplicate, agree? – dbc Jan 28 '21 at 19:09
  • 1
    @dbc Yeah, this looks like a duplicate. Thanks for directing me here, the suggested package `Macross.Json.Extensions` brings in the support I need for this. – devklick Jan 28 '21 at 19:53

0 Answers0