mathwl / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

provide standard set of convenience methods to navigate JSON DOM tree #390

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

This request is to add a standard set of convenience methods to complex JSON 
element implementations. The "convenience" here is that the user doesn't have 
to re-type the result, and instead calls for the method that does the typing 
internally, or throws an exception, if the typing is not possible. It would be 
also a convenience, if the user could quickly tell if a given object is null.

JsonObject already implements methods that allow to retrieve a property as 
another json object, a json array, or a primitive. However, to retrieve a 
property as a terminal primitive type, the user has to call additional methods.

Since JsonElement supports returning values as the following terminal types:

primList = {BigDecimal, BigInteger, boolean, byte, char, double, float, int, 
long, Number, short, String}

I propose that the list of the convenience methods should shadow these 
primitive types, having JsonObject export getAsTYPE, where TYPE is each one 
from the primList.

The same is for the JsonArray. JsonArray current support retrieving primitive 
types of the first array of the element (which I actually believe is more 
dangerous than useful), but does not support returning n-th element as a 
specific type.

It would be as convenient, if the user could retrieve a specific array element 
as both complex json type, or a terminal primitive type. So, I propose adding 
the following methods to JsonArray (the only parameter is the 0-based index of 
the array element):

JsonArray getAsJsonArray(int);
JsonObject getAsJsonObject(int);
JsonPrimitive getAsJsonPrimitive(int);

and then also the set of getAsTYPE(int), where TYPE is one of primList.

To support checking for a NULL, method isNull(String) can be added JsonObject, 
and isNull(int) to JsonArray.

Thank you.

Original issue reported on code.google.com by pawel.ve...@gmail.com on 17 Dec 2011 at 2:23

GoogleCodeExporter commented 9 years ago
Personally I don't like this idea. The existing APIs are very normalized and 
that makes them easy to learn.

Original comment by limpbizkit on 29 Dec 2011 at 5:31

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 2 Sep 2012 at 9:48