I remember seeing some where that when you create multiple references of an object and copy one to the other pointers are created instead of a physical copy. I need to avoid this situation and I don’t recall how to avoid it when I am using json.
I am creating temporary json structures and then adding them to a single multilevel structure. When I initialize the temp structure (after copying to the main structure) it also get deleted from the main structure?
Dim TmpJ As New Xojo.Core.Dictionary
TmpJ = New Xojo.Core.Dictionary
Dim Rates As Xojo.Core.Dictionary
Rates = New Xojo.Core.Dictionary
TmpJ.Value("abc_options") = Array("Test Data")
Rates.Value("xyz_options") = TmpJ
Rates.Value("moretest") = "12345678"
MsgBox(json2text(Rates)) // All is good to here
TmpJ.RemoveAll
MsgBox(json2text(Rates)) // "abc_options" has been eliminated
Pretty sure this is happening because anything in TmpJ that is copied to Rates is only referenced. So when TmpJ is wiped so are the copied items to Rates.
How do I avoid this? Or is there a better way. I find it tricky to build multiple levels in a single json structure.
Before ( what I want)
{“moretest”:“12345678”,“xyz_options”:{“abc_options”:[“Test Data”]}}
After (not what I want)
{“moretest”:“12345678”,“xyz_options”:{}}
These will work but not exactly what I was looking for. The problem is I want to dynamically build a multi level json and I won’t always know the structure up front. If I did I could start with a skeleton. So I may be in a situation where I need to create something like this on the fly.
It’s easy enough to create a single layer json structure and include them one by one. That could get messy in some cases. Is there a better way to create a json key when some of the layers are missing.
which creates a whole new instance that is empty for you to fill up without touching the existing one. The existing one is held onto by the Rates dictionary because you had previously done:
Rates.Value("xyz_options") = TmpJ
With reference types like this as long as something has a reference then it wont disappear.
you are not copying Tmpj, you are creating a reference (what we used to call a pointer to it. The number of dictionaries that exist is equal to the number of times you do “new”.