Skip to content

Commit

Permalink
Deserialize long values without throwing an exception when type isn't…
Browse files Browse the repository at this point in the history
… specified (#340)
  • Loading branch information
mduffin95 authored Aug 1, 2024
1 parent 0d09cbf commit f8ed70f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/joda/beans/ser/bin/AbstractBinReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,16 @@ Object parseSimple(int typeByte, Class<?> type) throws Exception {
// handle case where property type has changed from integral to float
return Float.valueOf((float) value);

} else {
} else if (type == Integer.class || type == int.class) {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Invalid binary data: Expected int, but was " + value);
}
return Integer.valueOf((int) value);
} else {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
return Long.valueOf(value);
}
return Integer.valueOf((int) value);
}
}
switch (typeByte) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,16 @@ private Object parseSimple(JsonEvent event, Class<?> type) throws Exception {
}
return Float.valueOf(fltVal);

} else {
} else if (type == Integer.class || type == int.class) {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Invalid JSON data: Expected int, but was " + value);
}
return Integer.valueOf((int) value);
} else {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
return Long.valueOf(value);
}
return Integer.valueOf((int) value);
}
}
case NUMBER_FLOATING: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,17 @@ private Object convertInteger(long value, Class<?> type) {
throw new IllegalArgumentException("Invalid data: Value exceeds capacity of float: " + value);
}
return Float.valueOf(fltVal);
} else {

} else if (type == Integer.class || type == int.class) {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Invalid data: Expected int, but was " + value);
}
return Integer.valueOf((int) value);
} else {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
return Long.valueOf(value);
}
return Integer.valueOf((int) value);
}
}

Expand Down
70 changes: 62 additions & 8 deletions src/test/java/org/joda/beans/sample/SimpleJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public final class SimpleJson implements ImmutableBean, Serializable {
private final ImmutableMap<String, List<String>> listInMap;
@PropertyDefinition
private final ImmutableMap<String, List<Integer>> listNumericInMap;
@PropertyDefinition
private final ImmutableMap<String, Object> objectInMap;

//------------------------- AUTOGENERATED START -------------------------
/**
Expand Down Expand Up @@ -152,7 +154,8 @@ private SimpleJson(
Map<Integer, String> intKeyMap,
Map<String, ImmKey> beanMap,
Map<String, List<String>> listInMap,
Map<String, List<Integer>> listNumericInMap) {
Map<String, List<Integer>> listNumericInMap,
Map<String, Object> objectInMap) {
this.primitiveChar = primitiveChar;
this.primitiveByte = primitiveByte;
this.primitiveShort = primitiveShort;
Expand All @@ -179,6 +182,7 @@ private SimpleJson(
this.beanMap = (beanMap != null ? ImmutableMap.copyOf(beanMap) : null);
this.listInMap = (listInMap != null ? ImmutableMap.copyOf(listInMap) : null);
this.listNumericInMap = (listNumericInMap != null ? ImmutableMap.copyOf(listNumericInMap) : null);
this.objectInMap = (objectInMap != null ? ImmutableMap.copyOf(objectInMap) : null);
}

@Override
Expand Down Expand Up @@ -420,6 +424,15 @@ public ImmutableMap<String, List<Integer>> getListNumericInMap() {
return listNumericInMap;
}

//-----------------------------------------------------------------------
/**
* Gets the objectInMap.
* @return the value of the property
*/
public ImmutableMap<String, Object> getObjectInMap() {
return objectInMap;
}

//-----------------------------------------------------------------------
/**
* Returns a builder that allows this bean to be mutated.
Expand Down Expand Up @@ -461,7 +474,8 @@ public boolean equals(Object obj) {
JodaBeanUtils.equal(intKeyMap, other.intKeyMap) &&
JodaBeanUtils.equal(beanMap, other.beanMap) &&
JodaBeanUtils.equal(listInMap, other.listInMap) &&
JodaBeanUtils.equal(listNumericInMap, other.listNumericInMap);
JodaBeanUtils.equal(listNumericInMap, other.listNumericInMap) &&
JodaBeanUtils.equal(objectInMap, other.objectInMap);
}
return false;
}
Expand Down Expand Up @@ -495,12 +509,13 @@ public int hashCode() {
hash = hash * 31 + JodaBeanUtils.hashCode(beanMap);
hash = hash * 31 + JodaBeanUtils.hashCode(listInMap);
hash = hash * 31 + JodaBeanUtils.hashCode(listNumericInMap);
hash = hash * 31 + JodaBeanUtils.hashCode(objectInMap);
return hash;
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder(864);
StringBuilder buf = new StringBuilder(896);
buf.append("SimpleJson{");
buf.append("primitiveChar").append('=').append(JodaBeanUtils.toString(primitiveChar)).append(',').append(' ');
buf.append("primitiveByte").append('=').append(JodaBeanUtils.toString(primitiveByte)).append(',').append(' ');
Expand All @@ -527,7 +542,8 @@ public String toString() {
buf.append("intKeyMap").append('=').append(JodaBeanUtils.toString(intKeyMap)).append(',').append(' ');
buf.append("beanMap").append('=').append(JodaBeanUtils.toString(beanMap)).append(',').append(' ');
buf.append("listInMap").append('=').append(JodaBeanUtils.toString(listInMap)).append(',').append(' ');
buf.append("listNumericInMap").append('=').append(JodaBeanUtils.toString(listNumericInMap));
buf.append("listNumericInMap").append('=').append(JodaBeanUtils.toString(listNumericInMap)).append(',').append(' ');
buf.append("objectInMap").append('=').append(JodaBeanUtils.toString(objectInMap));
buf.append('}');
return buf.toString();
}
Expand Down Expand Up @@ -680,6 +696,12 @@ public static final class Meta extends DirectMetaBean {
@SuppressWarnings({"unchecked", "rawtypes" })
private final MetaProperty<ImmutableMap<String, List<Integer>>> listNumericInMap = DirectMetaProperty.ofImmutable(
this, "listNumericInMap", SimpleJson.class, (Class) ImmutableMap.class);
/**
* The meta-property for the {@code objectInMap} property.
*/
@SuppressWarnings({"unchecked", "rawtypes" })
private final MetaProperty<ImmutableMap<String, Object>> objectInMap = DirectMetaProperty.ofImmutable(
this, "objectInMap", SimpleJson.class, (Class) ImmutableMap.class);
/**
* The meta-properties.
*/
Expand Down Expand Up @@ -710,7 +732,8 @@ public static final class Meta extends DirectMetaBean {
"intKeyMap",
"beanMap",
"listInMap",
"listNumericInMap");
"listNumericInMap",
"objectInMap");

