I have following JsonArray consisting of Long Values:
我有以下由Long Values组成的JsonArray:
[1234567873,852369471,9517,789 ,4826,96127435]
Long , Long ,Long,Long,Long, Long
After sent over the eventbus there is a JsonArray consisting of Integers and Longs:
在通过eventbus发送之后,有一个由整数和长整数组成的JsonArray:
[1234567873,852369471,9517 ,789 ,4826 ,96127435]
Long , Long ,Integer,Integer,Integer,Long
Obiously Vertx
downsize small number-Longs to save memory - that's why I got the Cannot cast from Integer to Long
-ClassCastException when I try following Code:
很奇怪Vertx缩小了小数量 - Longs以节省内存 - 这就是为什么当我尝试使用Code时,我无法从Integer转换为Long-ClassCastException:
List<Long> collect = jsonArray.stream().map(element -> (Long) element).collect(Collectors.toList());
In contrast to that following Codeline works:
与Codeline工作后的情况相反:
for (int jsonArrayIndex = 0; jsonArrayIndex < jsonArray.size(); jsonArrayIndex++) {
Long longValue = jsonArray.getLong(jsonArrayIndex);
}
How this can work?
这怎么可行?
2 个解决方案
#1
1
JSON does not have a concept of Long or Integer, only the much more general concept of number
(see the JSON spec at json.org). Therefore it is up to your code to decide what object (i.e. Integer or Long) to parse a particular JSON number into.
JSON没有Long或Integer的概念,只有更一般的数字概念(参见json.org的JSON规范)。因此,由您的代码决定将特定JSON编号解析为哪个对象(即Integer或Long)。
By using jsonArray.stream()
(which returns a Stream<Object>
) you are allowing the Vertx
library to decide which object to parse each element into. In this case, it chooses the most appropriate type for each element one by one, i.e. some get parsed into a Long
and some into an Integer
. If you had any numbers like 1.5
, these would likely be parsed into Double
(you would need to check this though).
通过使用jsonArray.stream()(返回Stream
However, by using the jsonArray.getLong()
method, you are telling Vertx
that you have decided what object to parse each element into (a Long
) a therefore it will not try to be clever by selecting the most appropriate type for each element. If you had any number like 1.5
here, this method would likely throw an exception (again, you would have to check this).
但是,通过使用jsonArray.getLong()方法,您告诉Vertx您已决定将每个元素解析为(long)a的对象,因此不会通过为每个元素选择最合适的类型来设置聪明。如果你在这里有任何数字,如1.5,这种方法可能会抛出异常(再次,你必须检查这个)。
#2
1
Even tho you can't cast from Integer
to Long
(because of class hierarchy, Integer
would have to extend Long
at it doesn't), you can convert from int
to long
, that's probably what this method does behind the scenes.
即使你不能从Integer转换为Long(因为类层次结构,Integer必须扩展Long而不是),你可以从int转换为long,这可能就是这个方法在幕后所做的。
#1
1
JSON does not have a concept of Long or Integer, only the much more general concept of number
(see the JSON spec at json.org). Therefore it is up to your code to decide what object (i.e. Integer or Long) to parse a particular JSON number into.
JSON没有Long或Integer的概念,只有更一般的数字概念(参见json.org的JSON规范)。因此,由您的代码决定将特定JSON编号解析为哪个对象(即Integer或Long)。
By using jsonArray.stream()
(which returns a Stream<Object>
) you are allowing the Vertx
library to decide which object to parse each element into. In this case, it chooses the most appropriate type for each element one by one, i.e. some get parsed into a Long
and some into an Integer
. If you had any numbers like 1.5
, these would likely be parsed into Double
(you would need to check this though).
通过使用jsonArray.stream()(返回Stream
However, by using the jsonArray.getLong()
method, you are telling Vertx
that you have decided what object to parse each element into (a Long
) a therefore it will not try to be clever by selecting the most appropriate type for each element. If you had any number like 1.5
here, this method would likely throw an exception (again, you would have to check this).
但是,通过使用jsonArray.getLong()方法,您告诉Vertx您已决定将每个元素解析为(long)a的对象,因此不会通过为每个元素选择最合适的类型来设置聪明。如果你在这里有任何数字,如1.5,这种方法可能会抛出异常(再次,你必须检查这个)。
#2
1
Even tho you can't cast from Integer
to Long
(because of class hierarchy, Integer
would have to extend Long
at it doesn't), you can convert from int
to long
, that's probably what this method does behind the scenes.
即使你不能从Integer转换为Long(因为类层次结构,Integer必须扩展Long而不是),你可以从int转换为long,这可能就是这个方法在幕后所做的。