Skip to content

Commit

Permalink
refactor: address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Zurcusa committed Aug 14, 2024
1 parent 9cfe9fe commit 3946ecb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/limechain/utils/json/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,22 @@ private String parseString() {
return sb.toString();
}

private Number parseNumber() {
private Object parseNumber() {
int start = index;
while (index < json.length() && (Character.isDigit(json.charAt(index)) || json.charAt(index) == '-' || json.charAt(index) == '.' || json.charAt(index) == 'e' || json.charAt(index) == 'E')) {
boolean isValidNumberChar = (Character.isDigit(json.charAt(index))
|| json.charAt(index) == '-'
|| json.charAt(index) == '.'
|| json.charAt(index) == 'e'
|| json.charAt(index) == 'E');
while (index < json.length() && isValidNumberChar) {
index++;
}
String numberStr = json.substring(start, index).trim();
try {
if (numberStr.contains(".") || numberStr.contains("e") || numberStr.contains("E")) {
return Double.parseDouble(numberStr);
} else {
return Long.parseLong(numberStr);
return numberStr;
}
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid number format: " + numberStr);
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/com/limechain/utils/json/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Base64;
import java.util.List;
Expand Down Expand Up @@ -67,13 +68,18 @@ private <T> T convertValue(Class<T> type, Object value) {
if (type.isInstance(value)) {
return (T) value;
} else if (type == Integer.class || type == int.class) {
return (T) (Integer) ((Number) value).intValue();
return handleWholeNumber(value, (long) Integer.MIN_VALUE, (long) Integer.MIN_VALUE);
} else if (type == Long.class || type == long.class) {
return (T) (Long) ((Number) value).longValue();
return handleWholeNumber(value, Long.MIN_VALUE, Long.MAX_VALUE);
} else if (type == Double.class || type == double.class) {
return (T) (Double) ((Number) value).doubleValue();
BigDecimal bigDecimalValue = new BigDecimal((String) value);
double doubleValue = bigDecimalValue.doubleValue();
if (doubleValue == Double.POSITIVE_INFINITY || doubleValue == Double.NEGATIVE_INFINITY) {
throw new ArithmeticException("Value out of range for Double: " + value);
}
return (T) Double.valueOf(doubleValue);
} else if (type == BigInteger.class) {
return (T) BigInteger.valueOf((Long) value);
return (T) new BigInteger((String) value);
} else if (type == Boolean.class || type == boolean.class) {
return (T) value;
} else if (type == String.class) {
Expand All @@ -91,6 +97,16 @@ private <T> T convertValue(Class<T> type, Object value) {
throw new RuntimeException("Unsupported field type: " + type);
}

@SuppressWarnings("unchecked")
private <T> T handleWholeNumber(Object value, Long min, Long max) {
BigInteger bigIntValue = new BigInteger((String) value);
if (bigIntValue.compareTo(BigInteger.valueOf(min)) < 0 ||
bigIntValue.compareTo(BigInteger.valueOf(max)) > 0) {
throw new ArithmeticException("Value out of range number type: " + value);
}
return (T) Integer.valueOf(bigIntValue.intValue());
}

private Object convertArray(Class<?> componentType, List<?> jsonArray) {
Object array = Array.newInstance(componentType, jsonArray.size());
for (int i = 0; i < jsonArray.size(); i++) {
Expand Down

0 comments on commit 3946ecb

Please sign in to comment.