JSON deserialization for a flattened class

Here I found the same question ...

Removing deserialization of a nested JSON structure into a flattened class using Json.NET using annotations

... but without the right answer. One of the best suggestions is to wrap the nested object in a new class, but this approach presents another problem: the name lego. In my example, the most logical name for this class is the same name as the parent class and, of course, is impossible. My example is simple, I just want to exclude the "language" property in the parent class. Can someone help me do this?

using Newtonsoft.Json;

public partial class NamedType
{
    public string Name { get; set; }
}

public class Proficiency
{
    public string Level { get; set; }

    public string Name { get; set; }
}

public class Language
{
    public int Id { get; set; }

    public string Name { get; set; }

    //public Language Language { get; set; } //Compiler error
    //public Language Value { get; set; } //Not correct
    //public NamedType Language { get; set; } //Compiler error
    //public NamedType Value { get; set; } //Ugly, isn't?

    public Proficiency Proficiency { get; set; }
}

List<Language> languageList = JsonConvert.DeserializeObject<List<Language>>(json);

Json example:

{
    "languages": [
        {
            "id": 1,
            "language": { "name": "Spanish" },
            "proficiency": {
                "level": "native_or_bilingual",
                "name": "Native or bilingual proficiency"
            }
        },
        {
            "id": 2,
            "language": { "name": "English" },
            "proficiency": {
                "level": "full_professional",
                "name": "Full professional proficiency"
            }
        },
        {
            "id": 3,
            "language": { "name": "Japanese" },
            "proficiency": {
                "level": "elementary",
                "name": "Elementary proficiency"
            }
        }
    ]
}
+1
source share
1 answer

, JSON #, DataMember JsonProperty, .

, DataContractJsonSerializer, Json.NET:

[DataContract]
public class Language
{
    [DataContract]
    class NamedType
    {
        [DataMember]
        public string name { get; set; }
    }

    [DataContract]
    class ProficiencyType
    {
        [DataMember]
        public string level { get; set; }
        [DataMember]
        public string name { get; set; }
    }

    [DataMember(Name="id")]
    public int Id { get; set; }

    [IgnoreDataMember] // Do not serialize this property
    public string Name { get; set; }

    [IgnoreDataMember]
    public string ProficiencyLevel { get; set; }

    [IgnoreDataMember]
    public string ProficiencyName { get; set; }

    [DataMember(Name="language")] // serialize this nested class property with name "language"
    [JsonProperty(ObjectCreationHandling=ObjectCreationHandling.Replace)] // When deserializing, always create a fresh instance instead of reusing the proxy class.
    NamedType LanguageName
    {
        get
        {
            return new NamedType { name = Name };
        }
        set
        {
            Name = (value == null ? null : value.name);
        }
    }

    [DataMember(Name = "proficiency")]
    [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
    ProficiencyType Proficiency
    {
        get
        {
            return new ProficiencyType { level = ProficiencyLevel, name = ProficiencyName };
        }
        set
        {
            ProficiencyLevel = (value == null ? null : value.level);
            ProficiencyName = (value == null ? null : value.name);
        }
    }
}

, DataContract Json.NET , :

public class Language
{
    class NamedType
    {
        public string name { get; set; }
    }

    class ProficiencyType
    {
        public string level { get; set; }
        public string name { get; set; }
    }

    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    [JsonIgnore]
    public string Name { get; set; }

    [JsonIgnore]
    public string ProficiencyLevel { get; set; }

    [JsonIgnore]
    public string ProficiencyName { get; set; }

    [JsonProperty(PropertyName = "language", ObjectCreationHandling = ObjectCreationHandling.Replace)]
    NamedType LanguageName
    {
        get
        {
            return new NamedType { name = Name };
        }
        set
        {
            Name = (value == null ? null : value.name);
        }
    }

    [JsonProperty(PropertyName = "proficiency", ObjectCreationHandling = ObjectCreationHandling.Replace)]
    ProficiencyType Proficiency
    {
        get
        {
            return new ProficiencyType { level = ProficiencyLevel, name = ProficiencyName };
        }
        set
        {
            ProficiencyLevel = (value == null ? null : value.level);
            ProficiencyName = (value == null ? null : value.name);
        }
    }
}
+1

All Articles