doushangan3690 2019-06-04 12:29
浏览 98

比JSON更好的序列化格式? 快速查找和保存键的顺序? [重复]

I am using PHP, but question is not entirely PHP specific.

I've got a need to serialize object, current I am using JSON format and this could be example of serialized object: {"22":{"val":"e","gIds":["48"]},"23":{"val":"e","gIds":["235"]}}

Given this format I can simply use PHP's : $array = json_decode($string, true), then I can use either isset($array[22]) or array_key_exists(22, $array) for quick lookups, which I think would be equivalent to hashmap / dictionary in different languages.

Unfortunately the JSON format by default doesn't keep the order of properties within the object (https://stackoverflow.com/a/7214312/2263395). The order might be consistent in the same version of PHP, but it's not guaranteed behavior according to JSON standard.

I could make it a list of nested objects: [{"22":{"val":"e","gIds":["48"]}},{"23":{"val":"e","gIds":["235"]}}] or even move the id inside: [{"id": "22","val":"e","gIds":["48"]},{"id": "23","val":"e","gIds":["235"]}] That way I can preserve order, however each I can no longer use the quick lookup functions like: isset or array_key_exists before I create a hashmap. For JSONs having thousands of items, I would have to iterate that many times in order to prepare a hashmap, so I can use the quick lookup function.

Can I have the both order and speed, is there any format that could do that?

Speaking of PHP options is there any performant competitor?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 求解达问题(有红包)
    • ¥15 请解包一个pak文件
    • ¥15 不同系统编译兼容问题
    • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
    • ¥30 数字电源对DSP芯片的具体要求
    • ¥20 antv g6 折线边如何变为钝角
    • ¥30 如何在Matlab或Python中 设置饼图的高度
    • ¥15 nginx中的CORS策略应该如何配置
    • ¥30 信号与系统实验:采样定理分析
    • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao