Is there a way to copy an object?

283 views
Skip to first unread message

Ryan Phillips

unread,
Oct 31, 2009, 1:10:52 AM10/31/09
to jansso...@googlegroups.com
I have a parsed json object and have fetched an object using
json_object_get. Is there a way to copy this gotten object to another
json_t object?

The library is great. Thanks!

Regards,
Ryan Phillips

Petri Lehtinen

unread,
Oct 31, 2009, 4:26:05 PM10/31/09
to jansso...@googlegroups.com
Ryan Phillips wrote:
> I have a parsed json object and have fetched an object using
> json_object_get. Is there a way to copy this gotten object to another
> json_t object?

I don't quite understand what you're trying to do. You have parsed a
JSON object:

{
"a": {
...
}
}

like this:

json_t *json = json_load_file("/path/to/json", NULL);

Then fetched the object inside the first object:

json_t *a = json_object_get(json, "a");

Now, what do you want to do? Please explain a bit more clearly.

Petri

Ryan Phillips

unread,
Oct 31, 2009, 7:28:42 PM10/31/09
to jansso...@googlegroups.com
On Sat, Oct 31, 2009 at 3:26 PM, Petri Lehtinen <pe...@digip.org> wrote:
>
> Ryan Phillips wrote:
>> I have a parsed json object and have fetched an object using
>> json_object_get. Is there a way to copy this gotten object to another
>> json_t object?
>
> I don't quite understand what you're trying to do. You have parsed a
> JSON object:
>
>  {
>     "a": {
>        ...
>     }
>  }
>
> like this:
>
>  json_t *json1 = json_load_file("/path/to/json", NULL);
> json_t *json2 = json_object();

>
> Then fetched the object inside the first object:
>
>  json_t *a = json_object_get(json1, "a");
> json_object_set(json2, "a", a);
> json_decref(json1);

>
> Now, what do you want to do? Please explain a bit more clearly.
>
> Petri

Hi Petri,

The documentation states that object 'a' is a borrowed reference. What
I need is to basically do a deep copy of object 'a', place it into
json2, and be able to free json1.

I have tweaked the example to perhaps clarify things.

Regards,
Ryan

Petri Lehtinen

unread,
Nov 1, 2009, 6:14:15 AM11/1/09
to jansso...@googlegroups.com
Ryan Phillips wrote:
> > {
> > "a": {
> > ...
> > }
> > }
> >
> > like this:
> >
> > json_t *json1 = json_load_file("/path/to/json", NULL);
> > json_t *json2 = json_object();
> >
> > Then fetched the object inside the first object:
> >
> > json_t *a = json_object_get(json1, "a");
> > json_object_set(json2, "a", a);
> > json_decref(json1);
>
> The documentation states that object 'a' is a borrowed reference. What
> I need is to basically do a deep copy of object 'a', place it into
> json2, and be able to free json1.

The code in your example works correctly. The call

json_object_set(json2, "a", a);

increments the reference count of 'a', so 'a' won't be destroyed when
you call

json_decref(json1);

All the other values inside json1 will be destroyed along with json1
itself.

You don't need a deep copy because 'a' will only destroy (decrement
the refcount of its children) when it is destroyed itself.

Do I make things any clearer? :)

Petri

Ryan Phillips

unread,
Nov 1, 2009, 11:38:52 AM11/1/09
to jansso...@googlegroups.com

Thanks, that explanation helps.

Regards,
Ryan

vopatek

unread,
Nov 1, 2009, 6:01:57 PM11/1/09
to Jansson users
Hi Ryan and Petri.

I was reading your posts and want to continue on the deep copy thingy
that Ryan was asking about.

The json structure should probably have some form of deep copy
functionality that allocates
new memory and copies the content from one json structure to the
other.
That is, if you change something in the copy it will not change the
original.

Something like:
root2 = json_copy(root1);

But references should probably be cleared in the copy, otherwise it
might be weird or?
I think of a json_copy function having the same functionality as this:
char *dump = json_dumps(root1, 0);
root2 = json_loads(dump, &error);

Or perhaps this was the way to do a deep copy of a json structure ;-)?
/Martin

Petri Lehtinen

unread,
Nov 2, 2009, 3:01:45 PM11/2/09
to jansso...@googlegroups.com
vopatek wrote:
>
> Hi Ryan and Petri.
>
> I was reading your posts and want to continue on the deep copy thingy
> that Ryan was asking about.
>
> The json structure should probably have some form of deep copy
> functionality that allocates
> new memory and copies the content from one json structure to the
> other.
> That is, if you change something in the copy it will not change the
> original.
>
> Something like:
> root2 = json_copy(root1);

Yeah. Ryan's mail also made me think about copying/cloning JSON
values. I think that both shallow and deep copying might be useful.

A deep copy is a new JSON value that has deep copies of the original's
children as its children, i.e. all the values inside objects and
arrays at all depth levels are new references.

A shallow copy is a copy of the first level only, so the children of
objects and arrays are just references to the original's children.

Obviously, shallow and deep copies make difference only for objects
and arrays, simple values are always fully copied.

I can think of two possible APIs. Either

json_t *json_copy(json_t *json, int deep);

which makes a shallow copy if deep = 0 and a deep copy if deep = 1, or

json_t *json_copy(json_t *json);
json_t *json_deep_copy(json_t *json);

i.e. separate functions for shallow and deep copying. Perhaps the
latter is better.

>
> But references should probably be cleared in the copy, otherwise it
> might be weird or?
> I think of a json_copy function having the same functionality as this:
> char *dump = json_dumps(root1, 0);
> root2 = json_loads(dump, &error);
>
> Or perhaps this was the way to do a deep copy of a json structure ;-)?
> /Martin

Heh, this is obviously the easiest way. But I believe not the most
effective :)

Petri

Reply all
Reply to author
Forward
0 new messages