I would like to find a way to map multiple json strings to a single object in c#. I am currently using Newtonsoft.Json in a solution, but I am not opposed to using something else.
Here is an example using a Unit object and different json strings.
General object
public class Unit
{
public string Year { get; set; }
public string Model { get; set; }
}
Example json objects
{
"UnitYear":"2018",
"UnitModel":"F250 Super Duty"
}
{
"AssetYear":"2019",
"AssetModel":"Corvette Stingray"
}
If I were to run each json string through the Newtonsoft.Json deserialize method i'd want the output to be like so.
Unit1 : Year = 2018, Model = F250 Super Duty
Unit2 : Year = 2019, Model = Corvette Stingray
For now I'll store the mappings in a dictionary then later on transfer it to a database structure.
private static Dictionary<string, List<KeyValuePair<string, string>>> Units = new Dictionary<string, List<KeyValuePair<string,string>>>
{
{ "0000001", new List<KeyValuePair<string,string>>
{
new KeyValuePair<string,string>("Year", "UnitYear"),
new KeyValuePair<string,string>("Model", "UnitModel")
}
},
{ "0000002", new List<KeyValuePair<string,string>>
{
new KeyValuePair<string,string>("Year", "AssetYear"),
new KeyValuePair<string,string>("Model", "AssetModel")
}
},
};
I found that Newtonsoft.Json has a customer converter that can be implemented. The code I found on the website was very lacking. I did notice that you have to instantiate the custom converter to pass into the deserialize method. I'm think that I can use the constructor to pass in the identifier for the json that I'm converting.
Unit u1 = JsonConvert.DeserializeObject<Unit>(json, new GenericConverter("0000001"));
Unit u2 = JsonConvert.DeserializeObject<Unit>(json, new GenericConverter("0000002"));
But I am lost as to how I utilize the ReadJson that you have to override.
Any help here would be very appreciated.
Thank you