简体   繁体   中英

C# Partial deserialization and full serialization

I have a json file in C#. It's 70mb of data. I want to read it into a Windows Forms application with JSON.net, edit some of the data and save it back out. My problem is I don't want to create ALL of the data structures for this JSON file but I DO want to create SOME of them. When I reserialize the file I want all the changes from the data structures I did create without losing the data for the structures I didn't. Any idea if this is possible? I hope my question is clear.

I suggest you use the XPath equivalent for Json. With Json.NET you can parse the string and create a dynamic object.

With SelectToken you can query values, or using Linq .

For the example I will assume a json string that contains the serialized object.

var o = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
o.SelectToken("TheNodeToChange").Replace("TheNewValue");
var updatedJsonString = JsonConvert.SerializeObject(o);

This will deserialize the entire object and you do need to know the node to change. But you don't have to implement the complete object model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM