Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastjson2 在对LinkedHashMap做toJSONString时指定JSONWriter.Feature.MapSortField无效 #2318

Closed
ztgoto opened this issue Mar 12, 2024 · 8 comments
Labels
bug Something isn't working fixed question Further information is requested
Milestone

Comments

@ztgoto
Copy link

ztgoto commented Mar 12, 2024

使用2.0.15

Map<Object, Object> m= new LinkedHashMap<>();
m.put("abc", 1);
m.put("123", 2);
System.out.println(JSON.toJSONString(m,JSONWriter.Feature.MapSortField));

输出
{"abc":1,"123":2}
key并未进行排序

@ztgoto ztgoto added the question Further information is requested label Mar 12, 2024
@rowstop
Copy link
Contributor

rowstop commented Mar 13, 2024

这个结果不正是排序后的吗?底层是 Treemap 的自然排序

@ztgoto ztgoto changed the title fastjson2 在对HashMap做toJSONString时指定JSONWriter.Feature.MapSortField无效 fastjson2 在对LinkedHashMap做toJSONString时指定JSONWriter.Feature.MapSortField无效 Mar 15, 2024
@ztgoto
Copy link
Author

ztgoto commented Mar 15, 2024

这个结果不正是排序后的吗?底层是 Treemap 的自然排序

我demo复制错了,应该是LinedHashMap 指定了还是按放入顺序输出,未排序

@rowstop
Copy link
Contributor

rowstop commented Mar 15, 2024

lin

这个结果不正是排序后的吗?底层是 Treemap 的自然排序

我demo复制错了,应该是LinedHashMap 指定了还是按放入顺序输出,未排序

LinkedHashMap是按存放顺序排列的,你如果想按照 key 排序 就用 HashMap 别用LinkedHashMap

@ztgoto
Copy link
Author

ztgoto commented Mar 15, 2024

lin

这个结果不正是排序后的吗?底层是 Treemap 的自然排序

我demo复制错了,应该是LinedHashMap 指定了还是按放入顺序输出,未排序

LinkedHashMap是按存放顺序排列的,你如果想按照 key 排序 就用 HashMap 别用LinkedHashMap

我觉得这种设计就很奇怪,既然我用这个工具都需要使用方自己选数据类型决定排序了,那我干嘛不直接选用个TreeMap, 这个序列化的Feature参数有什么意义?或者干脆就不要叫MapSortField
比如我业务controller接受一个Map 要做排序签名,那我还得判断这个是否是LinkedHashMap?
jackson json 的配置SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS就没这个问题。没搞懂这么设计的原因

ztgoto added a commit to ztgoto/fastjson2 that referenced this issue Mar 15, 2024
@ztgoto
Copy link
Author

ztgoto commented Mar 15, 2024

@rowstop 我主要有几个疑问

  1. 既然feature叫MapSortField,理应对所有Map生效,不然会有歧义。
  2. 我不知道什么原因要排除LinkedHashMap的排序特性,但如果不同的Map有不同的排序表现,那用户侧就能通过传入的类型来决定是否排序了,那这个feature又有什么意义
  3. 更奇怪的是代码中只对LinkedHashMap不进行排序,为什么LinkedHashMap的子类又支持排序了,就算基于什么原因考虑LinkedHashMap不支持MapSortField,那不是也应该LinkedHashMap的子类也不支持这个feature么。jackson json 的配置SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS对Map的表现就比较一致

@rowstop
Copy link
Contributor

rowstop commented Mar 16, 2024

lin

这个结果不正是排序后的吗?底层是 Treemap 的自然排序

我demo复制错了,应该是LinedHashMap 指定了还是按放入顺序输出,未排序

LinkedHashMap是按存放顺序排列的,你如果想按照 key 排序 就用 HashMap 别用LinkedHashMap

我觉得这种设计就很奇怪,既然我用这个工具都需要使用方自己选数据类型决定排序了,那我干嘛不直接选用个TreeMap, 这个序列化的Feature参数有什么意义?或者干脆就不要叫MapSortField 比如我业务controller接受一个Map 要做排序签名,那我还得判断这个是否是LinkedHashMap? jackson json 的配置SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS就没这个问题。没搞懂这么设计的原因

不用判断 你可以直接 new HashMap<>(map)就可以

@wenshao
Copy link
Member

wenshao commented Mar 17, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.48-SNAPSHOT/

我增加了一个Feature#SortMapEntriesByKeys,排序会包括LinkedHashMap,并且将MapSortField设置为deprecated。

请帮忙用2.0.48-SNAPSHOT验证,2.0.48正式版本预计在月末发布

@wenshao wenshao added the bug Something isn't working label Mar 17, 2024
@wenshao wenshao added this to the 2.0.48 milestone Mar 17, 2024
@wenshao wenshao added the fixed label Mar 17, 2024
@wenshao
Copy link
Member

wenshao commented Mar 25, 2024

@wenshao wenshao closed this as completed Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants