简体   繁体   English

如何在 Jackson 自定义 JSON 反序列化器中使用常规 JSON 解释

[英]How to use regular JSON interpretation inside of a Jackson custom JSON deserializer

I am writing a custom deserializer and not sure how to best handle the following case:我正在编写自定义解串器,但不确定如何最好地处理以下情况:

I am deserializing something that looks like this:我正在反序列化看起来像这样的东西:

{
  "someProp": {
    "name": "some string"
    "value": ****** can be anything here, including string, number, object, etc. ******
  }
}

The object I am mapping this to has this:我映射到的对象有这个:

public class ObjectToMapTo {
    String name;
    Object value;
}

In my code, I have the JsonNode that represents the someProp field.在我的代码中,我有代表someProp字段的JsonNode I can obviously extract the JsonNode of the name and easily convert it to String with JsonNode.asText() .我显然可以提取nameJsonNode并使用JsonNode.asText()轻松将其转换为String

However, how do I deal with the value ?但是,我如何处理value JsonNode does NOT have a method for asObject() . JsonNode没有asObject()的方法。 Surprisingly, it only has isObject() .令人惊讶的是,它只有isObject() Do I have to go through a switch statement to figure out what type of of a thing it is just so I can call the correct method like asLong() or asText() ?我是否必须通过switch语句来确定它是什么类型的东西,以便我可以调用正确的方法,如asLong()asText()

But even if I do the switch statement, what if the nodeType is Object .但即使我执行switch语句,如果nodeTypeObject怎样。 Then what?然后呢? How do I get that JsonNode to just be a regular Java Map like it would regularly if I were to use the ObjectMapper without a custom deserializer?如果我在没有自定义反序列化器的情况下使用ObjectMapper我如何让JsonNode只是一个常规的 Java Map

I am afraid you have to do some custom deserializing and switching/iffing here.恐怕您必须在这里进行一些自定义反序列化和切换/验证。

JsonNode does NOT have a method for asObject(). JsonNode 没有 asObject() 方法。 Surprisingly, it only has isObject().令人惊讶的是,它只有 isObject()。

Yes.是的。 It is not a problem to determine if the value is an object since object nodes start with { .由于对象节点以{开头,因此确定该值是否为对象不是问题。 But to what specific class instance it can be constructed is impossible to guess.但是它可以构造到什么特定的类实例是无法猜测的。 That should not be surprising.这应该不足为奇。

what if the nodeType is Object.如果 nodeType 是 Object 怎么办。 Then what?然后呢? How do I get whatever is specified for value in JSON如何获取 JSON 中为值指定的任何内容

If there was a field - say type - that tells the type you could do it easily in your custom deserializer.如果有一个字段 - 比如说type - 告诉你可以在自定义反序列化器中轻松完成的类型。 So if you have power to add it then you could give there the whole class name to instantiate to in your deserializer.因此,如果您有权添加它,那么您可以在那里提供整个类名以在您的反序列化器中实例化。

Otherwise I think the best you can do would be to instantiate arbitrary object as an instance of a Map or just as a TreeNode .否则,我认为您能做的最好的事情就是将任意对象实例化为Map的实例或仅作为TreeNode实例化。

You could annotate field value for the deserialiser like (so not the parent):您可以为反序列化器注释字段value (因此不是父级):

@Getter @Setter
public class ObjectToMapTo {
    private String name;
    @JsonDeserialize(using =  MyDeserializer.class)
    private Object value;
}

In your deserializer after you have checked that it is not array nor value, something like:在您检查它不是数组或值之后,在您的反序列化器中,例如:

Object o;
TreeNode valueTreeNode = jp.readValueAsTree();
// check if array or valueNode
// but if is object
ObjectMapper om = new ObjectMapper();
o = om.treeToValue(valueTreeNode, Map.class);
return o;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM