fyhack / google-gson

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

Handle bitsets correctly #207

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
See 
http://groups.google.com/group/google-gson/browse_thread/thread/535892ffcf691aa
for details on the bug

Original issue reported on code.google.com by inder123 on 27 Apr 2010 at 4:19

GoogleCodeExporter commented 9 years ago
There's many different ways to map a bit set to JSON...

Suppose we're given the bit set 100101100. Here are some options:
  bit array: [1,0,0,1,0,1,1,0,0]
  bit string: "100101100"
  hex array: ["01","2C"]
  hex string: "012C"
Hex is compact but it cannot preserve the length attribute of the bit set.

I'd prefer that we leave this up to the application developer, who will be able 
to make an informed decision. They'll need to weigh the tradeoffs between 
encoded length and retention of the bit set length.

Original comment by limpbizkit on 3 Nov 2010 at 4:27

GoogleCodeExporter commented 9 years ago
(The application developer would be expected to create a type adapter for the 
bit set, and register that in their GsonBuilder)

Original comment by limpbizkit on 3 Nov 2010 at 4:28

GoogleCodeExporter commented 9 years ago
We should consider writing a sample type adapter for bitset in our guide. 
http://sites.google.com/site/gson/gson-user-guide

Original comment by inder123 on 3 Nov 2010 at 7:53

GoogleCodeExporter commented 9 years ago
Most of the BitSet problems are occurred by using Gson in Google AppEngine. I 
also got the same problem. In most cases, I think most of the people don't use 
and don't care about it, because we can't use this kind of type in our value 
object.

Here is a snapshot of this exception thrown in Google Appengine.

java.lang.SecurityException: java.lang.IllegalAccessException: Reflection is 
not allowed on private static final int java.util.BitSet.ADDRESS_BITS_PER_WORD
    at com.google.appengine.runtime.Request.process-56ead41f7d18d602(Request.java)
    at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:29)
    at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:149)

People can implement a default BitSetSerializer like this:

import java.lang.reflect.Type;
import java.util.BitSet;

import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class BitSetSerializer implements JsonSerializer<BitSet> {

  @Override
  public JsonElement serialize(BitSet src, Type arg1, JsonSerializationContext arg2) {
    return null; //I dont care about it !!!
  }

}

Hope this can help you work around this problem in AppEngine

Original comment by matt.jiang on 13 Mar 2011 at 1:25

GoogleCodeExporter commented 9 years ago
This is fixed and will be in the next release.

Original comment by joel.leitch@gmail.com on 15 Apr 2011 at 4:10

GoogleCodeExporter commented 9 years ago
Thanks for posting that Matt; I was running in the exact same problem and that 
did the trick.  I just created a new gson builder with that BitSetSerializer:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(java.util.BitSet.class, new BitSetSerializer());

Original comment by ad...@cosanta.com on 14 May 2011 at 2:34