[
https://issues.apache.org/jira/browse/SLING-12269?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17828427#comment-17828427
]
Remo Liechti edited comment on SLING-12269 at 3/19/24 3:58 PM:
---------------------------------------------------------------
I think the behaviour is changed for all methods.
I.e. for getLong:
*Old:*
{{{}public long getLong(String key) throws JSONException{}}}{{{}{ {}}}
{{ Object o = get(key); }}
{{ return o instanceof Number ? }}
{{ ((Number)o).longValue() : }}
{{ Long.valueOf((String)o).longValue(); }}
{{}}}
+Cases+
# Object o is Number: returns long
# Object o is String of format Long: returns long
# Object o is String with invalid format: throws a NumberFormatException
# Object o is no String: throws ClassCastException
*New:*
{{public long getLong(String key) throws JSONException {}}
{{ final Object object = this.get(key);}}
{{{} if (object instanceof Number){}}}{{{}{ {}}}
{{ return ((Number) object).longValue(); }}
{{ }}}{{ }}
{{{} try{}}}{{{}{ {}}}
{{ return Long.parseLong(object.toString()); }}
{{{} }{}}}{{{}catch (Exception e){}}}{{{}{ {}}}
{{ throw wrongValueFormatException(key, "long", object, e); }}}{{
}}
{{}}}
+Cases+
# Object o is Number: returns long
# Object o is any type, does toString() with valid format: returns long
# Object o is any type, does toString() with invalid format: throws a
JsonException
# throws no more ClassCastException nor NumberFormatException
was (Author: JIRAUSER304615):
I think the behaviour is changed for all methods.
I.e. for getLong:
*Old:*
public long getLong(String key) throws JSONException {
Object o = get(key);
return o instanceof Number ?
((Number)o).longValue() :
Long.valueOf((String)o).longValue();
}
+Cases+
# Object o is Number: returns long
# Object o is String of format Long: returns long
# Object o is String with invalid format: throws a NumberFormatException
# Object o is no String: throws ClassCastException
*New:*
public long getLong(String key) throws JSONException {
final Object object = this.get(key);
if (object instanceof Number) {
return ((Number) object).longValue();
}
try {
return Long.parseLong(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(key, "long", object, e);
}
}
+Cases+
# Object o is Number: returns long
# Object o is any type, does toString() with valid format: returns long
# Object o is any type, does toString() with invalid format: throws a
JsonException
# throws no more ClassCastException nor NumberFormatException
> Changed behaviour in commons json when used as a dependency
> -----------------------------------------------------------
>
> Key: SLING-12269
> URL: https://issues.apache.org/jira/browse/SLING-12269
> Project: Sling
> Issue Type: Bug
> Components: Commons
> Reporter: Remo Liechti
> Priority: Blocker
>
> With the new internal release 2.0.22, there seems to be a different behaviour
> when it comes to get values from JSONObject.
> The new version checks for types and throws an exception, as of the old
> version simply called toString() on any object found.
> *Old:*
> {{public String getString(String key) throws JSONException {}}
> {{ return get(key).toString();}}
> {{}}}
> *New:*
> {{public String getString(String key) throws JSONException {}}
> {{ Objectobject=this.get(key);}}
> {{ if (objectinstanceofString) {}}
> {{ return (String) object;}}
> }
> {{ throwwrongValueFormatException(key, "string", object, null);}}
> {{}}}
>
> Same is true for all other types, such as getInt, getLong etc.
> There might be more such small differences in behaviour.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)