/**
* Restricted constructor.
Expand Down Expand Up @@ -773,6 +796,8 @@ protected MetaProperty<?> metaPropertyGet(String propertyName) {
return listInMap;
case 391098024: // listNumericInMap
return listNumericInMap;
case -1297715720: // objectInMap
return objectInMap;
}
return super.metaPropertyGet(propertyName);
}
Expand Down Expand Up @@ -1001,6 +1026,14 @@ public MetaProperty<ImmutableMap<String, List<Integer>>> listNumericInMap() {
return listNumericInMap;
}

/**
* The meta-property for the {@code objectInMap} property.
* @return the meta-property, not null
*/
public MetaProperty<ImmutableMap<String, Object>> objectInMap() {
return objectInMap;
}

//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
Expand Down Expand Up @@ -1057,6 +1090,8 @@ protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
return ((SimpleJson) bean).getListInMap();
case 391098024: // listNumericInMap
return ((SimpleJson) bean).getListNumericInMap();
case -1297715720: // objectInMap
return ((SimpleJson) bean).getObjectInMap();
}
return super.propertyGet(bean, propertyName, quiet);
}
Expand Down Expand Up @@ -1104,6 +1139,7 @@ public static final class Builder extends DirectFieldsBeanBuilder<SimpleJson> {
private Map<String, ImmKey> beanMap;
private Map<String, List<String>> listInMap;
private Map<String, List<Integer>> listNumericInMap;
private Map<String, Object> objectInMap;

/**
* Restricted constructor.
Expand Down Expand Up @@ -1142,6 +1178,7 @@ private Builder(SimpleJson beanToCopy) {
this.beanMap = beanToCopy.getBeanMap();
this.listInMap = beanToCopy.getListInMap();
this.listNumericInMap = beanToCopy.getListNumericInMap();
this.objectInMap = beanToCopy.getObjectInMap();
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1200,6 +1237,8 @@ public Object get(String propertyName) {
return listInMap;
case 391098024: // listNumericInMap
return listNumericInMap;
case -1297715720: // objectInMap
return objectInMap;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
Expand Down Expand Up @@ -1287,6 +1326,9 @@ public Builder set(String propertyName, Object newValue) {
case 391098024: // listNumericInMap
this.listNumericInMap = (Map<String, List<Integer>>) newValue;
break;
case -1297715720: // objectInMap
this.objectInMap = (Map<String, Object>) newValue;
break;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
Expand Down Expand Up @@ -1327,7 +1369,8 @@ public SimpleJson build() {
intKeyMap,
beanMap,
listInMap,
listNumericInMap);
listNumericInMap,
objectInMap);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1621,10 +1664,20 @@ public Builder listNumericInMap(Map<String, List<Integer>> listNumericInMap) {
return this;
}

/**
* Sets the objectInMap.
* @param objectInMap the new value
* @return this, for chaining, not null
*/
public Builder objectInMap(Map<String, Object> objectInMap) {
this.objectInMap = objectInMap;
return this;
}

//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(864);
StringBuilder buf = new StringBuilder(896);
buf.append("SimpleJson.Builder{");
buf.append("primitiveChar").append('=').append(JodaBeanUtils.toString(primitiveChar)).append(',').append(' ');
buf.append("primitiveByte").append('=').append(JodaBeanUtils.toString(primitiveByte)).append(',').append(' ');
Expand All @@ -1651,7 +1704,8 @@ public String toString() {
buf.append("intKeyMap").append('=').append(JodaBeanUtils.toString(intKeyMap)).append(',').append(' ');
buf.append("beanMap").append('=').append(JodaBeanUtils.toString(beanMap)).append(',').append(' ');
buf.append("listInMap").append('=').append(JodaBeanUtils.toString(listInMap)).append(',').append(' ');
buf.append("listNumericInMap").append('=').append(JodaBeanUtils.toString(listNumericInMap));
buf.append("listNumericInMap").append('=').append(JodaBeanUtils.toString(listNumericInMap)).append(',').append(' ');
buf.append("objectInMap").append('=').append(JodaBeanUtils.toString(objectInMap));
buf.append('}');
return buf.toString();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/joda/beans/ser/SerTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public static SimpleJson testSimpleJson() {
.beanMap(ImmutableMap.of("a", key1, "b", key2))
.listInMap(map)
.listNumericInMap(map2)
.objectInMap(ImmutableMap.of("a", (long) Integer.MAX_VALUE + 1))
.build();
return result;
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/org/joda/beans/ser/SimpleJson.simplejson
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
},
"listNumericInMap": {
"A": [3, 2, 1]
},
"objectInMap": {
"a": 2147483648
}
}

0 comments on commit f8ed70f

Please sign in to comment.