You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jclouds.apache.org by Zack Shoylev <za...@RACKSPACE.COM> on 2014/05/05 22:40:27 UTC

GSON value names

When deserializing/serializing an object, the jclouds GSON supports multiple annotations for defining variable names.
Example: you have some_value in JSON but in a jclouds domain class it should be someValue.
We have (at least):

@SerializedName (a GSON-specific annotation, applied to member variable)
@Named (guice injection annotation, applied to member variable)
@ConstructorProperties (java beans annotation, and applied to the constructor of the domain object)

I am not actually sure if @ConstructorProperties works for serialization (it works when deserializing).

I have seen all these 3 used. My personal preference is to only use @SerializedName, and only where needed. Can I get some more opinions/insight/feedback on these annotations?

Thanks!
Zack

Re: GSON value names

Posted by Jeremy Daggett <je...@RACKSPACE.COM>.
Yes, thanks Zack!  I looked around for some examples of how @Named is used
in the codebase and Nova uses it in many of the domain classes like Flavor
[1] and Quota [2].

@ConstructorProperties is a standard JavaBeans annotation which is part of
the core JDK classes. My opinion is that we use it with the @Named
annotation to facilitate future portability.

/jd

[1] 
https://github.com/jclouds/jclouds/blob/master/apis/openstack-nova/src/main
/java/org/jclouds/openstack/nova/v2_0/domain/Flavor.java#L135

[2] 
https://github.com/jclouds/jclouds/blob/master/apis/openstack-nova/src/main
/java/org/jclouds/openstack/nova/v2_0/domain/Quota.java#L194




On 5/5/14, 2:06 PM, "Ignasi Barrera" <na...@apache.org> wrote:

>Thanks for bringing this up Zack!
>
>The SerializedName and Named annotatioms are used only to serialize
>objects
>[1] and are the only ones that can be used to configure the property
>names.
>I also prefer the SerializedName one.
>
>The ConstructorProperties one [2] is used only when deserializing.
>
>Another thing to take care of (I always try to do it) is to require, in
>the
>PRs, to specify the corresponding SerializedName annotations for each
>field
>that has a different serialized form, even if the domain object is not
>going to be serialized. That will make the whole model consistent and
>easier to evolve the apis.
>
>
>I.
>
>[1]
>https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclo
>uds/json/config/GsonModule.java#L99-103
>[2]
>https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclo
>uds/json/config/GsonModule.java#L123-127
>El 05/05/2014 22:40, "Zack Shoylev" <za...@rackspace.com> escribió:
>
>> When deserializing/serializing an object, the jclouds GSON supports
>> multiple annotations for defining variable names.
>> Example: you have some_value in JSON but in a jclouds domain class it
>> should be someValue.
>> We have (at least):
>>
>> @SerializedName (a GSON-specific annotation, applied to member variable)
>> @Named (guice injection annotation, applied to member variable)
>> @ConstructorProperties (java beans annotation, and applied to the
>> constructor of the domain object)
>>
>> I am not actually sure if @ConstructorProperties works for serialization
>> (it works when deserializing).
>>
>> I have seen all these 3 used. My personal preference is to only use
>> @SerializedName, and only where needed. Can I get some more
>> opinions/insight/feedback on these annotations?
>>
>> Thanks!
>> Zack


Re: GSON value names

Posted by Ignasi Barrera <na...@apache.org>.
Thanks for bringing this up Zack!

The SerializedName and Named annotatioms are used only to serialize objects
[1] and are the only ones that can be used to configure the property names.
I also prefer the SerializedName one.

The ConstructorProperties one [2] is used only when deserializing.

Another thing to take care of (I always try to do it) is to require, in the
PRs, to specify the corresponding SerializedName annotations for each field
that has a different serialized form, even if the domain object is not
going to be serialized. That will make the whole model consistent and
easier to evolve the apis.


I.

[1]
https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclouds/json/config/GsonModule.java#L99-103
[2]
https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclouds/json/config/GsonModule.java#L123-127
El 05/05/2014 22:40, "Zack Shoylev" <za...@rackspace.com> escribió:

> When deserializing/serializing an object, the jclouds GSON supports
> multiple annotations for defining variable names.
> Example: you have some_value in JSON but in a jclouds domain class it
> should be someValue.
> We have (at least):
>
> @SerializedName (a GSON-specific annotation, applied to member variable)
> @Named (guice injection annotation, applied to member variable)
> @ConstructorProperties (java beans annotation, and applied to the
> constructor of the domain object)
>
> I am not actually sure if @ConstructorProperties works for serialization
> (it works when deserializing).
>
> I have seen all these 3 used. My personal preference is to only use
> @SerializedName, and only where needed. Can I get some more
> opinions/insight/feedback on these annotations?
>
> Thanks!
> Zack