-2

Before serialize -

 "welcomePageConfig": {
        "welcomeTitleText": {
            "style": {
                "color": "#FFFFFF"
            },
            "content": {
                "sv": "Välkommen",
                "en": "Welcome"
            }
        }

I use JsonSerializer to deserialize below string to an object.

string jsonString = JsonSerializer.Serialize(welcomePageConfig);

After serialize -

{\"WelcomeTitleText\":{\"Style\":{\"Color\":\"#FFFFFF\"},\"Content\":[{\"Key\":\"sv\",\"Value\":\"Välkommen\"},{\"Key\":\"en\",\"Value\":\"Welcome\"}]}

welcomePageConfig = JsonSerializer.Deserialize<WelcomePageConfig>(jsonString);

When I try to deserialize, it gives me an error mentioning "The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]."

It pops after "{"Key...." this part. Since it's a dictionary.

public class WelcomePageConfig
    {
        [JsonProperty("welcomeTitleText")]
        public StylingComponent WelcomeTitleText { get; set; }
    }

public class StylingComponent
    {
        [JsonProperty("style")]
        public Style Style { get; set; }

        [JsonProperty("content")]
        public Dictionary<string, string> Content { get; set; }
    }

How to fix this issue?

Hash_Dew
  • 313
  • 1
  • 8
  • Please show your `WelcomePageConfig` class implementation. – Yong Shun Apr 20 '23 at 08:06
  • 1
    that list of objects that you point to is not a dictionary; Dictionary item has Key and Value properties not Key1 Value, Key2 Value, Key3 Value; Name of property cannot change unless you will write a custom converter that will ignore the number in the name and build the dictionary. – Rafal Apr 20 '23 at 08:08
  • There are two ways to serialise a dictionary, either as an array of key-value pairs, or as an json "object" notation. You have an array of key value pairs. To answer the question we need to see the definition of WelcomePageConfig. – Ben Apr 20 '23 at 08:36
  • @Rafal Sorry I copied the wrong json. Now I updated with the correct one. It contains Key and Value not Key1 and Key2 – Hash_Dew Apr 20 '23 at 08:50
  • @Ben I updated the question with more insights – Hash_Dew Apr 20 '23 at 08:56
  • @Hash_Dew What means "Before serialize " ? Why data is different ? Should be the same . Can you show how do you serialize it pls? – Serge Apr 20 '23 at 10:36

1 Answers1

1

There is quite a common convention that Dictionary<string, ...> is handled as JSON object during (de)serialization, this convention for example is supported by both System.Text.Json and Newtonsoft's Json.NET. It seems that at some point during serialization something is going not as expected (you have not shown your serialization code) and the Content is handled like IEnumerable (of key-value pairs) not as Dictionary, so you need to change the model for deserialization:

public class StylingComponent
{
    // ...
    public List<KeyValuePair<string, string>> Content { get; set; }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • This is the serialing code - string jsonString = JsonSerializer.Serialize(welcomePageConfig); After using keyValuePairs it doesn't give any errors but not deserializing. Objects set to null after deserializing. – Hash_Dew Apr 20 '23 at 12:23
  • @Hash_Dew there is no serialize code - only deserialization one: `welcomePageConfig = JsonSerializer.Deserialize>(jsonString);` . – Guru Stron Apr 20 '23 at 12:25
  • Just updated the question with that code. Please take a look. – Hash_Dew Apr 20 '23 at 12:27
  • 1
    @Hash_Dew check out [demo](https://dotnetfiddle.net/jTqlrx). Your update does not make sense - JSON contains a single object while you are trying to deserialize to a collection. – Guru Stron Apr 20 '23 at 12:34
  • @Hash_Dew `JsonPropertyAttribute` is attribute from Newtonsoft Json.NET and will be ignored by `JsonSerializer.Deserialize`. – Guru Stron Apr 20 '23 at 12:35
  • I tried to simplify the issue to make more easy to understand and made some mess there. Anyway inbuild json serializer didn't work for me. But I used newtonsoft serializer then it worked. Thanks alot for the help. – Hash_Dew Apr 21 '23 at 05:21