Deserialize List<string> from Json to List<object>

Viewed 3538

I have the following json string as a sample for my problem:

{
    "Code": "Admin",
    "Groups":
    [
        "Administrator",
        "Superuser",
        "User"
    ]
}

Also I have a class named User with code like this...

[JsonObject(MemberSerialization.OptIn)]
public class User
{
    public User (string code)
    {
        this.Code = code;
    }

    [JsonProperty]
    public string Code { get; set; }

    [JsonProperty("Groups")]
    private List<UserGroup> groups;
    public List<UserGroup> Groups
    {
       if (groups == null)
           groups = new List<UserGroup>();
       return groups;
    }
}

... and a class named UserGroup with - for this example - only this few code lines:

public class UserGroup
{
    public UserGroup (string code)
    {
        this.Code = code;

        // Some code to fill all the other properties, just by knowing the code.
    }

    public string Code { get; set; }

    // More properties
}

Now, what I want is that the above shown JSON string would be deserialized into an instance of an User and all strings in the "Groups" array should be deserialized into a List<UserGroup> with instances from each of those strings. Also - the other way round - should a User be serialized into an JSON string with only the Code property of the contained UserGroups.

I don't deserialize normally but I create an instance of an User and populate it with this code...

Newtonsoft.Json.JsonConvert.PopulateObject(jsonString, myUserInstance);

... but if I run the code with the above shown JSON string all I get is the following exception:

Newtonsoft.Json.JsonSerializationException: 'Error converting value "Administrator" to type 'UserGroup'.

My final requirement is that, I want the UserGroup to be serialized as an array of strings only if I serialize the User. When I serialize a UserGroup standalone as a root object, it should be serialized normally (with all properties). (For comparison, in Json.Net: Serialize/Deserialize property as a value, not as an object the object is serialized as a string in all situations.)

2 Answers
Related