Skip to content

Commit 2559114

Browse files
committed
Merge branch 'master' into fix-similar-check
2 parents c6089e5 + bb048e3 commit 2559114

6 files changed

Lines changed: 40 additions & 21 deletions

File tree

src/main/java/org/json/JSONObject.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,18 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
11591159
* to convert.
11601160
*/
11611161
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
1162+
return objectToBigDecimal(val, defaultValue, true);
1163+
}
1164+
1165+
/**
1166+
* @param val value to convert
1167+
* @param defaultValue default value to return is the conversion doesn't work or is null.
1168+
* @param exact When <code>true</code>, then {@link Double} and {@link Float} values will be converted exactly.
1169+
* When <code>false</code>, they will be converted to {@link String} values before converting to {@link BigDecimal}.
1170+
* @return BigDecimal conversion of the original value, or the defaultValue if unable
1171+
* to convert.
1172+
*/
1173+
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolean exact) {
11621174
if (NULL.equals(val)) {
11631175
return defaultValue;
11641176
}
@@ -1172,7 +1184,14 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
11721184
if (!numberIsFinite((Number)val)) {
11731185
return defaultValue;
11741186
}
1175-
return new BigDecimal(((Number) val).doubleValue());
1187+
if (exact) {
1188+
return new BigDecimal(((Number)val).doubleValue());
1189+
}else {
1190+
// use the string constructor so that we maintain "nice" values for doubles and floats
1191+
// the double constructor will translate doubles to "exact" values instead of the likely
1192+
// intended representation
1193+
return new BigDecimal(val.toString());
1194+
}
11761195
}
11771196
if (val instanceof Long || val instanceof Integer
11781197
|| val instanceof Short || val instanceof Byte){
@@ -1639,9 +1658,6 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
16391658
* implementations and interfaces has the annotation. Returns the depth of the
16401659
* annotation in the hierarchy.
16411660
*
1642-
* @param <A>
1643-
* type of the annotation
1644-
*
16451661
* @param m
16461662
* method to check
16471663
* @param annotationClass
@@ -2137,8 +2153,8 @@ static boolean isNumberSimilar(Number l, Number r) {
21372153
// BigDecimal should be able to handle all of our number types that we support through
21382154
// documentation. Convert to BigDecimal first, then use the Compare method to
21392155
// decide equality.
2140-
final BigDecimal lBigDecimal = objectToBigDecimal(l, null);
2141-
final BigDecimal rBigDecimal = objectToBigDecimal(r, null);
2156+
final BigDecimal lBigDecimal = objectToBigDecimal(l, null, false);
2157+
final BigDecimal rBigDecimal = objectToBigDecimal(r, null, false);
21422158
if (lBigDecimal == null || rBigDecimal == null) {
21432159
return false;
21442160
}

src/main/java/org/json/JSONPointer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public JSONPointer(List<String> refTokens) {
188188
}
189189

190190
/**
191-
* @see https://tools.ietf.org/html/rfc6901#section-3
191+
* @see <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2F%3C%2Fspan%3Ehttps%3A%2F%2Ftools.ietf.org%2Fhtml%2Frfc6901%23section-3%3Cspan+class%3D"x x-first x-last">">rfc6901 section 3</a>
192192
*/
193193
private static String unescape(String token) {
194194
return token.replace("~1", "/").replace("~0", "~");
@@ -268,7 +268,7 @@ public String toString() {
268268
* @param token the JSONPointer segment value to be escaped
269269
* @return the escaped value for the token
270270
*
271-
* @see https://tools.ietf.org/html/rfc6901#section-3
271+
* @see <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2F%3C%2Fspan%3Ehttps%3A%2F%2Ftools.ietf.org%2Fhtml%2Frfc6901%23section-3%3Cspan+class%3D"x x-first x-last">">rfc6901 section 3</a>
272272
*/
273273
private static String escape(String token) {
274274
return token.replace("~", "~0")

src/main/java/org/json/XMLParserConfiguration.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public XMLParserConfiguration (final boolean keepStrings) {
9494
* Configure the parser string processing to try and convert XML values to JSON values and
9595
* use the passed CDATA Tag Name the processing value. Pass <code>null</code> to
9696
* disable CDATA processing
97-
* @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
97+
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
9898
* to use that value as the JSONObject key name to process as CDATA.
9999
* @deprecated This constructor has been deprecated in favor of using the new builder
100100
* pattern for the configuration.
@@ -109,7 +109,7 @@ public XMLParserConfiguration (final String cDataTagName) {
109109
* Configure the parser to use custom settings.
110110
* @param keepStrings <code>true</code> to parse all values as string.
111111
* <code>false</code> to try and convert XML string values into a JSON value.
112-
* @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
112+
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
113113
* to use that value as the JSONObject key name to process as CDATA.
114114
* @deprecated This constructor has been deprecated in favor of using the new builder
115115
* pattern for the configuration.
@@ -182,7 +182,7 @@ protected XMLParserConfiguration clone() {
182182
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
183183
* they should try to be guessed into JSON values (numeric, boolean, string)
184184
*
185-
* @return The {@link #keepStrings} configuration value.
185+
* @return The <code>keepStrings</code> configuration value.
186186
*/
187187
public boolean isKeepStrings() {
188188
return this.keepStrings;
@@ -193,7 +193,7 @@ public boolean isKeepStrings() {
193193
* they should try to be guessed into JSON values (numeric, boolean, string)
194194
*
195195
* @param newVal
196-
* new value to use for the {@link #keepStrings} configuration option.
196+
* new value to use for the <code>keepStrings</code> configuration option.
197197
*
198198
* @return The existing configuration will not be modified. A new configuration is returned.
199199
*/
@@ -208,7 +208,7 @@ public XMLParserConfiguration withKeepStrings(final boolean newVal) {
208208
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
209209
* processing.
210210
*
211-
* @return The {@link #cDataTagName} configuration value.
211+
* @return The <code>cDataTagName</code> configuration value.
212212
*/
213213
public String getcDataTagName() {
214214
return this.cDataTagName;
@@ -220,7 +220,7 @@ public String getcDataTagName() {
220220
* processing.
221221
*
222222
* @param newVal
223-
* new value to use for the {@link #cDataTagName} configuration option.
223+
* new value to use for the <code>cDataTagName</code> configuration option.
224224
*
225225
* @return The existing configuration will not be modified. A new configuration is returned.
226226
*/
@@ -235,7 +235,7 @@ public XMLParserConfiguration withcDataTagName(final String newVal) {
235235
* should be kept as attribute(<code>false</code>), or they should be converted to
236236
* <code>null</code>(<code>true</code>)
237237
*
238-
* @return The {@link #convertNilAttributeToNull} configuration value.
238+
* @return The <code>convertNilAttributeToNull</code> configuration value.
239239
*/
240240
public boolean isConvertNilAttributeToNull() {
241241
return this.convertNilAttributeToNull;
@@ -247,7 +247,7 @@ public boolean isConvertNilAttributeToNull() {
247247
* <code>null</code>(<code>true</code>)
248248
*
249249
* @param newVal
250-
* new value to use for the {@link #convertNilAttributeToNull} configuration option.
250+
* new value to use for the <code>convertNilAttributeToNull</code> configuration option.
251251
*
252252
* @return The existing configuration will not be modified. A new configuration is returned.
253253
*/
@@ -262,7 +262,7 @@ public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal
262262
* will be converted to target type defined to client in this configuration
263263
* {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
264264
* xsi:type="integer" as integer, xsi:type="string" as string
265-
* @return {@link #xsiTypeMap} unmodifiable configuration map.
265+
* @return <code>xsiTypeMap</code> unmodifiable configuration map.
266266
*/
267267
public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
268268
return this.xsiTypeMap;

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public void verifySimilar() {
131131
assertTrue("obj1-obj4 Should eval to true", obj1.similar(obj4));
132132
assertFalse("obj1-obj5 Should eval to false", obj1.similar(obj5));
133133

134+
// verify that a double and big decimal are "similar"
135+
assertTrue("should eval to true",new JSONObject().put("a",1.1d).similar(new JSONObject("{\"a\":1.1}")));
136+
134137
}
135138

136139
@Test
@@ -945,7 +948,7 @@ public void stringToValueNumbersTest() {
945948
assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double);
946949
assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double);
947950
assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String);
948-
assertTrue( "0.2 should be a Double!",
951+
assertTrue( "0.2 should be a BigDecimal!",
949952
JSONObject.stringToValue( "0.2" ) instanceof BigDecimal );
950953
assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!",
951954
JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof BigDecimal );

src/test/java/org/json/junit/JSONPointerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void tildeEscaping() {
120120
/**
121121
* We pass backslashes as-is
122122
*
123-
* @see https://tools.ietf.org/html/rfc6901#section-3
123+
* @see <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2F%3C%2Fspan%3Ehttps%3A%2F%2Ftools.ietf.org%2Fhtml%2Frfc6901%23section-3%3Cspan+class%3D"x x-first x-last">">rfc6901 section 3</a>
124124
*/
125125
@Test
126126
public void backslashHandling() {
@@ -130,7 +130,7 @@ public void backslashHandling() {
130130
/**
131131
* We pass quotations as-is
132132
*
133-
* @see https://tools.ietf.org/html/rfc6901#section-3
133+
* @see <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2F%3C%2Fspan%3Ehttps%3A%2F%2Ftools.ietf.org%2Fhtml%2Frfc6901%23section-3%3Cspan+class%3D"x x-first x-last">">rfc6901 section 3</a>
134134
*/
135135
@Test
136136
public void quotationHandling() {

src/test/java/org/json/junit/data/ExceptionalBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.lang.reflect.InvocationTargetException;
99

1010
/**
11-
* Object for testing the exception handling in {@link JSONObject#populateMap}.
11+
* Object for testing the exception handling in {@link org.json.JSONObject#populateMap}.
1212
*
1313
* @author John Aylward
1414
*/

0 commit comments

Comments
 (0)