英文:
Deserializing JSON array of integers to List<Integer>
问题
我有以下代码:
@ApplicationScoped
@Path("/sets")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PartitionSet {
@Path("/form")
@POST
public Response create(List<List<BigDecimal>> partitions){
Map<String, Integer> obj = new HashMap<String, Integer>();
obj.put("numPartitions", partitions.size());
buildGraph(partitions);
return Response.status(Status.OK).entity(obj).build();
}
}
这实际上是正常工作的,但我想要能够让"create"方法的参数是List<List<Integer>>,而不是jsonb反序列化器似乎正在使用的BigDecimal。有没有办法做到这一点?
我的pom有以下依赖项。我应该使用不同的依赖项来使其工作吗?
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
</dependency>
如果我只是将参数更改为List<List<Integer>>,则在反序列化时不会失败。然而,当我使用反序列化的结果(当我执行partitions.get(<some number>).get(<some number>))时,我会得到:
java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.Integer (java.math.BigDecimal and java.lang.Integer are in module java.base of loader 'bootstrap')
我不确定为什么错误消息会提到这些类在哪个模块中。我应该关心吗?
[已编辑以添加]
提供的输入JSON示例:
[
[
16, 15, 2, 1
],
[
16, 14, 3, 1
]
]
[![显示触发异常的代码的异常屏幕截图][1]][1]
[1]: https://i.stack.imgur.com/YBtZk.png
英文:
I've got the following code:
@ApplicationScoped
@Path("/sets")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PartitionSet {
@Path("/form")
@POST
public Response create(List<List<BigDecimal>> partitions){
Map<String, Integer> obj = new HashMap<String, Integer>();
obj.put("numPartitions", partitions.size());
buildGraph(partitions);
return Response.status(Status.OK).entity(obj).build();
}
This actually works correctly, but I'd like to be able to have that argument to the "create" method be List<List<Integer>> instead of the BigDecimal that the jsonb deserializer seems to be using. Is there a way to do that?
My pom has the following dependency. Should I be using a different one to make this work?
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
</dependency>
`
If I simply change the argument to List<List<Integer>>
, there is no failure in the deserialization. However, when it comes to using the result of the deserialization (when I do partitions.get(<some number>).get(<some number>)
), I get:
java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.Integer (java.math.BigDecimal and java.lang.Integer are in module java.base of loader 'bootstrap')
I'm not sure why the error message talks about which module those classes are in. Should I care?
[Edited to add]
Example of JSON provided as input:
[
[
16, 15, 2, 1
],
[
16, 14, 3, 1
]
]
答案1
得分: 0
I've fixed this by defining a JsonbAdapter:
public class IntegerSerializer implements JsonbAdapter<Integer, BigDecimal>{
@Override
public BigDecimal adaptToJson(Integer obj) throws Exception {
return new BigDecimal(obj);
}
@Override
public Integer adaptFromJson(BigDecimal obj) throws Exception {
return obj.intValue();
}
}
....which requires a JsonbConfigCustomizer:
@Singleton
public class IntegerSerConfig implements JsonbConfigCustomizer{
@Override
public void customize(JsonbConfig jsonbConfig) {
System.out.println("Configuring....");
jsonbConfig.withAdapters(new IntegerSerializer());
}
}
For some reason, this would not work with the jsonb reactive extensions, so in my pom, I switched to the following dependencies:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
英文:
I've fixed this by defining a JsonbAdapter:
public class IntegerSerializer implements JsonbAdapter<Integer, BigDecimal>{
@Override
public BigDecimal adaptToJson(Integer obj) throws Exception {
return new BigDecimal(obj);
}
@Override
public Integer adaptFromJson(BigDecimal obj) throws Exception {
return obj.intValue();
}
}
....which requires a JsonbConfigCustomizer:
@Singleton
public class IntegerSerConfig implements JsonbConfigCustomizer{
@Override
public void customize(JsonbConfig jsonbConfig) {
System.out.println("Configuring....");
jsonbConfig.withAdapters(new IntegerSerializer());
}
}
For some reason, this would not work with the jsonb reactive extensions, so in my pom, I switched to the following dependencies:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论