Registering Maps and Collections in SpiderMonkey

Hey there,



I found another shortcoming, with fix, in SpiderMonkey.



A few days agao, I tried sendint a TreeSet through SpiderMonkey, but it didn’t work. Now I’ve fount why. SpiderMonkey uses the newInstance method to create an instance of the class to which the Serializer is registered. But for Maps and Collection, the serializer is registered to the interface, not the class so this doesn’t work.



I changed out this:

[java]

registerClass(Collection.class, new CollectionSerializer());

registerClass(Map.class, new MapSerializer());

[/java]



with this:

[java]

// all the Collection classes go here

registerClass(AbstractCollection.class, new CollectionSerializer());

registerClass(AbstractList.class, new CollectionSerializer());

registerClass(AbstractSet.class, new CollectionSerializer());

registerClass(ArrayList.class, new CollectionSerializer());

registerClass(BeanContextServicesSupport.class, new CollectionSerializer());

registerClass(BeanContextSupport.class, new CollectionSerializer());

registerClass(HashSet.class, new CollectionSerializer());

registerClass(LinkedHashSet.class, new CollectionSerializer());

registerClass(LinkedList.class, new CollectionSerializer());

registerClass(TreeSet.class, new CollectionSerializer());

registerClass(Vector.class, new CollectionSerializer());



// All the Map classes go here

registerClass(AbstractMap.class, new MapSerializer());

registerClass(Attributes.class, new MapSerializer());

registerClass(HashMap.class, new MapSerializer());

registerClass(Hashtable.class, new MapSerializer());

registerClass(IdentityHashMap.class, new MapSerializer());

registerClass(RenderingHints.class, new MapSerializer());

registerClass(TreeMap.class, new MapSerializer());

registerClass(WeakHashMap.class, new MapSerializer());

[/java]



and now it works like a charm. The trick is that you need to add all the various Collection and Map classes seperately, but then you CAN register them all to the same Serializer.

1 Like