Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of long numbers in RpcObject #21

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public final class RpcBoolean extends RpcObject {
public final class RpcBoolean implements RpcObject {

private final boolean value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

@RequiredArgsConstructor
public final class RpcList extends RpcObject {
public final class RpcList implements RpcObject {

private final List<RpcObject> list;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Map;

@RequiredArgsConstructor
public final class RpcMap extends RpcObject {
public final class RpcMap implements RpcObject {

private final Map<String, RpcObject> map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import java.util.Map;

public final class RpcNull extends RpcObject {
public final class RpcNull implements RpcObject {
@Override
public boolean isNull() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public final class RpcNumber extends RpcObject {
public final class RpcNumber implements RpcObject {

private final Number value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,77 @@
import java.util.List;
import java.util.Map;

public abstract class RpcObject {
public interface RpcObject {

RpcObject() {
}

public boolean isNull() {
default boolean isNull() {
return false;
}

public boolean isBoolean() {
default boolean isBoolean() {
return false;
}

public boolean isList() {
default boolean isList() {
return false;
}

public boolean isMap() {
default boolean isMap() {
return false;
}

public boolean isNumber() {
default boolean isNumber() {
return false;
}

public boolean isString() {
default boolean isString() {
return false;
}

public Boolean asBoolean() {
default Boolean asBoolean() {
throw new IllegalStateException();
}

public List<RpcObject> asList() {
default List<RpcObject> asList() {
throw new IllegalStateException();
}

public Map<String, RpcObject> asMap() {
default Map<String, RpcObject> asMap() {
throw new IllegalStateException();
}

public Number asNumber() {
default Number asNumber() {
throw new IllegalStateException();
}

public String asString() {
default String asString() {
throw new IllegalStateException();
}

public static RpcObject ofNull() {
static RpcObject ofNull() {
return new RpcNull();
}

public static RpcObject of(boolean value) {
static RpcObject of(boolean value) {
return new RpcBoolean(value);
}

public static RpcObject of(List<RpcObject> list) {
static RpcObject of(List<RpcObject> list) {
return list == null ? new RpcNull() : new RpcList(list);
}

public static RpcObject of(Map<String, RpcObject> map) {
static RpcObject of(Map<String, RpcObject> map) {
return map == null ? new RpcNull() : new RpcMap(map);
}

public static RpcObject of(Number value) {
static RpcObject of(long value) {
return new RpcNumber(value);
}

static RpcObject of(Number value) {
return value == null ? new RpcNull() : new RpcNumber(value);
}

public static RpcObject of(String value) {
static RpcObject of(String value) {
return value == null ? new RpcNull() : new RpcString(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public final class RpcString extends RpcObject {
public final class RpcString implements RpcObject {

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.val;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;

Expand All @@ -25,7 +27,7 @@ public RpcObject read(JsonReader in) throws IOException {
case BOOLEAN:
return new RpcBoolean(in.nextBoolean());
case NUMBER:
return new RpcNumber(in.nextDouble());
return new RpcNumber(parseNumber(in));
case STRING:
return new RpcString(in.nextString());
case BEGIN_ARRAY:
Expand All @@ -48,4 +50,21 @@ public RpcObject read(JsonReader in) throws IOException {
return new RpcMap(map);
}
}

private Number parseNumber(JsonReader in) throws IOException {
val value = in.nextString();
try {
return Long.parseLong(value);
} catch (NumberFormatException ignored) {
// Ignore
}

try {
return new BigInteger(value);
} catch (NumberFormatException ignored) {
// Ignore
}

return new BigDecimal(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ void decodeNullResult() {
.isEqualTo(expected);
}

@Test
void DecodeLongNumber() {
val json = "{\n" +
" \"result\": 506884800,\n" +
" \"id\": 0,\n" +
" \"jsonrpc\": \"3.0\"\n" +
"}";
val actual = RpcCoder.decodeJson(json);

val expected = new JsonRpcResponse();
expected.jsonrpc = "3.0";
expected.id = 0;
expected.result = RpcObject.of(506884800);
assertThat(actual)
.usingRecursiveComparison()
.isEqualTo(expected);
}

@Test
void decodeJson() {
val json = "{\n" +
Expand Down