Gson 1.7 Released

5,520 views
Skip to first unread message

Joel

unread,
Apr 12, 2011, 7:25:47 PM4/12/11
to google-gson
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

Kyle

unread,
Apr 12, 2011, 8:48:58 PM4/12/11
to google-gson
Haha you just fixed my deserialization error! woot :D <33

On Apr 12, 4:25 pm, Joel <joel.lei...@gmail.com> wrote:
> I am excited to announce the Gson 1.7 is ready for prime-time. Please
> go tohttp://code.google.com/p/google-gson/downloads/listto 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/ma...

inde...@gmail.com

unread,
Apr 13, 2011, 2:04:23 AM4/13/11
to google-gson
For the Maven2 users, Gson 1.7 is now also available in the Maven
Central repository.
See http://repo2.maven.org/maven2/com/google/code/gson/gson/1.7/

inde...@gmail.com

unread,
Apr 13, 2011, 2:26:03 AM4/13/11
to google-gson
I just realized that the Gson 1.7 that we pushed to Maven Central is
corrupted (the jar file contains the javadocs instead of classes).
So, Gson 1.7 is unusable from Maven Central. We will be addressing
this problem with-in the next few days.
Apologies for any inconvenience this caused.

Inder


On Apr 12, 11:04 pm, "inder...@gmail.com" <inder...@gmail.com> wrote:
> For the Maven2 users, Gson 1.7 is now also available in the Maven
> Central repository.
> Seehttp://repo2.maven.org/maven2/com/google/code/gson/gson/1.7/

Lin Dapeng

unread,
Apr 13, 2011, 4:14:08 AM4/13/11
to googl...@googlegroups.com
great for "Hierarchical Type Adapters" to be "public". Thanks.


--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.


David Dunwoody

unread,
Apr 13, 2011, 11:57:44 AM4/13/11
to google-gson
Thanks for this release - the hierarchical type adapters have allowed
me to reduce some of my domain implementations back to package
visibility and have cut down the amount of config I need to write
significantly.

Regards,

David

michael.hixson

unread,
Apr 21, 2011, 1:16:09 AM4/21/11
to google-gson
I'm surprised by the "Complex Map Key serialization and
deserialization" feature. Doesn't that mean Gson.toJson can now
generate invalid JSON?

michael.hixson

unread,
Apr 22, 2011, 3:24:46 AM4/22/11
to google-gson
Oops, I'm dumb. You make an array of arrays, not a map with non-
string keys.

On Apr 20, 10:16 pm, "michael.hixson" <michael.hix...@gmail.com>
wrote:

Chris Brett

unread,
Jul 22, 2011, 7:46:12 AM7/22/11
to googl...@googlegroups.com
Regarding "object allocation with no default constructor", it appears that variables assigned a value outside of the constructor (in the variable declaration) will not be assigned.

For example:

public class SomeClass{
    private Collection<String> collection = new ArrayList<String>();
    private String assignedInConstructor;
   
    public SomeClass(String string){
        assignedInConstructor = string;
    }
}

In this example, "collection" will not have been initialised to an empty ArrayList when Gson creates it. This means that any functions that assume (with good reason) that it will be not null will be sadly disappointed.

Why is this? Is it intentional?

Is there a way to get it to behave sensibly without adding a private no-args constructor that does absolutely nothing? I have tested that and it fixes the issue but looks remarkably pointless and is liable to be deleted by other devs as they will assume that it doesn't do anything. I would also rather not have to define an InstanceCreator for every class that doesn't have a no-args constructor but does set some variables outside of the constructor.

inde...@gmail.com

unread,
Jul 22, 2011, 11:28:14 AM7/22/11
to googl...@googlegroups.com


On Friday, July 22, 2011 4:46:12 AM UTC-7, Chris Brett wrote:
Regarding "object allocation with no default constructor", it appears that variables assigned a value outside of the constructor (in the variable declaration) will not be assigned.

For example:

public class SomeClass{
    private Collection<String> collection = new ArrayList<String>();
    private String assignedInConstructor;
   
    public SomeClass(String string){
        assignedInConstructor = string;
    }
}

In this example, "collection" will not have been initialised to an empty ArrayList when Gson creates it. This means that any functions that assume (with good reason) that it will be not null will be sadly disappointed.

Why is this? Is it intentional?

This is intentional and how Java works. The variable assignments while declaration are syntactic conveniences. Gson will probably never be able to figure out how to invoke those default assignments. Most of those will be overridden anyway based on the input Json stream. 

 
Is there a way to get it to behave sensibly without adding a private no-args constructor that does absolutely nothing? I have tested that and it fixes the issue but looks remarkably pointless and is liable to be deleted by other devs as they will assume that it doesn't do anything. I would also rather not have to define an InstanceCreator for every class that doesn't have a no-args constructor but does set some variables outside of the constructor.

The private no-args constructor is doing something in your case: it is assigning fields to some sensible default values. I dont think there is another way. 
Here is a convention I use while declaring such no-args constructor:

// Needed by Gson
@SuppressWarnings("unused")
private MyObject() { 
}

Also note that Gson can not work without a no-args constructor for Google App Engine (and I think other Sun JDK based) deployments. This is because we used some tricks in Java serialization mechanism to construct an object that doesn't have a no-args constructor. We knew how to do that for Android but haven't been able to figure out a way for J2SE. So, in a way, having such no-args constructors is a safer choice to ensure the objects will be deserializable with Gson.

Inder
Reply all
Reply to author
Forward
0 new messages