Home > Enterprise >  JSON serialize return type rather than the property of the return type
JSON serialize return type rather than the property of the return type

Time:03-16

I have the following JSON

{"KeyValuePairs":{"Gender":"Male","IsDeveloper":true,"Language":"English"},"PreferredLanguage":"C#","PreferredIDE":"Visual Studio","PreferredSourceControl":"Team Foundation Server (TFS)"}

and it is based on the class of:

public class DataModel
{
    public Dictionary<string, object> KeyValuePairs { get; set; }

    public string PreferredLanguage { get; set; }

    public string PreferredIDE { get; set; }

    public string PreferredSourceControl { get; set; }

    public DataModel()
    {
        KeyValuePairs = new Dictionary<string, object>();
    }
}

I would like to make it like the following when I serialize the class object of DataModel:

{"Gender":"Male","IsDeveloper":true,"Language":"English","PreferredLanguage":"C#","PreferredIDE":"Visual Studio","PreferredSourceControl":"Team Foundation Server (TFS)"}

The reason on why I put "Gender", "IsDeveloper", or so on inside the KeyValuePairs, as because it is being generated dynamically from JSON file (it could contains anything other than "Gender", "IsDeveloper"), while the other properties like "PreferredLanguage", "PreferredIDE" and "PreferredSourceControl" does not

Also, if the above being achieved, how can I put back into KeyValuePairs for the "Gender", "IsDeveloper" or any other properties inside KeyValuePairs previously when doing the deserialization?

Basically I would like to convert all properties inside the DataModel class to the Dictionary<string, object> pairs, which Newtonsoft.Json did it perfectly when there is only primitive type for the return type of the property, but for my case, I would like to set the KeyValuePairs into Dictionary<string, object> instead of KeyValuePairs: Dictionary<string, object>, and also when doing the deserialization, system will detect if there is no name of the property inside DataModel class, it will go add directly into KeyValuePairs (for example, "Gender" not exists in DataModel class, then it will be added into KeyValuePairs when doing the deserialization)

Or is there any way that I can achieve the same?

Thank you very much

CodePudding user response:

you can use JsonExtensionData - Instructs the JsonSerializer to deserialize properties with no matching class member into the specified collection and write values during serialization.

var json="{\"Gender\":\"Male\",\"IsDeveloper\":true,\"Language\":\"English\",\"PreferredLanguage\":\"C#\",\"PreferredIDE\":\"Visual Studio\",\"PreferredSourceControl\":\"Team Foundation Server (TFS)\"}";
    
DataModel dataModel= JsonConvert.DeserializeObject<DataModel>(json);

Test

    json=JsonConvert.SerializeObject(dataModel);

creates the same json

class

public class DataModel
{
    [JsonExtensionData]
    public Dictionary<string, object> KeyValuePairs { get; set; }

    public string PreferredLanguage { get; set; }

    public string PreferredIDE { get; set; }

    public string PreferredSourceControl { get; set; }

    public DataModel()
    {
        KeyValuePairs = new Dictionary<string, object>();
    }
}

UPDATE

if you want at first convert your json to normalized one you can do it this way:

var json = "{\"KeyValuePairs\":{\"Gender\":\"Male\",\"IsDeveloper\":true,\"Language\":\"English\"},\"PreferredLanguage\":\"C#\",\"PreferredIDE\":\"Visual Studio\",\"PreferredSourceControl\":\"Team Foundation Server (TFS)\"}";
var jsonParsed = JObject.Parse(json); 

foreach (var item in ((JObject)jsonParsed["KeyValuePairs"]).Properties())
        jsonParsed.Add(item.Name, item.Value);

jsonParsed.Property("KeyValuePairs").Remove();

json = jsonParsed.ToString();

or you can deserialize jsonParsed as well instead of creating a new json

DataModel dataModel =jsonParsed.ToObject<DataModel>();
  • Related