1

Basically I got into problem where I am not sure what to do next. Maybe I just made my solution too complicated and there's one more simpler or I just don't see an answer. I am developing a system where each object has a prototype parent of its own. Prototype object has all the information that objects needs. During the serialization I only store properties that are different from the same property in prototype object. I do that do decrease serialized object file size. That seems nice, but it creates a new problem.

Here's an example: lets say I have a enemy character object. It has name, health, inventory values, etc. During game saving process I serialize all of it, excluding values that didn't change and are the same like in prototype object. That part is easy, but what I don't understand is how to solve my deserialization process.

When I load my game back I want to deserialize objects from my save files and then - if a prototype objects exists, I want to populate deserialized objects with prototype object data. The problem that I see in this case is that if character's inventory contains different items from its prototype - it will still receive those items from original blueprint object.

How can I automate cases like that where I want to skip some values if they are already there? For example if health value already exists - I don't want a original value from prototype. Or if inventory already has items in it - I don't want to receive items from blueprint object.

tommy_kid
  • 25
  • 2

1 Answers1

1

You can do something like this:

JObject o1 = JObject.Parse(@"{
  'FirstName': 'John',
  'LastName': 'Smith',
  'Enabled': false,
  'Roles': [ 'User' ]
}");
JObject o2 = JObject.Parse(@"{
  'Enabled': true,
  'Roles': [ 'User', 'Admin' ]
}");

o1.Merge(o2, new JsonMergeSettings
{
    // union array values together to avoid duplicates
    MergeArrayHandling = MergeArrayHandling.Union
});

string json = o1.ToString();
// {
//   "FirstName": "John",
//   "LastName": "Smith",
//   "Enabled": true,
//   "Roles": [
//     "User",
//     "Admin"
//   ]
// }

Copied from: http://james.newtonking.com/archive/2014/08/04/json-net-6-0-release-4-json-merge-dependency-injection

Paul
  • 1,011
  • 10
  • 13