将JSON整数数组反序列化为List<Integer>

huangapple go评论82阅读模式
英文:

Deserializing JSON array of integers to List<Integer>

问题

  1. 我有以下代码
  2. @ApplicationScoped
  3. @Path("/sets")
  4. @Consumes(MediaType.APPLICATION_JSON)
  5. @Produces(MediaType.APPLICATION_JSON)
  6. public class PartitionSet {
  7. @Path("/form")
  8. @POST
  9. public Response create(List<List<BigDecimal>> partitions){
  10. Map<String, Integer> obj = new HashMap<String, Integer>();
  11. obj.put("numPartitions", partitions.size());
  12. buildGraph(partitions);
  13. return Response.status(Status.OK).entity(obj).build();
  14. }
  15. }
  16. 这实际上是正常工作的但我想要能够让"create"方法的参数是List<List<Integer>>而不是jsonb反序列化器似乎正在使用的BigDecimal有没有办法做到这一点
  17. 我的pom有以下依赖项我应该使用不同的依赖项来使其工作吗
  18. <dependency>
  19. <groupId>io.quarkus</groupId>
  20. <artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
  21. </dependency>
  22. 如果我只是将参数更改为List<List<Integer>>则在反序列化时不会失败然而当我使用反序列化的结果当我执行partitions.get(<some number>).get(<some number>)我会得到
  23. 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')
  24. 我不确定为什么错误消息会提到这些类在哪个模块中我应该关心吗
  25. [已编辑以添加]
  26. 提供的输入JSON示例
  27. [
  28. [
  29. 16, 15, 2, 1
  30. ],
  31. [
  32. 16, 14, 3, 1
  33. ]
  34. ]
  35. [![显示触发异常的代码的异常屏幕截图][1]][1]
  36. [1]: https://i.stack.imgur.com/YBtZk.png
英文:

I've got the following code:

  1. @ApplicationScoped
  2. @Path(&quot;/sets&quot;)
  3. @Consumes(MediaType.APPLICATION_JSON)
  4. @Produces(MediaType.APPLICATION_JSON)
  5. public class PartitionSet {
  6. @Path(&quot;/form&quot;)
  7. @POST
  8. public Response create(List&lt;List&lt;BigDecimal&gt;&gt; partitions){
  9. Map&lt;String, Integer&gt; obj = new HashMap&lt;String, Integer&gt;();
  10. obj.put(&quot;numPartitions&quot;, partitions.size());
  11. buildGraph(partitions);
  12. return Response.status(Status.OK).entity(obj).build();
  13. }
  1. This actually works correctly, but I&#39;d like to be able to have that argument to the &quot;create&quot; method be List&lt;List&lt;Integer&gt;&gt; instead of the BigDecimal that the jsonb deserializer seems to be using. Is there a way to do that?
  2. My pom has the following dependency. Should I be using a different one to make this work?

&lt;dependency&gt;
&lt;groupId&gt;io.quarkus&lt;/groupId&gt;
&lt;artifactId&gt;quarkus-resteasy-reactive-jsonb&lt;/artifactId&gt;
&lt;/dependency&gt;

`

If I simply change the argument to List&lt;List&lt;Integer&gt;&gt;, there is no failure in the deserialization. However, when it comes to using the result of the deserialization (when I do partitions.get(&lt;some number&gt;).get(&lt;some number&gt;)), 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 &#39;bootstrap&#39;)

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
]
]

将JSON整数数组反序列化为List<Integer>

答案1

得分: 0

I've fixed this by defining a JsonbAdapter:

  1. public class IntegerSerializer implements JsonbAdapter<Integer, BigDecimal>{
  2. @Override
  3. public BigDecimal adaptToJson(Integer obj) throws Exception {
  4. return new BigDecimal(obj);
  5. }
  6. @Override
  7. public Integer adaptFromJson(BigDecimal obj) throws Exception {
  8. return obj.intValue();
  9. }
  10. }

....which requires a JsonbConfigCustomizer:

  1. @Singleton
  2. public class IntegerSerConfig implements JsonbConfigCustomizer{
  3. @Override
  4. public void customize(JsonbConfig jsonbConfig) {
  5. System.out.println("Configuring....");
  6. jsonbConfig.withAdapters(new IntegerSerializer());
  7. }
  8. }

For some reason, this would not work with the jsonb reactive extensions, so in my pom, I switched to the following dependencies:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-resteasy</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.quarkus</groupId>
  7. <artifactId>quarkus-resteasy-jsonb</artifactId>
  8. </dependency>
英文:

I've fixed this by defining a JsonbAdapter:

  1. public class IntegerSerializer implements JsonbAdapter&lt;Integer, BigDecimal&gt;{
  2. @Override
  3. public BigDecimal adaptToJson(Integer obj) throws Exception {
  4. return new BigDecimal(obj);
  5. }
  6. @Override
  7. public Integer adaptFromJson(BigDecimal obj) throws Exception {
  8. return obj.intValue();
  9. }
  10. }

....which requires a JsonbConfigCustomizer:

  1. @Singleton
  2. public class IntegerSerConfig implements JsonbConfigCustomizer{
  3. @Override
  4. public void customize(JsonbConfig jsonbConfig) {
  5. System.out.println(&quot;Configuring....&quot;);
  6. jsonbConfig.withAdapters(new IntegerSerializer());
  7. }
  8. }

For some reason, this would not work with the jsonb reactive extensions, so in my pom, I switched to the following dependencies:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
  3. &lt;artifactId&gt;quarkus-resteasy&lt;/artifactId&gt;
  4. &lt;/dependency&gt;
  5. &lt;dependency&gt;
  6. &lt;groupId&gt;io.quarkus&lt;/groupId&gt;
  7. &lt;artifactId&gt;quarkus-resteasy-jsonb&lt;/artifactId&gt;
  8. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年6月13日 07:56:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定