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.
Asked
Active
Viewed 903 times
0
-
Does this thing absolutely have to be JSON instead of a database? To any question of the form "How do I painfully kludge A into a fragile mutant imitation of something B excels at", the answer is almost always "If you want B, you know where to find it". – 15ee8f99-57ff-4f92-890c-b56153 Jun 27 '17 at 17:40
-
What have you tried? I'm sure it may be possible with some filtering on the data before grabbing it from the source. But it's difficult to say without knowing what you have actually done. – geostocker Jun 27 '17 at 17:40
-
You can gain complete control of the serialization process so of course this is possible. Implement ISerializable if necessary, store a reference to the file, and when it comes time to serialize, load the file, make your changes, and either save them directly to the file or return its content. It's the implementation that's hard here - the concept is easy. – hoodaticus Jun 27 '17 at 17:45
-
duplicate question - https://stackoverflow.com/questions/5404303/using-json-net-partial-custom-serialization-of-a-c-sharp-object – Jun 27 '17 at 17:50
1 Answers
2
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.
-
1This was the idea behind my post but didn't know you could parse to a var like that, thanks for teaching me something. – Jason Maxwell Jun 28 '17 at 18:33