Jackson @JsonPropertyOrder 在 2.8.6 版中被忽略 [英] Jackson @JsonPropertyOrder ignored in version 2.8.6

查看:100
本文介绍了Jackson @JsonPropertyOrder 在 2.8.6 版中被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Jackson 2.8.6 中既没有 @JsonPropertyOrder(alphabetic = true) 也没有

In Jackson 2.8.6 neither @JsonPropertyOrder(alphabetic = true) nor

@JsonPropertyOrder(value = {"status", "extraCost", "liability", "advice", "clause"})

..对我的 Java 对象的序列化方式有任何影响.

..have any effect on how my Java object is serialized.

@com.fasterxml.jackson.annotation.JsonPropertyOrder(alphabetic = true)
class MyPremium {
  private String status;
  private BigDecimal extraCost = BigDecimal.ZERO;
  private Premium liability = new Premium();    
  private String advice;
  private List<String> clause;

  // getters and setters omitted
}

随着每个应用程序重新启动,序列化输出中的属性顺序会发生变化.

With every application restart the order of the properties in the serialized output changes.

知道可能是什么问题吗?文档相当模糊.它表明它不适用于 JSON,但我再次看到此注释的许多 examples成功订购 JSON 属性.

Any idea what could be the problem? The documentation is rather vague. It suggests it doesn't work for JSON, but then again I see many examples of this annotation successfully ordering JSON properties.

这个注解对反序列化可能有也可能没有影响:对于基本的 JSON 处理没有效果,但对于其他支持的数据可能存在类型(或结构约定).

This annotation may or may not have effect on deserialization: for basic JSON handling there is no effect, but for other supported data types (or structural conventions) there may be.

注意:属性允许注解,从 2.4 开始,主要是支持 Map 条目的字母顺序.

NOTE: annotation is allowed for properties, starting with 2.4, mostly to support alphabetic ordering of Map entries.

http://static.javadoc.io/com.fasterxml.jackson.core/jackson-annotations/2.8.6/com/fasterxml/jackson/annotation/JsonPropertyOrder.html

推荐答案

文档中提到的反序列化在这里无关紧要——您正在序列化属性.注释绝对适用于 Jackson 2.x.

Documentation's mention of deserialization is irrelevant here -- you are serializing properties. And annotation should definitely work for Jackson 2.x.

然而,您没有包含用于序列化的代码,因此展示它(或解释使用它的框架,如果不是直接使用)可能会有所帮助.

You are not including code you use for serialization, however, so showing it (or explaining what framework is using it if not direct usage) could be helpful.

除此之外,是的,在编写 JSON(或其他支持的格式)时,使用所示的注释应该更改顺序.一种可能性可能是您使用的实际 ObjectMapper 来自 Jackson 1.x(具有不同的注释).或者,如果您不控制序列化,可能框架甚至不使用 Jackson(某些 Web 框架默认使用其他 json 库).

Other than this, yes, use of annotation like shown should change ordering when writing JSON (or other supported formats). One possibility could be that actual ObjectMapper you use came from Jackson 1.x (which has different annotations). Or, if you are not controlling serialization maybe frameworks is not even using Jackson (some web frameworks default to other json libraries).

这篇关于Jackson @JsonPropertyOrder 在 2.8.6 版中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