Comments (5)
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.
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:
- If
old
ornew
are anything but an object, returnnew
. - If both
old
andnew
are objects, create a merged object like this:- Add keys from
old
not present innew
(“no change” case). - Add keys from
new
not present inold
(“create” case). - For keys present in both
old
andnew
, apply merge recursively to their values. If the result is.null
, skip it (“delete” case), otherwise store (“update” case.)
- Add keys from
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.
I’ll be away for a week or two (sorry), but I’ll get back to you eventually. Thank you!
from generic-json-swift.
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.
Merged.
from generic-json-swift.
Related Issues (16)
- Publish new version of the pod? HOT 2
- Add support for Swift Package Manager
- Automate release process HOT 1
- Add Linux support
- Switch to a cross-platform framework
- Usage after decoding HOT 6
- Initialisation using `nil` or `NSNull` HOT 5
- Tags Are Not SPM-Compliant HOT 3
- JSON initialisation using variables of primitive type HOT 2
- Mutation support HOT 2
- NSNumber Initialization HOT 3
- How to use JSON in Core Data HOT 7
- Provide `Decoder` to turn `JSON` into `Decodable` HOT 2
- Loop through JSON keys?
- Improve querying using dynamic member lookup? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generic-json-swift.