I am excited to announce the Gson 1.7 is ready for prime-time. Please
go to
http://code.google.com/p/google-gson/downloads/list to download
this new version.
Below is a short description of some of the more significant features
that were added as part of this release.
1. Object allocation with no default constructor (my favourite):
It is no longer necessary to put in a default constructor (or instance
creator) that passes in null values or values to satisfy the
constructor preconditions.
private static class ObjectNoDefaultConstructor {
private final String stringValue;
private final int intValue;
@SuppressWarnings("unused")
public ObjectNoDefaultConstructor(String stringValue, int
intValue) {
this.stringValue = stringValue;
this.intValue = intValue;
}
}
2. Hierarchical Type Adapters
You can now create a type adapter that will be applied to all of its
child classes. This works well for special Enums that contain method/
data (anonymous classes). Here is a real example for doing protocol
buffers.
http://code.google.com/p/google-gson/source/browse/trunk/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java
To register it as a hierarchical type adapter, you must register is as
follows:
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(GeneratedMessage.class, new
ProtoTypeAdapter())
.create();
3. Complex Map Key serialization and deserialization:
New support for complex map keys. If the key to a Java Map is a
complex object (i.e. a non JSON primitive) then Gson will generate an
array of arrays (or can be seen as an array of key-value pairs).
For example:
public static void main(String[] args) {
Gson gson = new GsonBuilder()
.enableComplexMapKeySerialization()
.create();
Map<Point, String> original = new LinkedHashMap<Point, String>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));}
}
The JSON output would look as follows:
[
[ { "x": 5, "y": 6 }, "a" ],
[ { "x": 8, "y": 8 }, "b" ]
]
4. Serialization and Deserialization specific exclusion strategies:
Provides the abillity to run an exclusion strategy on either
serialization or deserialization only. This allows for reuse of the
same Gson object for both serialization and deserialization if you
required this kind of feature.
Gson gson = new GsonBuilder()
.addSerializationExclusionStrategy(new
SerializeOnlyExclusionStrategy())
.addDeserializationExclusionStrategy(new
DeserializeOnlyExclusionStrategy())
5. Allow concrete data structure fields without type adapters:
Abillity to specify a field as a concrete "Collection" type, such as
ArrayList. In the past, this would actually throw an error.
class ArrayListWrapper {
private ArrayList<String> arrayList;
}
6. Major performance enhancement:
Added some new caches to reduce the amount redundant reflection calls
that occur when working with the same data objects over-and-over.
We hope you enjoy the new release and as always, your feedback is very
much appreciated.
Happy coding,
Joel