首页 \ 问答 \ Jackon JSON:子类未知时的多态解除(Jackon JSON: Polymorphic deseralization when subclasses are unknown)

Jackon JSON:子类未知时的多态解除(Jackon JSON: Polymorphic deseralization when subclasses are unknown)

我正在尝试使用Jackson对JSON进行一些多态的deseralization,但是子类列表在编译时是未知的,所以我不能在基类上使用@JsonSubtype注释。 相反,我想使用TypeIdResolver类来执行与属性值的转换。

我可能遇到的可能子类列表将是动态的,但它们都是在运行时使用注册表注册的。 所以我似乎需要我的TypeIdResolver对象来引用该注册表类。 它必须在基本上是一个依赖注入环境中运行(即我不能有TypeIdResolver参考的单例类),所以我认为我需要将注册表类注入TypeIdResolver。 我认为我想写的代码是:

ObjectMapper mapper = new ObjectMapper();
mapper.something(new MyTypeIdResolver(subclassRegistry));
mapper.readValue(...)

但是,我找不到在中间做位的方法。 我能找到的唯一方法是使用java注释来指定TypeIdResolver将是什么。

这个问题有没有办法在mapper配置而不是注释上指定@JsonTypeIdResolver? 是相同的,虽然动机不同,答案是使用注释mixin,这在这里不起作用。


I'm trying to do some polymorphic deseralization of JSON using Jackson, however the list of subclasses is unknown at compile time, so I can't use a @JsonSubtype annotation on the base class. Instead I want to use a TypeIdResolver class to perform the conversion to and from a property value.

The list of possible subclasses I might encounter will be dynamic, but they are all registered at run time with a registry. So I would appear to need my TypeIdResolver object to have a reference to that registry class. It has to operate in what is essentially a dependency injection environment (i.e I can't have a singleton class that the TypeIdResolver consults), so I think I need to inject the registry class into the TypeIdResolver. The kind of code I think I want write is:

ObjectMapper mapper = new ObjectMapper();
mapper.something(new MyTypeIdResolver(subclassRegistry));
mapper.readValue(...)

However, I can't find a way of doing the bit in the middle. The only methods I can find use java annotations to specify what the TypeIdResolver is going to be.

This question Is there a way to specify @JsonTypeIdResolver on mapper config instead of annotation? is the same, though the motivation is different, and the answer is to use an annotation mixin, which won't work here.


原文:https://stackoverflow.com/questions/37793134
更新时间:2023-09-20 15:09

最满意答案

问题似乎在于你调用动画的地方。 我改变了你的CSS选择器,以便在悬停时执行此操作(以便每次悬停时动画都会发生)并对-moz-animation属性进行调整以包含更多值,对此:

#primaryNav li#menu-item-17:hover > ul.sub-menu {
    animation:sub-menu-anim-prod 0.5s;
    -moz-animation: 0.5s ease 0s normal none 1 sub-menu-anim-prod;
    -webkit-animation: sub-menu-anim-prod 0.5s; /* Safari and Chrome */
    -o-animation: sub-menu-anim-prod 0.5s; /* Opera */
}

这似乎有效。 我在Firefox和Chrome中检查了它。 我还更新了其他选择器以包含我上面所做的。 请检查这个小提琴是否有其他变化。


The problem seemed to be with where you were calling the animation. I changed your CSS selector to do it on the hover (so that the animation happens each time you hover) and made an adjustment to the -moz-animation property to include more values, to this:

#primaryNav li#menu-item-17:hover > ul.sub-menu {
    animation:sub-menu-anim-prod 0.5s;
    -moz-animation: 0.5s ease 0s normal none 1 sub-menu-anim-prod;
    -webkit-animation: sub-menu-anim-prod 0.5s; /* Safari and Chrome */
    -o-animation: sub-menu-anim-prod 0.5s; /* Opera */
}

This seems to work. I have checked it in Firefox and Chrome. I also update other selectors to incorporate what I have done above. Please check this fiddle for the rest of the changes.

相关问答

更多
  • 您需要在其中包含浏览器特定的关键帧动画以及其浏览器特定的转换 @-webkit-keyframes pulsate { 0% { -webkit-transform: scale(0.8, 0.8); opacity: 0.3; } 50% { -webkit-transform: scale(1, 1); opacity: 1.0; } 100% { -webkit-tr ...
  • 问题似乎在于你调用动画的地方。 我改变了你的CSS选择器,以便在悬停时执行此操作(以便每次悬停时动画都会发生)并对-moz-animation属性进行调整以包含更多值,对此: #primaryNav li#menu-item-17:hover > ul.sub-menu { animation:sub-menu-anim-prod 0.5s; -moz-animation: 0.5s ease 0s normal none 1 sub-menu-anim-prod; -webkit- ...
  • 从动画名称中删除双引号( rotate而不是"rotate" )。 然后它会工作: 运行演示 -webkit-animation-duration: 4s; -webkit-animation-timing-function: linear; -webkit-animation-name:rotate; -webkit-animation-iteration-count: infinite; -moz-animation-duration: 4s; -moz-animation-timing-functi ...
  • 因为你在loadbars周围有引号( '' )。 删除它们,它会工作。 更新了codepen Because you have quotation marks('') around loadbars. Remove them and it will work. Updated codepen
  • 我相信你会想要一个纯粹的CSS解决方案,但现在这是不可能的。 许多浏览器仍然没有达到新的CSS功能。 我建议jQuery为您的解决方案。 API中有几个函数,如slideIn(),fadeIn(),fadeOut(),。toggle()等... 使用这些函数就像等待DOM准备就绪一样简单,然后将您的类应用于您想要的效果。 下面是一个简单的例子。