简体   繁体   English

Jackson ObjectMapper:java.util.Set是按升序排序顺序为json数组吗?

[英]Jackson ObjectMapper: Is java.util.Set serialized to json array in ascending sorted order?

There's an object mapper feature to serialize maps sorted by map keys, but nothing is said about java sets. 有一个对象映射器功能来序列化按地图键排序的地图,但没有任何关于java集的说法。 If I store items in a java.util.Set in my POJO, will the Jackson ObjectMapper serialize them to the json array in any kind of order? 如果我将项目存储在我的POJO中的java.util.Set中,那么Jackson ObjectMapper会以任何顺序将它们序列化为json数组吗?

If not, I'm thinking of writing a custom serializer for the set for this purpose - is there a better way that someone can suggest? 如果没有,我正在考虑为此目的为该集编写一个自定义序列化器 - 有没有更好的方法可以建议?

Update: 更新:

Ultimately, the goal is to deserialize an array of unique integers into some sort of container in a "request" pojo. 最终,目标是在“请求”pojo中将一组唯一整数反序列化为某种容器。 Then process the request and pass that input field into the corresponding output field (unchanged) in a "response" object. 然后处理请求并将该输入字段传递到“响应”对象中的相应输出字段(未更改)。

I'd like to store the data in the POJOs as java.util.Set just to keep all the users happy (if you try to store it as as subtype of Set then consumers of the POJO's interface may be upset that they can't just get a Set but have to expect a HashSet or a TreeSet). 我想将数据存储在POJO中作为java.util.Set只是为了让所有用户都满意(如果你试图将它存储为Set的子类型,那么POJO界面的消费者可能会感到不安,他们不能只需要一个Set但必须要一个HashSet或TreeSet)。

Will this work? 这会有用吗?

  1. json array [5,4,3,2,1] is deserialized into Set BUT, I use @JsonDeserialize(as=TreeSet.class) on the setter method. json数组[5,4,3,2,1]被反序列化为Set BUT,我在setter方法上使用@JsonDeserialize(as = TreeSet.class)。 So the runtime type of the object is now a TreeSet (even though it's stored in the POJO as a Set). 因此,对象的运行时类型现在是TreeSet(即使它作为Set存储在POJO中)。
  2. The Set is passed around through the server and ultimately ends being stored in the response POJO (runtime type is still TreeSet, even though the POJO field type is Set). Set通过服务器传递,最终结束存储在响应POJO中(即使POJO字段类型为Set,运行时类型仍为TreeSet)。
  3. ObjectMapper serializes a TreeSet to json array [1,2,3,4,5] (because TreeSet is sorted naturally). ObjectMapper将TreeSet序列化为json数组[1,2,3,4,5](因为TreeSet是自然排序的)。

Item 3 above is the one I'm wondering about - will ObjectMapper use the runtime type of the POJO's field, or the declared type? 上面的第3项是我想知道的 - ObjectMapper会使用POJO字段的运行时类型,还是声明的类型?

A JSON Array is an ordered collection, exactly like a Java array is: JSON数组是一个有序集合,与Java数组完全相同:

An array is an ordered collection of values. 数组有序的值集合。 An array begins with [ (left bracket) and ends with ] (right bracket). 数组以[ (左括号)开头,以]结尾(右括号)。 Values are separated by , (comma). 值由分开, (逗号)。

However, ordered does not mean sorted . 但是, 有序并不意味着排序 It just means that it retains the order of the given values: 它只是意味着它保留了给定值的顺序

  • If the Set is sorted (eg TreeSet ), then the JSON array is sorted , according to the sort order of the Set : 如果Set已排序(例如TreeSet ),则根据Set的排序顺序对JSON数组进行排序

    TreeSet : The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used. TreeSet :元素按照其自然顺序排序,或者在创建时创建时提供的Comparator ,具体取决于使用的构造函数。

  • If the Set is unordered (eg HashSet ), then the JSON array is in the arbitrary order returned by the Set . 如果Set是无序的(例如HashSet ),则JSON数组处于Set返回的任意顺序。

    HashSet : It makes no guarantees as to the iteration order of the set; HashSet :它不保证集合的迭代顺序; in particular, it does not guarantee that the order will remain constant over time. 特别是,它不保证订单会随着时间的推移保持不变。

This is unlike the fields of a JSON Object: 这与JSON对象的字段不同:

An object is an unordered set of name/value pairs. 对象是一组无序的名称/值对。 An object begins with { (left brace) and ends with } (right brace). 对象以{ (左括号)开头,以}结尾(右大括号)。 Each name is followed by : (colon) and the name/value pairs are separated by , (comma). 每个名称后面跟着: (结肠)和名称/值对被分离的, (逗号)。

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

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