clugg / sm-json

A pure SourcePawn JSON encoder/decoder.
GNU General Public License v3.0
82 stars 8 forks source link

[Suggestion] Some new useful functions #8

Closed MAGNAT2645 closed 4 years ago

MAGNAT2645 commented 4 years ago

Would be good to implement some new helper functions like json_merge and json_copy (without need to use json_encode and json_decode).

json_merge would merge two given JSON_Objects into single object. json_copy would copy keys from first object to second if second object doesn't have these keys.

Idk if this is a good idea/suggestion.

clugg commented 4 years ago

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?

MAGNAT2645 commented 4 years ago

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).

clugg commented 4 years ago

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.

clugg commented 4 years ago

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.

clugg commented 4 years ago

I have added support for copying instances in v2.2.0. I've also updated the README with examples.