> + */
> +package org.jclouds.openstack.neutron.v2.domain;
> +
> +import com.google.common.base.Objects;
> +import com.google.gson.annotations.SerializedName;
> +
> +/**
> + */
> +public class AddressPair {
> +
> +   @SerializedName("mac_address")
> +   protected final String macAddress;
> +   @SerializedName("ip_address")
> +   protected final String ipAddress;
> +
> +   protected AddressPair(String macAddress, String ipAddress) {

Mystery solved :)

What made me doubt is the fact that to deserialize the JSON, if there is no 
default constructor, the one with parameters must be used and Gson must know in 
which order the values must be passed. It is not an option to pass null values 
and set them afterward by reflection, because the constructor can have null 
check validations.

I've debugged the code, and Gson, when the `@ConstructorProperties` annotation 
is not found, picks an internal implementation that ends up using their 
[UnsafeAllocator](https://code.google.com/p/google-gson/source/browse/tags/gson-2.2.4/src/main/java/com/google/gson/internal/UnsafeAllocator.java),
 which uses `sun.misc.Unsafe` to directly allocate a new instance of the 
deserialization class in memory without calling the constructor. After this, it 
sets the field with reflection, according to the field annotations.

Although this works, it has several issues: the constructor might not be used 
at all and all validations in there may not be triggered, and also using 
`sun.misc.Unsafe` might not work in all implementations of the JVM. 

I think we should try to be as portable as possible, so I'd recommend adding 
the annotation, now we know more about the deserialization internals. WDYT?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-labs-openstack/pull/111/files#r14213281

Reply via email to