Android - json-smart deserialization issue

I have a strange problem in my Android code when I try to pass json-smart JSONObject or JSONArray as serializable data like Intent serializable extra. Json-smart is a very fast and skinny implementation of the JSON parser, I highly recommend it. JSONObject extends HashMap<String, Object> and JSONArray extends ArrayList<Object> with very little overhead. The emptiness of these objects overrides Object#write or Object#read

Here's the problem:

If I use these objects in say Fragment#onSaveInstanceState(String, JSONObject) , everything works fine. If I serialize / deserialize these objects in a simple Java sample, it works again as intended. However, if I use Intent#putExtra(String, JSONObject) and then try to get my JSONObject by doing

 JSONObject json = (JSONObject) intent.getSerializableExtra("JSON"); 

I will get a ClassCastException because what is returned by the method is a simple HashMap (and in the case of JSONArray, an ArrayList). Moreover, if I look inside the map / array, the content is completely devoid of any references to JSONObject / JSONArray and replaced by HashMaps / ArrayLists

I sent a ticket and provided a rough draft, but, unfortunately, the author closed it without permission, so I'm just trying to get to the bottom of This. If you go on a ticket, he has a simple project.

Is there a way out of this? At the moment I need to convert a JSONObject or JSONArray to String and rewrite it back to an object, for example, in:

 JSONArray feeds = (JSONArray) JSONValue.parse(intent.getStringExtra(RAW_FEED)); 
+4
source share
3 answers

Implementing android.os.Parableable:

 http://developer.android.com/reference/android/os/Parcelable.html 

Basically, you add a little extra code to your object to explain how to serialize and deserialize it. That's why he lost information about your class, he does not know how to serialize it correctly. If I were you, I would convey the object of value through your intentions to the verses of the JSON object simply because I know that you know exactly what type of object you are transmitting. You get your Parcelable as follows:

 getIntent().getExtras().getParcelable(key); 

I know this probably looks like a little extra code, but I actually think this is a pretty clean way to exchange data between actions. You go through a strong type, you indicate how to blow it off / inflate it, and your actions need not have any working knowledge of each other.

In the value object, you simply fill in the spaces as follows:

 /** Parcelable implementation: deserialize object * @param in Parcel object containing the data for this object * @return a Person object */ public Person( Parcel in ) { String[] data = new String[2]; in.readStringArray(data); this.firstName = data[0]; this.lastName = data[1]; } /** Parcelable implementation code */ public int describeContents() { return 0; } /** Parcelable implementation: Serialize object to be passed between activities via Intent * @param dest The parcel to transport the object * @param flags refer to Parcelable api * @return nothing */ public void writeToParcel(Parcel dest, int flags) { dest.writeStringArray( new String[] { this.firstName, this.lastName, }); } 

Hope this helps, maybe a little more code than what you were looking for, but this is a really clean solution, because when the targeted activity receives an object, you don’t have to worry about deserialization, which means cleaner code in your activity . Bold model, clean views :)

+3
source

Since JSON-Object is implicitly a string, why is it so simple to pass it as a String?

 // save the json string JSONObject json = ... intent.putExtra("json", json.toString()); // Let read it in in the next activity JSONObject json = new JSONObject(intent.getExtra("json")); 
+1
source

If you are reading this and wondering what solution I finally implemented, I realized that I would be happy to use a truncated version of the Map / List object, since it preserves the structure and all the keys. No need monkey with parcelable

0
source

Source: https://habr.com/ru/post/1385582/


All Articles