Reputation: 150
have you noticed that if you put a JSONObject into another JSONObject with put(), the container JSONObject won't store the reference to the first JSONObject, but a new JSONObject?
An example:
JSONObject jtmp1 = new JSONObject();
JSONObject jtmp2 = new JSONObject();
jtmp1.put("test", jtmp2);
System.out.println(System.identityHashCode(jtmp2));
System.out.println(System.identityHashCode(jtmp1.getJSONObject("test")));
The output from the 2 prints is different, therefore jtmp2 is not directly stored in jtmp1.
Is this the normal behavior? Are there any workarounds to it? I'm quite new to JAVA but in my opinion this is not the behavior one would expect.
Upvotes: 3
Views: 1077
Reputation: 5712
I ran into this problem too, it appears to be normal behavior, but I have had trouble consistently reproducing it. The way to work around is treat all objects being put in and taken out as pass by value. It is probably actually an issue, but that JSON library that you appear to be using is not supported anymore. If you can, I would change to GSON, which is very similar in syntax to the library you are using, does not have this issue, and is currently supported.
Upvotes: 0