songchuanyuan66 / concurrentlinkedhashmap

Automatically exported from code.google.com/p/concurrentlinkedhashmap
Apache License 2.0
0 stars 1 forks source link

gson deserialize fail #39

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
i use the concurrentlinkedhashmap to store data and serialize to String on 
disk。

when deserialize the string with gson, the exception cause:

01-17 17:34:35.647: W/System.err(8870): java.lang.IllegalArgumentException: 
invalid value for field
01-17 17:34:35.647: W/System.err(8870):     at 
java.lang.reflect.Field.setField(Native Method)
01-17 17:34:35.647: W/System.err(8870):     at 
java.lang.reflect.Field.set(Field.java:588)
01-17 17:34:35.647: W/System.err(8870):     at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveType
AdapterFactory.java:95)
01-17 17:34:35.647: W/System.err(8870):     at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflecti
veTypeAdapterFactory.java:172)
01-17 17:34:35.647: W/System.err(8870):     at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveType
AdapterFactory.java:93)
01-17 17:34:35.647: W/System.err(8870):     at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflecti
veTypeAdapterFactory.java:172)

the gson(newest) treat the data(deserialize) as 
“com.google.gson.internal.LinkedTreeMap” not the concurrentlinkedhashmap,
maybe the reson is that concurrentlinkedhashmap has no construct func, or else?

the class i want to serialize/deserialize :

public class MediaMapping {
    public MediaMapping(){
        map = new ConcurrentLinkedHashMap.Builder<String, MediaValue>()
                .maximumWeightedCapacity(Constant.MEDIA_CACHE_LIMIT) // 1GB, internal storage, not memory
                .weigher(memoryUsageWeigher)
                .listener(listener)
                .build();
         }
  ...
}

Original issue reported on code.google.com by zhangwei...@gmail.com on 17 Jan 2014 at 10:07

GoogleCodeExporter commented 9 years ago
I'm sorry for not being available to answer your concern previously. I'm sure 
you already found a workaround.

Gson does not deserialize back to the same type, but rather generic types that 
it thinks comply to the same interface (Map in this case). If it had worked 
silently that could have been very problematic for you, as you would have had 
an unbounded non-thread-safe map. In this scenario, a custom TypeAdapter would 
be the preferred solution.

Original comment by Ben.Manes@gmail.com on 15 Dec 2014 at 8:27