Skip to content

Commit

Permalink
RuedigerMoeller#332 JSON deserialisation of special collections
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-smith-preservica authored Apr 13, 2023
1 parent e8da559 commit 96c5f65
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo ser
if ( objectClass == LinkedList.class ) {
res = new LinkedList();
} else {
if ( AbstractList.class.isAssignableFrom(objectClass) && objectClass.getName().startsWith( "java.util.Arrays" ) ) {
// some collections produced by JDK are not properly instantiable (e.g. Arrays.ArrayList), fall back to arraylist then
res = new ArrayList<>();
} else {
res = objectClass.newInstance();
}
// some collections produced by JDK are not properly instantiable (e.g. Arrays.ArrayList), fall back to 'normal' version of collection then
String className = objectClass.getClazz().getName();
boolean specialCollection = className.startsWith("java.util.Collections$") || className.startsWith("java.util.Arrays$");
Class deserializeAs = specialCollection ? (
List.class.isAssignableFrom(objectClass) ? ArrayList.class :
Set.class.isAssignableFrom(objectClass) ? LinkedHashSet.class : // preserve the order; we don't know if the class we passed did that
Map.class.isAssignableFrom(objectClass) ? LinkedHashMap.class :
objectClass
) : objectClass;
res = objectClass.newInstance();
}
in.registerObject(res, streamPosition,serializationInfo, referencee);
Collection col = (Collection)res;
Expand Down

0 comments on commit 96c5f65

Please sign in to comment.