0

tl;dr is there an existing, standardized way to store sequences of diffs to a JSON or JSON-type object?

Question:

So currently I have something a class that looks something like this (pseudocode):

class SwimmingPool
{
  JSON description;

  void swim()
  {
    startTime = getCurrentTime();
    enterWater(); flailLimbs(); exitWater();
    endTime = getcurrentTime();
    description["In water"].append({"start" : startTime, "stop" : stopTime});
  }

  void buyPrezel()
  {
    sendCash(pretzelPrice);
    description["Snacks"]["Pretzels"]["Quantity"]++;
    description["Total sodium consumption (mg)"] += 30;
  }

  // ... other functionality ...

  void leave()
  {
    description["Left at"] = getCurrentTime();
    log(description);
    description.clear();
  }
}

So if I run some code like

pool.swim();
pool.buyPretzel();
pool.swim();
pool.leave();

Then I get a JSON object logged that looks something like

{
  "Total sodium consumption (mg)" : 30,
  "In water" : [ 
    { "start" : "12:13", "stop" : "12:15" },
    { "start" : "13:00", "stop" : "13:02" }
  ],
  "Snacks" : { "Pretzels" : 1 },
  "Left at" : "13:16"
}

and if I then make another visit to the pool, I'll get a second JSON object logging this.

However, this arrangement is less than ideal for a couple of reasons:

  1. I might like to see the current state of the description while I'm still at the pool.
  2. For reasons beyond my control, the program is routinely terminated without notice, causing anything stored in the description but not yet logged to be lost.

As such, I'm thinking that instead I should store a sequence of diffs to the JSON object. For instance, I could instead produce a log that looked something like the following:

descriptions["SwimmingPool1746"]["In water"][8]["start"] = "13:45"
descriptions["SwimmingPool1746"]["In water"][8]["stop"] = "13:47"
descriptions["SwimmingPool408"]["In water"][8]["stop"] = "13:46"
descriptions["SwimmingPool408"]["In water"][8]["stop"] = "13:48"
descriptions["SwimmingPool408"]["Snacks"]["Pretzels"]["Quantity"] = 6
descriptions["SwimmingPool408"]["Total sodium consumption (mg)"] = 180
descriptions["SwimmingPool408"]["Snacks"]["Pretzels"]["Quantity"] = 7
descriptions["SwimmingPool408"]["Total sodium consumption (mg)"] = 210
descriptions["SwimmingPool408"]["Left at"] = "13:52"
flushLog(descriptions["SwimmingPool408"])
descriptions["SwimmingPool1746"]["In water"][8]["start"] = "13:55"
descriptions["SwimmingPool1746"]["In water"][8]["stop"] = "13:57"

Then I could have a separate process that streams this log, follows the instructions it's outputting, and builds up the JSON objects. Then I could inspect the objects in situ, and I wouldn't lose them if the program writing this output crashed.

I'd prefer not to re-invent the wheel here. Is there some extant format for doing this sort of thing? Does this sort of approach have a name?

2

Have a look at JSON Patch. There are libraries available to apply patches to a JSON document and to create patches that describe the difference between two versions of a JSON document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.