henriqueolliveira / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

Support JSON null #189

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

http://tools.ietf.org/html/rfc4627

Java environments (e.g. Java 6, Android 2.3, App Engine 1.4.2, or All)?

All.

Please describe the feature requested.

How should we handle this JSON:

{"value": null}

Suppose we have this JSON data class:

  public static class A {
    @Key
    public String value;
  }

Currently null is essentially treated the same as undefined, setting field 
value to Java null.   The problem is that then we serialize it like this:

{}

This feature is to allow any field to have a new "magic" null value which is 
treated differently from a Java null.  In this way, the data classes stay 
exactly the same.  In practice, it means adding the following constants/methods 
to the library:

public class Data {
  public static final Boolean NULL_BOOLEAN;
  public static final String NULL_STRING;
  public static final Integer NULL_INTEGER;
  public static final Double NULL_DOUBLE;
...
  public static <T> T nullOf(Class<?> objClass);
  public static boolean isNull(Object object);
}

Thus, for any class, there is a specific value that is assumed to be the "null" 
value for its class.  There is also a method to check if a particular object is 
the "null" value for its class.  In nullOf(Class), if there is no "null" value 
declared for a class, it will create a new instance of the object by calling 
its public default constructor and caching it.

So now, for our example above, if we wanted to check if a parsed value is null, 
we write code as follows:

if (Data.isNull(a.value)) {
  System.out.println("value is null");
} else {
  System.out.println("value is " + a.value);
}

And if we wanted to set value to JSON null for serialization, we write this:

A a = new A();
a.value = Data.NULL_STRING;

Or alternatively:

a.value = Data.nullOf(String.class);

One interesting special case: enums.  There is no way to create a new instance 
of an enum in nullOf(Class).  We will instead look for the @NullValue 
annotation.  For example:

public enum E {
  @Value("value") VALUE,
  @NullValue NULL_VALUE
}

public class A {
  @Key E e;
}

If field e has JSON value "value", it will map to E.VALUE, but if e has JSON 
value null, it will map to E.NULL_VALUE.

Original issue reported on code.google.com by yan...@google.com on 25 Apr 2011 at 11:38

GoogleCodeExporter commented 9 years ago
Is it important to document the tri-stateness of this?  That is, a plain old 
null means not present at all. A returned object could contain both nulls and 
Data.Nulls.  So we end up with

if (a == null) {
   System.out.println("no value present");
} else if (Data.isNull(a.value)) {
  System.out.println("value is null");
} else {
  System.out.println("value is " + a.value);
}

Original comment by ai...@google.com on 25 Apr 2011 at 9:44

GoogleCodeExporter commented 9 years ago
Yes, good point!  Data.isNull(null) is false for this reason.

Original comment by yan...@google.com on 26 Apr 2011 at 11:16

GoogleCodeExporter commented 9 years ago
http://codereview.appspot.com/4437045/

Original comment by yan...@google.com on 27 Apr 2011 at 1:04

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 2 May 2011 at 5:57