Skip to content
Open
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
26 changes: 14 additions & 12 deletions core/src/main/java/io/grpc/internal/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public static Double getNumberAsDouble(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Double) {
return (Double) value;
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value instanceof String) {
try {
Expand All @@ -132,8 +132,8 @@ public static Float getNumberAsFloat(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Float) {
return (Float) value;
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
try {
Expand All @@ -159,11 +159,12 @@ public static Integer getNumberAsInteger(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Double) {
Double d = (Double) value;
int i = d.intValue();
if (value instanceof Number) {
Number n = (Number) value;
double d = n.doubleValue();
int i = n.intValue();
if (i != d) {
throw new ClassCastException("Number expected to be integer: " + d);
throw new ClassCastException("Number expected to be integer: " + n);
}
return i;
}
Expand All @@ -190,11 +191,12 @@ public static Long getNumberAsLong(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Double) {
Double d = (Double) value;
long l = d.longValue();
if (value instanceof Number) {
Number n = (Number) value;
double d = n.doubleValue();
long l = n.longValue();
if (l != d) {
throw new ClassCastException("Number expected to be long: " + d);
throw new ClassCastException("Number expected to be long: " + n);
}
return l;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public ManagedChannelImplBuilder defaultServiceConfig(@Nullable Map<String, ?> s
parsedMap.put(key, checkListEntryTypes((List<?>) value));
} else if (value instanceof String) {
parsedMap.put(key, value);
} else if (value instanceof Double) {
} else if (value instanceof Number) {
parsedMap.put(key, value);
} else if (value instanceof Boolean) {
parsedMap.put(key, value);
Expand All @@ -606,7 +606,7 @@ private static List<?> checkListEntryTypes(List<?> list) {
parsedList.add(checkListEntryTypes((List<?>) value));
} else if (value instanceof String) {
parsedList.add(value);
} else if (value instanceof Double) {
} else if (value instanceof Number) {
parsedList.add(value);
} else if (value instanceof Boolean) {
parsedList.add(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,39 @@ public void defaultServiceConfig_intValue() {
Map<String, Object> config = new HashMap<>();
config.put("key", 3);

assertThrows(IllegalArgumentException.class, () -> builder.defaultServiceConfig(config));
builder.defaultServiceConfig(config);

assertThat(builder.defaultServiceConfig).containsExactlyEntriesIn(config);
}

@Test
public void defaultServiceConfig_longValue() {
Map<String, Object> config = new HashMap<>();
config.put("key", 3L);

builder.defaultServiceConfig(config);

assertThat(builder.defaultServiceConfig).containsExactlyEntriesIn(config);
}

@Test
public void defaultServiceConfig_list_intValue() {
Map<String, Object> config = new HashMap<>();
config.put("key", Collections.singletonList(3));

builder.defaultServiceConfig(config);

assertThat(builder.defaultServiceConfig).containsExactlyEntriesIn(config);
}

@Test
public void defaultServiceConfig_list_longValue() {
Map<String, Object> config = new HashMap<>();
config.put("key", Collections.singletonList(3L));

builder.defaultServiceConfig(config);

assertThat(builder.defaultServiceConfig).containsExactlyEntriesIn(config);
}

@Test
Expand Down
Loading