[英]Jackson - Map JSON string field to class/pojo that has only one field (a string)
I am writing a JSON interface/library to work with Bugzilla's webservice. 我正在编写一个JSON接口/库来与Bugzilla的webservice一起工作。
Is this possible using an annotation or something? 这可能是使用注释或其他东西吗? Or am I to write a custom deserializer for every instance like this? 或者我是否为这样的每个实例编写自定义反序列化器?
I've tried doing some research, and found some information about a Value Instantiator or using constructors, but isn't using constructors anti-bean like? 我已经尝试过做一些研究,发现了一些关于Value Instantiator或使用构造函数的信息,但是不使用构造函数反bean吗? I've found documentation to be sparse or hard to understand with the newer features. 我发现使用较新的功能,文档很稀疏或难以理解。
Example: 例:
public class Bug{
// Bug info, strings, ints, yadda yadda.
private User creator; // creator of the bug, json is like {"creator":"blahblah@email.com"}
}
public class User{
private String username;
}
//insert setter/getter.
The reason I am using a pojo for One field is because this User class is extended by another that has more fields. 我在一个字段中使用pojo的原因是因为此User类由具有更多字段的另一个扩展。 While I can implement a constructor to achieve this effect, doing so means I have to implement the same constructor for all other subclasses. 虽然我可以实现一个构造函数来实现这种效果,但这样做意味着我必须为所有其他子类实现相同的构造函数。 I feel like there is something similar to @JsonValue but for setting (I tried JsonCreator on my setter but still got the same error as I have been getting below). 我觉得有类似于@JsonValue的东西,但是对于设置(我在我的setter上尝试了JsonCreator,但仍然得到了与我下面相同的错误)。
"Can not instantiate value of type [simple type, class User], from String value; no single-String constructor/factory method" “无法实例化类型[simple type,class User]的值,来自String值;没有单一的String构造函数/工厂方法”
Thanks. 谢谢。
Use @JsonCreator for deserializing and use @JsonValue for Serializing. 使用@JsonCreator进行反序列化并使用@JsonValue进行序列化。 Have tested the code. 测试了代码。 Please make sure that getJsonString() is public method. 请确保getJsonString()是公共方法。
For example: 例如:
public class User{
private String username;
@JsonValue
public String getJsonString() {
return username;
}
@JsonCreator
private static User parseJson(String jsonStr) {
User u = new User();
u.username = jsonStr;
return u;
}
public static void main(String[] args) {
User u = new User();
u.userName = "Niraj";
ObjectMapper m = new ObjectMapper();
String jsonString = m.writeValueAsString(u);
User u1 = m.readValue(jsonString, User.class);
}
}
You don't need to use a @JsonCreator or @JsonValue or a construtor, Jackson will work OTTB with javabean methods: Try this instead: 您不需要使用@JsonCreator或@JsonValue或construtor,Jackson将使用javabean方法处理OTTB:请尝试以下方法:
public class User{
private String username;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public static void main(String[] args) throws Exception {
User u = new User();
u.setUsername("name");
ObjectMapper m = new ObjectMapper();
String userString = m.writeValueAsString(u);
System.out.println(userString);
User u1 = m.readValue(userString, User.class);
System.out.println(u1.getUsername());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.