I am currently using the Newtonsoft Json.NET library to parse Json encoded data and then later convert nodes to the desired data type.
Here is an example data structure which is already working for me:
{
"ToolUtility": {
"SharedBrushListModel": {
"SelectedBrushInstanceID": 786
},
"SomethingElse": 42
}
}
And here is how I can interact with this data:
// Parse data and collate into groups of properties.
var store = new Dictionary<string, Dictionary<string, JProperty>>();
foreach (var group in JObject.Parse(jsonInput)) {
var storeGroup = new Dictionary<string, JProperty>();
store[group.Key] = storeGroup;
foreach (var property in group.Value.Children<JProperty>())
storeGroup[property.Name] = property;
}
// Convert nodes into the desired types.
var utilityNode = store["ToolUtility"];
var model = utilityNode["SharedBrushListModel"].ToObject<SharedBrushListModel>();
int somethingElse = utilityNode["SomethingElse"].ToObject<int>();
// Convert desired types back to nodes.
var nodeA = JToken.FromObject(model);
var nodeB = JToken.FromObject(somethingElse);
// Encode back to Json.
var outputRoot = new JObject();
outputRoot["ToolUtility"] = new JObject();
outputRoot["ToolUtility"]["SharedBrushListModel"] = nodeA;
outputRoot["ToolUtility"]["SomethingElse"] = nodeB;
string json = outputRoot.ToString(formatter);
The nice thing with this API is that I can parse the Json encoded data without including any type annotations, and then explicitly convert nodes into the specialized type at a later time when this has been resolved.
Is there a way with regular .NET to do the same sort of thing with XML?
<?xml version="1.0" encoding="utf-8"?>
<root>
<ToolUtility>
<SharedBrushListModel>
<SelectedBrushInstanceID>786</SelectedBrushInstanceID>
</SharedBrushListModel>
<SomethingElse>42</SomethingElse>
</ToolUtility>
</root>
I would like to achieve this using standard .NET libraries rather than requiring any third-party libraries such as Json.NET.
The trouble with including additional libraries is that a number of issues occur in the target environment (Unity game engine) where multiple assets include the same library.