小编nut*_*ike的帖子

杰克逊:数组中缺少@JsonTypeInfo

我发现了杰克逊JSON处理器库的一些奇怪的行为,我很好奇这是故意还是错误.请看下面的代码:

@JsonTypeInfo(use = Id.NAME)
public class Nut {}
Run Code Online (Sandbox Code Playgroud)

...

ObjectMapper mapper = new ObjectMapper();

Nut nut = new Nut();
Object object = new Nut();
Nut[] nuts = new Nut[] { new Nut() };
Object[] objects = new Object[] { new Nut() };

System.out.println(mapper.writeValueAsString(nut));
System.out.println(mapper.writeValueAsString(object));
System.out.println(mapper.writeValueAsString(nuts));
System.out.println(mapper.writeValueAsString(objects));
Run Code Online (Sandbox Code Playgroud)

输出:

{"@type":"Nut"}
{"@type":"Nut"}
[{"@type":"Nut"}]
[{}]
Run Code Online (Sandbox Code Playgroud)

我期望(和想要)的是以下内容:

{"@type":"Nut"}
{"@type":"Nut"}
[{"@type":"Nut"}]
[{"@type":"Nut"}] // <<< type information included
Run Code Online (Sandbox Code Playgroud)

我是否会遗漏某些内容或者我应该提交错误报告?

java serialization json jackson

6
推荐指数
1
解决办法
1907
查看次数

jsonrpc4j:如何向参数添加类型信息?

我正在使用jsonrpc4j并被卡住了.我举了一个例子来说明我的问题:

抽象类:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(value = { @JsonSubTypes.Type(value = Walnut.class) })
public abstract class Nut {

}
Run Code Online (Sandbox Code Playgroud)

具体子类:

public class Walnut extends Nut {

}
Run Code Online (Sandbox Code Playgroud)

服务接口:

public interface ServiceInterface {
    public Nut getNut();
    public void setNut(Nut nut);
}
Run Code Online (Sandbox Code Playgroud)

服务本身:

public class Service implements ServiceInterface {
    public Nut getNut() { return new Walnut(); }
    public void setNut(Nut nut) {}
}
Run Code Online (Sandbox Code Playgroud)

服务器:

JsonRpcServer rpcServer = new JsonRpcServer(new ObjectMapper(), new Service());
StreamServer streamServer = new StreamServer(rpcServer, 50, 1420,
        50, InetAddress.getByName("127.0.0.1")); …
Run Code Online (Sandbox Code Playgroud)

java serialization json jackson deserialization

4
推荐指数
1
解决办法
4105
查看次数

标签 统计

jackson ×2

java ×2

json ×2

serialization ×2

deserialization ×1