5

Is there a way to configure how Azure Functions serializes an object into JSON for the return value? I would like to use strings rather than ints for enum values.

For instance, given this code:-

public enum Sauce
{
    None,
    Hot
}

public class Dish
{
    [JsonConverter(typeof(StringEnumConverter))]
    public Sauce Sauce;
}

public static class MyFunction
{
    [FunctionName("MakeDinner")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
    {
        var dish = new Dish() { Sauce = Sauce.Hot };
        return req.CreateResponse(HttpStatusCode.OK, dish, "application/json");
    }
}

The function returns:-

{
   "Sauce": 1
}

How can I make it return the below?

{
   "Sauce": "Hot"
}

I've tried returning a string instead of an object, but the result contains \" escapes; I want a JSON result, not an escaped string representation of a JSON object.

I know that in standard ASP.NET I can use the config to set serialization options. Is this even possible in Functions, or should I convert all my Enums to string constants?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
System.Cats.Lol
  • 1,620
  • 1
  • 26
  • 47

2 Answers2

3

This had me stumped for a while but check this answer.

Basically, use the Json.NET attribute StringEnumConverter.

[JsonConverter(typeof(StringEnumConverter))]
public enum Sauce
{
    [EnumMember(Value = "none")]
    None,
    [EnumMember(Value = "hot")]
    Hot
}
NickBrooks
  • 81
  • 3
2

Based on my test, I found the default version of the Newtonsoft.Json(installed when you created the azure function project) is 10.0.2. By using this version of the Newtonsoft.Json, it will auto replace the enum string name with the number.

Here is a workaround, I suggest you could open the Nuget Package install the Newtonsoft.Json 9.0.1, then it will work well.

More details, you could refer to below image:

enter image description here

Result:

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • Good to know, but I hate to have to revert to an old version of a dependency just to solve this problem. – System.Cats.Lol Aug 07 '17 at 19:44
  • But I have tried a lot of ways to use the 10.0.2 auto return the string value of the enum. It doesn't work. I guess this may related with azure function SDK. This is a workaround, that makes your function work well. I suggest you could continue followed the Newtonsoft.Json and azure function team to check if this could worked. – Brando Zhang Aug 08 '17 at 09:07
  • 1
    Update, according to this [answer](https://stackoverflow.com/a/45623382/7609093), you could found azure function now doesn't support 10.0.2. The develop team will connect to the VS team to update the version of the Newtonsoft.Json version. – Brando Zhang Aug 14 '17 at 00:50
  • @System.Cats.Lol , now the newest Visual studio preview used the 9.0.1 as the default Newtonsoft.Json version. You could update the VS. If you feel my answer is useful /helpful.Please mark it as an answer so that other folks could benefit from it. – Brando Zhang Aug 30 '17 at 06:08
  • this is more of a workaround than an actual answer (control over how enums are serialized on Azure), but apparently it's the best we can do in the current version of Functions. If an actual answer gets posted, I may change the accepted solution, just a heads-up. – System.Cats.Lol Aug 30 '17 at 18:28