Giter VIP home page Giter VIP logo

Comments (5)

clugg avatar clugg commented on September 16, 2024

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.

MAGNAT2645 avatar MAGNAT2645 commented on September 16, 2024

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.

clugg avatar clugg commented on September 16, 2024

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.

clugg avatar clugg commented on September 16, 2024

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.

clugg avatar clugg commented on September 16, 2024

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)

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.