Skip to content

Commit

Permalink
Range: BigDecimal to Number
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Jun 20, 2023
1 parent e54bb4d commit c006f2b
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 151 deletions.
7 changes: 4 additions & 3 deletions binding/src/main/java/org/jsonx/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ else if (value != null && !genericType.isInstance(value))
throw new UnsupportedOperationException(e);
}
catch (final InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException)e.getCause();
final Throwable cause = e.getCause();
if (cause instanceof RuntimeException)
throw (RuntimeException)cause;

throw new RuntimeException(e.getCause());
throw new RuntimeException(cause);
}
}

Expand Down
7 changes: 4 additions & 3 deletions binding/src/main/java/org/jsonx/JsdUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,11 @@ static Object invoke(final Executable executable, final Object arg) {
throw new UnsupportedOperationException(e);
}
catch (final InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException)e.getCause();
final Throwable cause = e.getCause();
if (cause instanceof RuntimeException)
throw (RuntimeException)cause;

throw new RuntimeException(e.getCause());
throw new RuntimeException(cause);
}
}

Expand Down
8 changes: 4 additions & 4 deletions binding/src/main/java/org/jsonx/JxDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public final <T extends JxObject>T parseObject(final String json, final Class<?
@SuppressWarnings({"null", "unchecked"})
public final <T extends JxObject>T parseObject(final JsonReader reader, final TriPredicate<JxObject,String,Object> onPropertyDecode, final Class<? extends T> ... types) throws DecodeException, IOException, JsonParseException {
DecodeException exception = null;
for (final Class<? extends T> type : assertNotEmpty(types)) {
for (final Class<? extends T> type : assertNotEmpty(types)) { // [A]
final Object result = parseObject(reader, onPropertyDecode, type, exception);
if (result instanceof DecodeException)
exception = (DecodeException)result;
Expand Down Expand Up @@ -297,7 +297,7 @@ public final <T extends JxObject>T parseObject(final JsonReader reader, final Tr
@SuppressWarnings({"null", "unchecked"})
public final <T extends JxObject>T parseObject(final JsonReader reader, final TriPredicate<JxObject,String,Object> onPropertyDecode, final Collection<Class<? extends T>> types) throws DecodeException, IOException, JsonParseException {
DecodeException exception = null;
for (final Class<? extends T> type : assertNotEmpty(types)) {
for (final Class<? extends T> type : assertNotEmpty(types)) { // [C]
final Object result = parseObject(reader, onPropertyDecode, type, exception);
if (result instanceof DecodeException)
exception = (DecodeException)result;
Expand Down Expand Up @@ -531,7 +531,7 @@ public final ArrayList<?> parseArray(final String json, final Class<? extends An
@SuppressWarnings("null")
public final ArrayList<?> parseArray(final JsonReader reader, final Class<? extends Annotation> ... annotationTypes) throws DecodeException, JsonParseException, IOException {
DecodeException exception = null;
for (final Class<? extends Annotation> annotationType : assertNotEmpty(annotationTypes)) {
for (final Class<? extends Annotation> annotationType : assertNotEmpty(annotationTypes)) { // [A]
final Object result = parseArray(reader, annotationType, exception);
if (result instanceof DecodeException)
exception = (DecodeException)result;
Expand Down Expand Up @@ -562,7 +562,7 @@ public final ArrayList<?> parseArray(final JsonReader reader, final Class<? exte
@SuppressWarnings("null")
public final ArrayList<?> parseArray(final JsonReader reader, final Collection<Class<? extends Annotation>> annotationTypes) throws DecodeException, JsonParseException, IOException {
DecodeException exception = null;
for (final Class<? extends Annotation> annotationType : assertNotEmpty(annotationTypes)) {
for (final Class<? extends Annotation> annotationType : assertNotEmpty(annotationTypes)) { // [C]
final Object result = parseArray(reader, annotationType, exception);
if (result instanceof DecodeException)
exception = (DecodeException)result;
Expand Down
25 changes: 14 additions & 11 deletions binding/src/main/java/org/jsonx/JxEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected JxEncoder(final int indent) {
*/
JxEncoder(final int indent, final boolean validate) {
if (indent < 0)
throw new IllegalArgumentException("Indent must be a non-negative: " + indent);
throw new IllegalArgumentException("Indent must be non-negative: " + indent);

this.indent = indent;
this.validate = validate;
Expand Down Expand Up @@ -217,10 +217,11 @@ private static Object getValue(final Object object, final Method getMethod, fina
throw new RuntimeException(e);
}
catch (final InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException)e.getCause();
final Throwable cause = e.getCause();
if (cause instanceof RuntimeException)
throw (RuntimeException)cause;

throw new RuntimeException(e.getCause());
throw new RuntimeException(cause);
}
}

Expand Down Expand Up @@ -371,14 +372,16 @@ void beforePut(final Method[] methods) {
Error toString(final JxObject object, final OnEncode onEncode, final StringBuilder b, final int depth) {
b.append('{');
boolean hasProperties = false;
Annotation[] annotations;
Annotation annotation = null;
String name;
boolean nullable = false;
Use use = null;
for (final Method getMethod : classToOrderedMethods.get(object.getClass())) { // [A]
Annotation annotation = null;
String name = null;
boolean nullable = false;
Use use = null;
final Annotation[] annotations = Classes.getAnnotations(getMethod);
for (int j = 0, j$ = annotations.length; j < j$; ++j) { // [A]
annotation = annotations[j];
annotations = Classes.getAnnotations(getMethod);
name = null;
for (int i = 0, i$ = annotations.length; i < i$; ++i) { // [A]
annotation = annotations[i];
if (annotation instanceof StringProperty) {
final StringProperty property = (StringProperty)annotation;
name = property.name();
Expand Down
12 changes: 8 additions & 4 deletions binding/src/main/java/org/jsonx/NumberCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ static String format(final Object object) {
* @return The provided provided {@code double} as a string.
* @implNote This method mimicks JavaScript's Double.toString() algorithm.
*/
private static String format(final double value) {
static String format(final double value) {
return 0.000001 <= value && value <= 9.999999999999999E20 ? decimalFormatLocal.get().format(value) : Numbers.stripTrailingZeros(String.valueOf(value));
}

static Class<? extends Number> getDefaultClass(final int scale) {
return scale == 0 ? Long.class : Double.class;
}

private static Number parseDefaultNumber(final int scale, final String json, final boolean strict) {
try {
return JsonUtil.parseNumber(scale == 0 ? Long.class : Double.class, json, strict);
return JsonUtil.parseNumber(getDefaultClass(scale), json, strict);
}
catch (final JsonParseException | NumberFormatException e) {
return null;
Expand Down Expand Up @@ -144,7 +148,7 @@ else if (object.longValue() != object.doubleValue()) {

if (range.length() > 0) {
try {
if (!Range.from(range, type).isValid(object))
if (!Range.from(range, scale, type).isValid(object))
return Error.RANGE_NOT_MATCHED(range, object, null);
}
catch (final ParseException e) {
Expand All @@ -167,7 +171,7 @@ else if (object.longValue() != object.doubleValue()) {
}
else {
try {
this.range = Range.from(range, JsdUtil.getRealType(getMethod));
this.range = Range.from(range, scale, JsdUtil.getRealType(getMethod));
}
catch (final ParseException e) {
throw new ValidationException("Invalid range attribute: " + Annotations.toSortedString(property, JsdUtil.ATTRIBUTES, true), e);
Expand Down

0 comments on commit c006f2b

Please sign in to comment.