多态对象的JSON使用者

编程入门 行业动态 更新时间:2024-10-27 22:21:57
本文介绍了多态对象的JSON使用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在解析JSON,并且遇到一个可能有三种形式之一的结构有困难。在我的情况下,它可以是零维,一维或二维。有什么方法可以动态检查JSON以确定它是哪一个?或者也许消费它然后计算出它是什么。

I am parsing JSON and am having difficulty with one structure that can have one of three forms. In my case it could be zero-dimensional, one-dimensional or two-dimensional. Is there some way I can inspect the JSON on the fly to determine which one it is? Or perhaps consume it anyway and work out what it is afterwards.

结构看起来像这样,可以嵌入到其他结构中。

The structures look like this and can be embedded in other structures.

"details":{ "Product":"A zero-dimensional Product" }, "details":{ "Product":"A one-dimensional Product", "Dimensions": [ "Size" ], "Labels": [ "XS", "S", "M", "L" ] }, "details":{ "Product":"A two-dimensional Product", "Dimensions": [ "Size", "Fit" ], "Labels": [[ "XS", "S", "M", "L" ],[ "26", "28", "30", "32" ]] }

我可能正在寻找的是通用的杰克逊将永远匹配的课程。

What I may be looking for is a generic class that Jackson will always match with.

翻译的内容:

{ "SomeField": "SomeValue", ... "details":{ ... } }

进入:

class MyClass { String SomeField; ... AClass details; }

是否有类 AClass 我可以定义它可以是任何JSON结构或数组的通用接收者吗?

Is there a class AClass I can define that could be a universal recipient for any JSON structure or array?

推荐答案

感谢Eric的评论指向我 programmerbruce 我成功破解了它。这是我使用的代码(减少简化)。

Thanks to Eric's comment pointing me to programmerbruce I managed to crack it. Here's the code I used (cut down to simplify).

public static class Info { @JsonProperty("Product") public String product; // Empty in the 0d version, One entry in the 1d version, two entries in the 2d version. @JsonProperty("Dimensions") public String[] dimensions; } public static class Info0d extends Info { } public static class Info1d extends Info { @JsonProperty("Labels") public String[] labels; } public static class Info2d extends Info { @JsonProperty("Labels") public String[][] labels; } public static class InfoDeserializer extends StdDeserializer<Info> { public InfoDeserializer() { super(Info.class); } @Override public Info deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Class<? extends Info> variantInfoClass = null; ObjectMapper mapper = (ObjectMapper) jp.getCodec(); ObjectNode root = (ObjectNode) mapper.readTree(jp); // Inspect the `diemnsions` field to decide what to expect. JsonNode dimensions = root.get("Dimensions"); if ( dimensions == null ) { variantInfoClass = Info0d.class; } else { switch ( dimensions.size() ) { case 1: variantInfoClass = Info1d.class; break; case 2: variantInfoClass = Info2d.class; break; } } if (variantInfoClass == null) { return null; } return mapper.readValue(root, variantInfoClass); } }

并在 ObjectMapper :

// Register the special deserializer. InfoDeserializer deserializer = new InfoDeserializer(); SimpleModule module = new SimpleModule("PolymorphicInfoDeserializerModule", new Version(1, 0, 0, null)); module.addDeserializer(Info.class, deserializer); mapper.registerModule(module); factory = new JsonFactory(mapper);

更多推荐

多态对象的JSON使用者

本文发布于:2023-08-04 00:55:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用者   对象   多态   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!