0

U am facing a little Json conversion problem using Newtonsoft in Azure Functions.

I have an HttpTrigger Function and am receiving a json string and convert it into my complex object afterwards.

My goal is to be able to convert my json string (below) to a c# Dictionary<AppLanguage, string>() object.

I have the feeling it cannot read the integer in key and convert it to my enum value. So i guess there's a configuration for that?

 System.Private.CoreLib: Exception while executing function: MyAppBranchFunctions. Newtonsoft.Json: Unexpected token StartArray when parsing enum. Path 'Texts.Tutorials', line 1, position 382828.

The json looks something like this:

{
"xxx":
    {
    "Texts":{"Tutorials":[{"Key":1,"Value":"tutorial"},{"Key":4,"Value":""},{"Key":5,"Value":""},{"Key":6,"Value":""},{"Key":7,"Value":""},{"Key":8,"Value":""},{"Key":9,"Value":""},{"Key":10,"Value":""},{"Key":2,"Value":""},{"Key":3,"Value":""}]
    }
}

The signatures of the classes looks as follow:

public class Texts
    {
    [JsonConverter(typeof(StringEnumConverter))]
    public Dictionary<AppLanguage, string> Tutorials { get; set; }
    }

[JsonConverter(typeof(StringEnumConverter))]
    public enum AppLanguage
    {
        /// <summary>
        /// german
        /// </summary>
        [EnumMember(Value = "de")]
        de,
        /// <summary>
        /// english
        /// </summary>
        [EnumMember(Value = "en")]
        en,
        /// <summary>
        /// french
        /// </summary>
        [EnumMember(Value = "fr")]
        fr,
AlBlue
  • 23,254
  • 14
  • 71
  • 91
lcsl
  • 51
  • 6
  • `AppLanguage` is an integer, not an object. Enums are essentially aliases for numeric values. Are you asking how to serialise using an enumeration's name instead of its value? – Panagiotis Kanavos Jun 25 '20 at 14:19
  • It would be helpful to see more of your code. The code that is doing the conversion, for example. – mlibby Jun 25 '20 at 14:20
  • @mlibby there is no. It's Azure Functions - its automatically converting. What I did however, i just use the default JsonConvert Newtonsoft.Json.JsonConvert.DeserializeObject(req); – lcsl Jun 25 '20 at 14:30
  • @PanagiotisKanavos - well i have the strings in my cosmos db. i have an api which gets the json from the cosmos db (where its already int) and sends it to my azure functions app. i receive the ints in my functions app, but it doesnt convert the dictionary unfortunatly – lcsl Jun 25 '20 at 14:30
  • It looks like the dictionary was serialized as an array of key/value pairs. To deserialize such an array into a dictionary, see [Newtonsoft Json Deserialize Dictionary as Key/Value list from DataContractJsonSerializer](https://stackoverflow.com/q/28451990/3744182) or [Serialize dictionary as array (of key value pairs)](https://stackoverflow.com/q/12751354/3744182). Also, your `AppLanguage` enum doesn't have enough values to deserialize all the key values shown in the JSON. Also, since the key values are integers not strings, `StringEnumConverter` is not needed here. – dbc Jun 25 '20 at 14:31

1 Answers1

0

In my case I had to serialize my json object to actual json before I add it as JsonBody to my RestSharp Post. I had like request.AddJsonBody(myObject) instead of request.AddJsonBody(jsonOfMyObject) which solved my problem now.

Should've posted my "post" code as well i guess.

Thanks for the replies.

lcsl
  • 51
  • 6