AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Inner json object deletion
 
 

Hi,

I know I posted many weird JSON problems lastly, but I use JSON a lot, so .. :D

I had a porblem in my application which I managed to replicate after a few hours, but I have to confess it still seems a little weird.

I create a some kind of linked list in cs:

$$innerobject = ^jsoncreate(object)
 $
$innerobject.id inner
 
$$middleobject = ^jsoncreate(object)
 $
$middleobject.id middle
 
$$middleobject.next = $$innerobject
 
$$outerobject = ^jsoncreate(object)
 $
$outerobject.id outer
 
$$outerobject.next = $$middleobject


 
^jsonwrite($$innerobject

So far everything works as expected, the inner object exists:

{ "id": "inner"}

Now if I cut the chain at the first possible point, the lastof the chain is destroyed:

$$outerobject.next null
 
^jsonwrite($$innerobject

{ }

So what can I do if a want to reset references to inner json objects without destroying them?

 

 

 
  [ # 1 ]

The system currently checks if a json object is referenced by some other json object still and wont recursively delete it if it is. HOWEVER, mere reference from a variable does not protect you.  It would be too expensive to go consult all variables.

In your example, $$outerobject.next points to middleobject, which points to inner object. There are no other references, so you will lose your objects when you delete the reference from outer.  If you want it protected some other json object will have to do that.

 

 
  [ # 2 ]
Bruce Wilcox - Dec 12, 2016:

If you want it protected some other json object will have to do that.

Thank you very much.
I tested both array and object to store some references:

$$saveJSONObject = ^jsoncreate(object)
 ^
jsonobjectinsert($$saveJSONObject ob $$innerobject


and

$$saveJSONArray = ^jsoncreate(array)
 ^
jsonarrayinsert($$saveJSONArray $$innerobject

But neither prevents

$$outerobject.next null 

from deleting innerObject?

 

 
  [ # 3 ]

If $$outerobject.next points to a “middle” object, ant that middle object contains the inner one, then setting $$outerobject.next to null would logically have to delete the inner object as well, since it’s contained within the middle object, and that got removed by being set to null.

Or maybe I’m missing something, in which case please ignore me. cheese

 

 
  [ # 4 ]

It worked fine for me…. My test case was this
$$innerobject = ^jsoncreate(object)
$$innerobject.id = inner
$$middleobject = ^jsoncreate(object)
$$middleobject.id = middle
$$middleobject.next = $$innerobject
$$outerobject = ^jsoncreate(object)
$$outerobject.id = outer
$$outerobject.next = $$middleobject

^jsonwrite($$innerobject) 
$$saveJSONObject = ^jsoncreate(object)  —- originally not there in 1st test
^jsonobjectinsert($$saveJSONObject ob $$innerobject) —- originally not there in 1st test

$$outerobject.next = null
^jsonwrite($$innerobject)

 

 
  login or register to react