V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
soccerlover
V2EX  ›  Java

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

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

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

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