soccerlover
V2EX  ›  Java

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

  •  
  •   soccerlover · Aug 25, 2022 · 2758 views
    This topic created in 1531 days ago, the information mentioned may be changed or developed.

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

    5 replies  •  2022-08-26 10:32:14 +08:00
    cheng6563
    cheng6563
    Aug 25, 2022
    1
    Json 没规定排序规则是吧,那就是按插入顺序排序咯,那不得用 LinkedHashMap 咯。
    lmshl
    lmshl
    Aug 25, 2022
    2
    从完备性角度讲 object 的 key 顺序也是信息
    当然一般业务开发中应当避免依赖顺序,如果需要在页面展示 table column ,也建议在对象内放一个排序或权重值。
    sampeng
    sampeng
    Aug 25, 2022
    3
    我觉得这就很蠢。会让 java 研发误 json 的有排序的。
    FYFX
    FYFX
    Aug 25, 2022
    4
    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
    icedir
    Aug 26, 2022
    5
    在某些场景下,保持 Json 有序是必要的。
    比如我之前做过一个功能,使用前端输入的 Json 转换为对应的数据行并进行一定的数据补全后返回给前端,这种时候,给用户的数据行按用户输入的 Json 字段顺序,就能减少用户使用上的迷惑。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1741 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 17:29 · PVG 01:29 · LAX 09:29 · JFK 12:29
    ♥ Do have faith in what you're doing.