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

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

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(&quot;/sets&quot;)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PartitionSet {
@Path(&quot;/form&quot;)
@POST
public Response create(List&lt;List&lt;BigDecimal&gt;&gt; partitions){
Map&lt;String, Integer&gt; obj = new HashMap&lt;String, Integer&gt;();
obj.put(&quot;numPartitions&quot;, partitions.size());
buildGraph(partitions);
return Response.status(Status.OK).entity(obj).build();
}
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?
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:

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&lt;Integer, BigDecimal&gt;{
@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(&quot;Configuring....&quot;);
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:

    &lt;dependency&gt;
&lt;groupId&gt;io.quarkus&lt;/groupId&gt;
&lt;artifactId&gt;quarkus-resteasy&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.quarkus&lt;/groupId&gt;
&lt;artifactId&gt;quarkus-resteasy-jsonb&lt;/artifactId&gt;
&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:

确定