说我有一个接口A,我想为所有类实现接口A使用自定义反序列化器,所以我使用下面的代码,但它不起作用,而CustomAserializer工作.那么我应该怎么做反序列化所有类使用我的自定义反序列化器实现A. 谢谢.
module.addDeserializer(A.class, new CustomADeserializer());
module.addSerializer(A.class, new CustomASerializer())
Run Code Online (Sandbox Code Playgroud) 我需要在已定义 POJO 的列表中动态排除某些属性。要序列化的主要 POJO 是:
public class Foo
{
List<Bar> bar;
public void setBar(List<Bar> bar)
{
this.bar = bar;
}
public List<Bar> getBar()
{
return this.bar;
}
public static class Bar
{
private int id;
private boolean ignoreId;
private String name;
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return this.id;
}
public void setIgnoreId(boolean ignoreId)
{
this.ignoreId = ignoreId;
}
public boolean isIgnoreId()
{
return this.ignoreId;
}
public void setName(String name)
{
this.name = …Run Code Online (Sandbox Code Playgroud) 假设我有以下Java类:
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Demo {
public int x;
public int y;
public List<Pair<Integer, Integer>> the_list;
}
Run Code Online (Sandbox Code Playgroud)
我想从以下json格式填充它:
{ "x" : 1,
"y" : 2,
"the_list" : [[1,2],[3,4]]}
Run Code Online (Sandbox Code Playgroud)
使用更快的xml
ObjectMapper mapper = new ObjectMapper();
Run Code Online (Sandbox Code Playgroud)
我可以在那里调用mapper.readTree(json)并填写我需要的一切.问题是我拥有的实际类(不是Demo)包含很多参数,我想从数据绑定功能中受益.
尝试平原:
mapper.readValue(json, Demo.class)
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of
org.apache.commons.lang3.tuple.Pair, problem: abstract types either need to be mapped to
concrete types, have custom deserializer, or be instantiated with additional type
information
Run Code Online (Sandbox Code Playgroud)
有没有办法将自定义解析与数据绑定混合?我查看了注释,但没有找到任何适合该目的的东西(我无法使用mixins来处理泛型,没有调用the_list的自定义setter可能是因为它是一个列表,JsonCreator不是一个选项,因为我没有写Pair类......).
使用Jackson,我知道可以使用来为视图添加/排除属性@JsonView。
如何按视图更改JSON属性的值?
例如,我可能希望视图A中的属性值是整个对象,视图B中的属性值被滤除某些属性,在视图C中,我只希望它是“ id”(无对象),并且在视图D中,我可能希望它成为“名称”(无对象):
// view A JSON
{
"prop": {"id": 123, "name": "abc", "description": "def"}
}
// view B JSON
{
"prop": {"id": 123, "name": "abc"}
}
// view C JSON
{
"prop": 123
}
// view D JSON
{
"prop": "abc"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将Java版本升级1.7到1.8.除了在Maven插件中将目标版本升级到1.8之外,没有更改任何代码.
Error: cannot access Versioned.
资源:
private ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(data);
Run Code Online (Sandbox Code Playgroud)
无法在互联网上的任何地方找到任何文档.我认为1.8应该完全向后兼容.
编辑:如果我没有改变pom,如果我将我的本地maven设置为Java 8,它仍然会破坏.所以用Java 8编译有问题:(
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile …Run Code Online (Sandbox Code Playgroud) 我为我的 DigitalInput 和 Resource 类创建了一个自定义解串器。
数字输入包含一个List<Resource>.
所以我需要ResourceDeserializer在 my 中使用我的自定义DigitalInputDeserializer,但我不知道该怎么做。
public class DigitalInputDeserializer extends JsonDeserializer<DigitalInput> {
@Override
public DigitalInput deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
DigitalInput digitalInput = null;
JsonNode node = p.getCodec().readTree(p);
Date timestamp = Instant.parse(node.get("timestamp").asText()).toDate();
String matter = node.get("matter").asText();
String description = node.get("comment").asText();
//...
JsonNode resources = node.get("resources");
//???
return digitalInput;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,你可以猜到,resources 是一个 json 数组。为了反序列化Resource我已经实现了 aResourceDeserializer并且我想在这里使用它。
我能做些什么?谢谢大家。
我们正在使用提供json的REST服务,该服务将包含一些标准属性以及许多动态属性.
例如:
{
id: 123,
name: "some name",
custom_name: "Some value",
some_other_custom_name: "Some other value",
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望课程设计如下:
public class MyObject{
@JsonProperty int id;
@JsonProperty String name;
private Map<String, String> customVals;
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getCustomVal(String key){
return customVals.get(key);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法说服杰克逊将自定义值推入Map(或实现等效功能)?
现在,我只是将整个对象反序列化为一个Map,并将其包装在我的业务对象中,但它并不像反序列化处理它那样优雅.
我有一个现有的类层次结构,如下所示:
public interface Service {
String getId();
String getName();
}
public class FooTask extends AbstractTask {
private final static ObjectMapper JSON_MAPPER = new ObjectMapper();
static {
JSON_MAPPER.registerModule(new SimpleModule().addDeserializer(Result.class, new ResultDeserializer());
}
public FooTask(Service service) {
super(service);
}
@Override public Result call() throws Exception {
InputStream json = <... execute some code to retrieve JSON ...>
Result result = JSON_MAPPER.readValue(json, Result.class);
}
private static class ResultDeserializer {
@Override public Result deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
//
// Need to …Run Code Online (Sandbox Code Playgroud) 我想从对象创建 JSON 字符串。
ObjectMapper om = new ObjectMapper();
String str = om.writeValueAsString(obj);
Run Code Online (Sandbox Code Playgroud)
有些对象很大,创建 JSON 字符串需要很长时间。创建 8MB JSON 字符串大约需要 15 秒。
我该如何改进这个?
我想用Jackson来序列化流的输出.杰克逊没有对序列化的内在支持java.util.stream.Stream,但它能够序列化java.util.Iterator.
为了说明这个问题,我想序列化这个简单的界面:
public interface SerializeMe {
Iterator<Integer> getMyNumbers();
}
Run Code Online (Sandbox Code Playgroud)
我将比较序列化List.iterator()与输出的输出Stream.iterator():
public class SerializeViaList implements SerializeMe {
@Override
public Iterator<Integer> getMyNumbers() {
return Arrays.asList(1, 2, 3).iterator();
}
}
public class SerializeViaStream implements SerializeMe {
@Override
public Iterator<Integer> getMyNumbers() {
return Arrays.asList(1, 2, 3).stream().iterator();
}
}
Run Code Online (Sandbox Code Playgroud)
以下方法将演示这两个类的输出:
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
System.out.println("via list: " + mapper.writeValueAsString(new SerializeViaList()));
System.out.println("via stream: " + mapper.writeValueAsString(new SerializeViaStream()));
}
Run Code Online (Sandbox Code Playgroud)
这个输出是: …