wangyao5 / json-smart

Automatically exported from code.google.com/p/json-smart
0 stars 0 forks source link

JSONArray is deserialized to ArryList #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I use your excellent library in Android project. I run into issue where if I 
try to pass JSONArray to the Activity by doing Intent.putExtra(String key, 
Serializable array) when I try to retrieve that array doing 
Intent.getSeriaizableExtra(key, null) array is converted to the plain 
ArrayList. Not only that but all the nested JSONObjects are converted to 
HashMaps. Currently I go around this by dumping content of JSONArray to String 
and then recreating it. However that is quite memory intensive especially for 
larger data sets. 
Anything you would recommend?

Regards,

Bo Stone

Original issue reported on code.google.com by bost...@gmail.com on 12 Nov 2011 at 7:27

GoogleCodeExporter commented 9 years ago
Please can you send me a sample project showing the trouble.

Uriel.

Original comment by uriel.chemouni on 14 Nov 2011 at 9:52

GoogleCodeExporter commented 9 years ago
Sample Android project is attached. Import into Eclipse and run (you will need 
Google Android plugin). You will see that the original JSONObject is converted 
to plain HashMap during deserialization.

Just to note - this seems to be Android-related problem since the code below 
does everything properly

[code]
JSONObject original = (JSONObject) 
JSONValue.parse("{one:1,two:[three:3,four:4,five:'five']}");
        try {
            // serialize
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(original);
            oos.close();
            // deserialize
            byte[] pickled = out.toByteArray();
            InputStream in = new ByteArrayInputStream(pickled);
            ObjectInputStream ois = new ObjectInputStream(in); 
            Object o = ois.readObject();
            System.out.println("Got: " + o + ", with class type of " + (o == null ? null : o.getClass())); 
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
[/code]

Original comment by bost...@gmail.com on 14 Nov 2011 at 8:38

GoogleCodeExporter commented 9 years ago
your json message look to be invalide.

"{one:1,two:[three:3,four:4,five:'five']}"
should probably be replaced by:
"{one:1,two:{three:3,four:4,five:'five'}}"
or
"{one:1,two:{three:3,four:4,five:five}}"
or
"{'one':1,'two':{'three':3,'four':4,'five':'five'}}"

Original comment by uriel.chemouni on 15 Nov 2011 at 10:51

GoogleCodeExporter commented 9 years ago
Why? Two is JSON array. It parses just fine, I don't think that is the issue

Original comment by bost...@gmail.com on 15 Nov 2011 at 11:00

GoogleCodeExporter commented 9 years ago
if you want 'two' to be an array use :
"{'one':1,'two':['three',3,'four',4,'five','five']}"
or 
"{one:1,two:[three,3,four,4,five,five]}"

Original comment by uriel.chemouni on 15 Nov 2011 at 11:58

GoogleCodeExporter commented 9 years ago
I noticed that you closed this bug - has this been resolved? Can I get a patch?

Original comment by bost...@gmail.com on 19 Nov 2011 at 9:44

GoogleCodeExporter commented 9 years ago
Cross reference to posting on Stackoverflow.com http://goo.gl/CVw1U

Original comment by bost...@gmail.com on 9 Dec 2011 at 7:39