Skip to main content

[jsr353-experts] Re: [json-processing-spec users] Re: Re: JSON_PROCESSING_SPEC-45: JsonObject/JsonArray accessor methods that take default values

  • From: Jitendra Kotamraju < >
  • To:
  • Subject: [jsr353-experts] Re: [json-processing-spec users] Re: Re: JSON_PROCESSING_SPEC-45: JsonObject/JsonArray accessor methods that take default values
  • Date: Mon, 28 Jan 2013 17:17:01 -0800

On 01/24/2013 02:07 PM, Jitendra Kotamraju wrote:
On 01/23/2013 06:53 AM, Jonathan Fuerth wrote:
On 2013-01-22, at 10:05 PM, Jitendra Kotamraju wrote:

The issue asks for additional accessor methods that take default values. Here is the initial proposal.

Add the following methods:

// If there is mapping and the mapping is JsonString, then JsonString#getValue()
// else default value
String getStringValue(String name, String defaultValue)

// If there is mapping and the mapping is JsonNumber, then JsonNumber#getIntValue()
// else default value
int getIntValue(String name, int defaultValue)

// If there is mapping and the mapping is JsonValue.TRUE/JsonValue.FALSE, then mapped value
// else default value
boolean getBooleanValue(String name, boolean defaultValue)
Yes! The above methods go a long way to addressing the usability problems I've encountered with using the API to walk through JSON data structures and pick out values. They help to paper over the type inconsistency problems in juggling JsonValue.NULL, JsonValue.TRUE, and JsonValue.FALSE, all of which are not assignment compatible with each other or any other JSON type. I'd still like to see the root cause fixed, but this is an effective remedy.
I would like to hear from other people. So that I can resolve this issue soon.
I haven't received any feedback, I will commit the above changes

Jitu

I'm still uneasy about singling out int as the official common number type in JSON. Generally, numbers are considered to be double precision floating point values, because JSON is both derived from and commonly used with JavaScript. All numbers in JavaScript are doubles. If we're to single out any particular representation for JsonNumber, why shouldn't it be double?
We had that discussion as I indicated in the other thread.

Or what about not singling out any particular representation, and instead provide:

// If there is mapping and the mapping is JsonNumber, then that number is returned;
// else default value
JsonNumber getNumberValue(String name, JsonNumber defaultValue)

I know this is more verbose to use, but at least it doesn't bias toward a non-canonical number representation.
This is a suspect. We are primarily providing accessor methods for non wrapper types.

If required, we should provide a generic method for wrappertype with default value.
<T extends JsonValue> T getValue(String name, T defaultValue)
But I think we need to use caution and don't have to put everything in the first version.

The other alternative would be to provide convenience accessors for all possible numeric representations. My knee-jerk reaction is to say no to this idea because there are too many options, and it would make JsonValue quite busy.

// If there is no mapping, throw NPE
// If it doesn't map to JsonValue.TRUE/Json.VALUE, throw CCE
// return true for JsonValue.TRUE, false for JsonValue.FALSE
boolean getBooleanValue(String name)
Sure, this also helps mask the strange situation that JsonValue.TRUE has a different type from JsonValue.FALSE.


One final idea to ponder for all of the above: what about removing "Value" from the method names? I think the meaning of code written against the API would still be quite clear (perhaps clearer) without having "Value" repeated everywhere.
I was also thinking for some methods, definitely for getValue(String name, Class<T>). If others are OK, we can do that.

Jitu

-Jonathan




[jsr353-experts] JSON_PROCESSING_SPEC-45: JsonObject/JsonArray accessor methods that take default values

Jitendra Kotamraju 01/23/2013

[jsr353-experts] Re: [json-processing-spec users] JSON_PROCESSING_SPEC-45: JsonObject/JsonArray accessor methods that take default values

Jonathan Fuerth 01/23/2013

[jsr353-experts] Re: [json-processing-spec users] Re: JSON_PROCESSING_SPEC-45: JsonObject/JsonArray accessor methods that take default values

Jitendra Kotamraju 01/24/2013

[jsr353-experts] Re: [json-processing-spec users] Re: Re: JSON_PROCESSING_SPEC-45: JsonObject/JsonArray accessor methods that take default values

Jitendra Kotamraju 01/29/2013
 
 
Close
loading
Please Confirm
Close