Code Monkey home page Code Monkey logo

Comments (5)

cjmconie avatar cjmconie commented on August 28, 2024 1

Yes, good reasoning for update(applying changes: JSON)– the intention is clearer.

Regarding the algorithm, our use case requires merging/updating in a similar fashion to the Javascript Object.assign() function. Here is an example with output:

const object1 = {
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
};

const object2 = {
  "array": [
    9,
    8,
    7
  ],
  "boolean": false,
  "null": null,
  "number": null,
  "object": {
    "a": "b",
    "c": null,
    "e": {
      "x": "y"
    }
  },
  "extra_string": "Hello Agian"
};

const objectResult = Object.assign(object1, object2);
console.log(objectResult);

// output
{
  "array": [
    9,
    8,
    7
  ],
  "boolean": false,
  "null": null,
  "number": null,
  "object": {
    "a": "b",
    "c": null,
    "e": {
      "x": "y"
    }
  },
  "string": "Hello World",
  "extra_string": "Hello Agian"
}

This means the partial update for your example would work the same, excepting that "null" would not result in a deletion, but rather an update:

{"x": "y"} + {} = {"x": "y"} // no change
{"x": "y"} + {"a": "b"} = {"x": "y", "a": "b"} // create
{"x": "y"} + {"x": 1} = {"x": 1} // update
{"x": "y", "a": "b"} + {"x": null} = {"a": null} // update (not delete)

We could also have different algorithms for the update which could be passed in as a enum case?

from generic-json-swift.

zoul avatar zoul commented on August 28, 2024

This is the essential definition of a JSON value:

enum JSON {
    case string(String)
    case number(Float)
    case object([String:JSON])
    case array([JSON])
    case bool(Bool)
    case null
}

So we need to specify how a merge would work on an old: JSON value with a new: JSON value. Some examples of how we expect this to work, given the partial update use case:

{"x": "y"} + {} = {"x": "y"} // no change
{"x": "y"} + {"a": "b"} = {"x": "y", "a": "b"} // create
{"x": "y"} + {"x": 1} = {"x": 1} // update
{"x": "y", "a": "b"} + {"x": null} = {"a": "b"} // delete

Is that all? Does it work the way you expect, Cameron? Given that, the gritty details could be:

  1. If old or new are anything but an object, return new.
  2. If both old and new are objects, create a merged object like this:
    1. Add keys from old not present in new (“no change” case).
    2. Add keys from new not present in old (“create” case).
    3. For keys present in both old and new, apply merge recursively to their values. If the result is .null, skip it (“delete” case), otherwise store (“update” case.)

Would this work?

At any rate, I am not comfortable talking about merging here. For me, merging would imply treating both old and new equally (as evident in the old and new names), whereas what we really do is treating new as a description of partial updates to old. So maybe it would be better to call the function something like update(applying changes: JSON)? Hopefully that would make the behaviour more clear.

from generic-json-swift.

zoul avatar zoul commented on August 28, 2024

I’ll be away for a week or two (sorry), but I’ll get back to you eventually. Thank you!

from generic-json-swift.

zoul avatar zoul commented on August 28, 2024

I see, so you really want a plain merge that would simply copy the null properties from the new JSON value. I was thinking about this from the REST PATCH method angle, where leaving out a property means “do not touch this” and specifying a null for a property means “delete the value”.

I don’t see anything wrong in doing it the Object.assign way, I am just thinking about the decoding behaviour. Because that can be different depending on whether the property is missing completely, or present and set to null – for example when the decoding init(from:) method uses defaults for non-present keys.

If simply setting the properties to null à la Object.assign is fine with you, I guess we can do it that way and revisit the decision if we run into problems later? Since now you are the only one with a real use case for the method at the moment.

from generic-json-swift.

zoul avatar zoul commented on August 28, 2024

Merged.

from generic-json-swift.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.