我尝试编写自定义jackson反序列化器.我希望"查看"一个字段并对类执行自动反序列化,请参阅下面的示例:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.mypackage.MyInterface;
import com.mypackage.MyFailure;
import com.mypackage.MySuccess;
import java.io.IOException;
public class MyDeserializer extends JsonDeserializer<MyInterface> {
@Override
public MyInterface deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectCodec codec = jp.getCodec();
JsonNode node = codec.readTree(jp);
if (node.has("custom_field")) {
return codec.treeToValue(node, MyFailure.class);
} else {
return codec.treeToValue(node, MySuccess.class);
}
}
}
Run Code Online (Sandbox Code Playgroud)
的POJO:
public class MyFailure implements MyInterface {}
public class MySuccess implements MyInterface {}
@JsonDeserialize(using = MyDeserializer.class)
public interface MyInterface …Run Code Online (Sandbox Code Playgroud) 我有一个模型,当从前端发送请求时,我在控制器中使用 @Valid 进行验证:
@NotNull
@Size(min=1, message="Name should be at least 1 character.")
private String name;
@NotNull
@Pattern(regexp = "^https://github.com/.+/.+$", message = "Link to github should match https://github.com/USER/REPOSITORY")
private String github;
Run Code Online (Sandbox Code Playgroud)
但现在我也在用杰克逊的 ObjectMapper 创建一个没有控制器的对象。有没有办法在 ObjectMapper 中注册这个验证,或者我应该只检查 setter 中的变量?
我有一个 JSON 字符串,其中包含嵌套和包装的 JSON 字符串。我想使用杰克逊反序列化它,但我不确定如何。这是一个示例 bean:
@JsonIgnoreProperties(ignoreUnknown = true)
public class SomePerson {
public final String ssn;
public final String birthday;
public final Address address;
@JsonCreator
public SomePerson(
@JsonProperty("ssn") String ssn,
@JsonProperty("birthday") String birthday,
@JsonProperty("data") Address address,
@JsonProperty("related") List<String> related) {
this.ssn = ssn;
this.birthday = birthday;
this.address = address;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Address {
public final String city;
public final String country;
@JsonCreator
public Address(
@JsonProperty("city") String city,
@JsonProperty("country") String country) {
this.city = city; …Run Code Online (Sandbox Code Playgroud) 我有一个有时看起来像这样的对象:
{
"foo" : "bar",
"fuzz" : "bla"
}
Run Code Online (Sandbox Code Playgroud)
有时看起来像这样:
{
"foo" : { "value" : "bar", "baz": "asdf" },
"fuzz" : { "thing" : "bla", "blip" : "asdf" }
}
Run Code Online (Sandbox Code Playgroud)
这些类看起来像:
public class Foo {
String value;
String baz;
}
public class Fuzz {
String thing;
String blip;
}
Run Code Online (Sandbox Code Playgroud)
第一种情况是第二种情况的简写。我想总是反序列化到第二种情况。
此外 - 这是我们代码中非常常见的模式,因此我希望能够以通用方式进行序列化,因为还有其他与Foo上述类似的类具有使用 String 作为语法糖的相同模式更复杂的对象。
我想使用它的代码看起来像这样
public class Thing {
@JsonProperty("fuzz")
Fuzz fuzz;
@JsonProperty("foo")
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
我如何编写一个通常处理这两种情况的自定义解串器(或其他一些模块)?
我有以下三个应用程序:
项目1举行
项目二
项目3
项目1的类如下所示:
public interface IDemoEntity {
String getName();
void setName(String name);
}
@Component
public class StoreFunction implements Consumer<Message<IDemoEntity>> {
@Override
public void accept(Message<IDemoEntity> t) {
System.out.println("Stored entity " + t.getPayload().getName());
return;
}
}
Run Code Online (Sandbox Code Playgroud)
对于项目2,IDemoEntity的实现如下所示:
@DynamoDBTable(tableName = "DemoEntity")
public class DynamoDemoEntity implements IDemoEntity {
private String name;
@Override
@DynamoDBHashKey
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
} …Run Code Online (Sandbox Code Playgroud) 我无法找出使用杰克逊实现自定义序列化/反序列化的正确方法。我有许多带有原始字段的类(~50),这些字段应该被序列化/反序列化,而不是作为原始字段。喜欢:
class User {
int height // this field should be serialized as "height": "10 m"
}
class Food {
int temperature // this field should be serialized as "temperature": "50 C"
}
class House {
int width // this field should be serialized as "width": "10 m"
}
Run Code Online (Sandbox Code Playgroud)
所有序列化和反序列化都非常相似,我只需要在整数后面添加一个后缀(C、页、米等..)
执行此操作的一种简单方法是将一对@JsonSerialize/@JsonDeserialize注释添加到每个此类字段并实现它们。但我最终会得到 100 个非常相似的序列化器/反序列化器。
我考虑过向每个字段(例如@Units("Degree")或@Units("Meters"))添加自定义注释到此类整数字段,并实现 ,SerializationProvider它将基于注释值以通用方式创建序列化器。但我没有找到提供有关属性注释信息的地方。