Comments (5)
I like the idea. I'll probably combine these into a single json_merge
with a replace
parameter. I haven't decided whether it should merge against the existing object or return a new one yet. Any thoughts?
from sm-json.
I thought about it too. We can propably add two functions like json_merge (apply to existing object) and json_merge_ex (return new object with data from two given objects).
from sm-json.
I have been thinking about the logistics of this. I think it makes the most sense to have a single merge function which merges in-place onto the first object.
void json_merge(JSON_Object to, JSON_Object from, bool replace = true)
replace
to denote whether or not to replace existing keys on to
if they are also in from
.
Then we can add the usual helper function onto the methodmap itself.
methodmap JSON_Object {
void Merge(JSON_Object from, bool replace = true)
}
At this point, you might want to merge two objects without directly affecting either of them. The easiest way to do this would be to create a new object and merge both onto it.
JSON_Object newObj = new JSON_Object();
newObj.Merge(obj1);
newObj.Merge(obj2);
At this point, I think it is also clear that we are missing a json_copy
function, to either perform a shallow or deep copy of an object. I imagine a signature like this, but I am unsure of whether shallow or deep copying should be the default, or if there should be a default at all.
enum JSONCopyMode
{
JSON_COPY_SHALLOW,
JSON_COPY_DEEP
};
JSON_Object json_copy(JSON_Object obj, JSONCopyMode mode /* = ? */)
And, of course, the helper function.
methodmap JSON_Object {
JSON_Object Copy(JSONCopyMode mode /* = ? */)
}
At this point we'd be able to create a fresh instance of two merged objects with no references back to the initial instances.
JSON_Object newObj = new JSON_Object();
newObj.Merge(obj1.Copy());
newObj.Merge(obj2.Copy());
Of course, JSON_Array
could also be merged and copied, but merging an array would be more like concatenation, which makes me wonder whether the API should be named differently for that.
from sm-json.
I have added support for merging instances in v2.1.0. I've also updated the README with examples, but it is fairly similar to what I outlined above. I will look into json_copy
for a later release.
from sm-json.
I have added support for copying instances in v2.2.0. I've also updated the README with examples.
from sm-json.
Related Issues (20)
- Single Quotes HOT 2
- Default Values HOT 4
- Some Additions and Improvements HOT 7
- [Bug] json_decode, SM 1.11 HOT 1
- [Bug] Compilation error with global defined array HOT 1
- [Feature Req] implement function to convert Unicode escape sequences to Unicode characters HOT 2
- [Bug] json encoding utf-8 fails HOT 3
- Some optimization HOT 1
- [Feature Req] Sort order of keys of the second level depth HOT 5
- [Feature Req] Dump to file function HOT 9
- [Bug] Snapshots not deleted
- [Bug]Int64 Not supported HOT 4
- [Feature Req] Error Buffer HOT 1
- json_read_from_file handle is not closed HOT 1
- json_is_meta_key does not exist on v4 HOT 7
- [Bug] Error using json_cleanup_and_delete HOT 2
- [Bug] Ordered keys are not preserved on `DeepCopy()` HOT 4
- [Feature Req] Properly handle duplicate keys when decoding HOT 3
- [Bug] JSON objects passed to forwards then iterated on causes memory problems HOT 9
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 sm-json.