anshulmajoka / google-gson

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

java.io.NotSerializableException: com.google.gson.internal.LinkedTreeMap$1 #621

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the product are you using? Gson 2.3.1
On what operating system? ArchLinux x86

What steps will reproduce the problem?
1. Create a Parcelable object which contains a Map which is filled by Gson
private Map<String, ?> mMap;

2.Write this map as a Serilizable in writeToParcel (I do this because I need to 
maintain Gson property order. If I do dest.writeMap() I will obtain a HashMap 
when creating from Parcel)
dest.writeSerializable((Serializable) mMap);

3. Obfuscate this with Proguard (not obfuscaed code not crashing).
4. Invoke writeToParcel()

What is the expected output?
No crash

What do you see instead?

java.lang.RuntimeException: Parcelable encountered IOException writing 
serializable object (name = com.google.gson.internal.LinkedTreeMap)
            at android.os.Parcel.writeSerializable(Parcel.java:1388)
            at com.smartatoms.lametric.model.device.DeviceInfoApp.writeToParcel(DeviceInfoApp.java:115)
            at android.os.Parcel.writeParcelable(Parcel.java:1357)
            at android.os.Parcel.writeValue(Parcel.java:1262)
            at android.os.Parcel.writeList(Parcel.java:711)
            at android.os.Parcel.writeValue(Parcel.java:1284)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
            at android.os.Bundle.writeToParcel(Bundle.java:1096)
            at android.os.Parcel.writeBundle(Parcel.java:663)
            at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
            at android.os.Parcel.writeTypedArray(Parcel.java:1191)
            at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:381)
            at android.os.Parcel.writeParcelable(Parcel.java:1357)
            at android.os.Parcel.writeValue(Parcel.java:1262)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
            at android.os.Bundle.writeToParcel(Bundle.java:1096)
            at android.os.Parcel.writeBundle(Parcel.java:663)
            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2884)
            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3261)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.io.NotSerializableException: com.google.gson.internal.LinkedTreeMap$1
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
            at android.os.Parcel.writeSerializable(Parcel.java:1383)
            at 
com.smartatoms.lametric.model.device.DeviceInfoApp.writeToParcel(DeviceInfoApp.j
ava:115)
            at android.os.Parcel.writeParcelable(Parcel.java:1357)
            at android.os.Parcel.writeValue(Parcel.java:1262)
            at android.os.Parcel.writeList(Parcel.java:711)
            at android.os.Parcel.writeValue(Parcel.java:1284)
            at 
android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
            at 
android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
            at android.os.Bundle.writeToParcel(Bundle.java:1096)
            at android.os.Parcel.writeBundle(Parcel.java:663)
            at 
android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
            at android.os.Parcel.writeTypedArray(Parcel.java:1191)
            at 
android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:3
81)
            at android.os.Parcel.writeParcelable(Parcel.java:1357)
            at android.os.Parcel.writeValue(Parcel.java:1262)
            at 
android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
            at 
android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
            at android.os.Bundle.writeToParcel(Bundle.java:1096)
            at android.os.Parcel.writeBundle(Parcel.java:663)
            at 
android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2884
)
            at 
android.app.ActivityThread$StopInfo.run(ActivityThread.java:3261)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at 
android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Original issue reported on code.google.com by jaroslav...@lemberg.co.uk on 23 Jan 2015 at 1:45

GoogleCodeExporter commented 9 years ago
Hi. Today got this crash. Gson parsed my object like 
ArrayList<LinkedTreeMap<Node<String, String>. This object convert to byte[] 
like this :

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
objOutStream = new ObjectOutputStream(outStream);
objOutStream.writeObject(obj);

So, I got exception :

java.io.NotSerializableException: 
ru.tcsbank.wallet.gson.patched.internal.LinkedTreeMap$1
and could not write serialized object to byte array.

Original comment by d3M1d...@gmail.com on 2 Feb 2015 at 4:27

GoogleCodeExporter commented 9 years ago
And yes. I dont know about true reason, but if I dont have proguard, this code 
works perfect.

Original comment by d3M1d...@gmail.com on 2 Feb 2015 at 4:30

GoogleCodeExporter commented 9 years ago
I ran into this same issue and it appears to be a configuration issue with the 
default proguard file provided by google (in my case of android). You basically 
need to maintain additional methods when it comes to serializable classes. You 
can read section "Processing serializable classes" here for more info 
http://proguard.sourceforge.net/manual/examples.html#serializable

Long story short, add this to your proguard file so they aren't stripped out.
-keepclassmembers class * implements java.io.Serializable {
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

Original comment by spark...@gmail.com on 6 May 2015 at 2:21

GoogleCodeExporter commented 9 years ago
Haven't tested this but spark's comment seems reasonable. I guess this issue 
should be closed as user error.

Original comment by jaroslav...@lemberg.co.uk on 6 May 2015 at 10:43