V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
soccerlover
V2EX  ›  Java

为什么 Jackson 中代表 json(ObjectNode)的底层数据结构使用的是 LinkedHashMap?

  •  
  •   soccerlover · 2022-08-25 17:48:34 +08:00 · 1662 次点击
    这是一个创建于 582 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这样做是有什么性能或者实践上的考虑吗?

    5 条回复    2022-08-26 10:32:14 +08:00
    cheng6563
        1
    cheng6563  
       2022-08-25 17:50:10 +08:00
    Json 没规定排序规则是吧,那就是按插入顺序排序咯,那不得用 LinkedHashMap 咯。
    lmshl
        2
    lmshl  
       2022-08-25 18:21:44 +08:00
    从完备性角度讲 object 的 key 顺序也是信息
    当然一般业务开发中应当避免依赖顺序,如果需要在页面展示 table column ,也建议在对象内放一个排序或权重值。
    sampeng
        3
    sampeng  
       2022-08-25 18:37:21 +08:00
    我觉得这就很蠢。会让 java 研发误 json 的有排序的。
    FYFX
        4
    FYFX  
       2022-08-25 18:49:05 +08:00
    https://github.com/FasterXML/jackson-databind/issues/85

    Quick answer: LinkedHashMap is there because I think it is good to retain ordering, even if it is insignificant.
    Even if users may abuse it. I just like it that way.

    开发者觉得保持顺序好
    icedir
        5
    icedir  
       2022-08-26 10:32:14 +08:00
    在某些场景下,保持 Json 有序是必要的。
    比如我之前做过一个功能,使用前端输入的 Json 转换为对应的数据行并进行一定的数据补全后返回给前端,这种时候,给用户的数据行按用户输入的 Json 字段顺序,就能减少用户使用上的迷惑。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2847 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 14:30 · PVG 22:30 · LAX 07:30 · JFK 10:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.