duannai1883 2014-01-09 13:12
浏览 440
已采纳

如何将大对象/数组序列化为JSON

My app needs to produce json of an object that has a large data property of type array. The array needs to remain in memory as it collects DB output and some properties can only be determined once the array is completed.

Complication: the array is numerically-based and must appear as such in the json output, therefore straight json_encode() is not an option.

To make this possible on low-spec machines like RasPi I've looked into trimming memory consumption:

  1. Use SPLFixedArray
  2. Use string and pack()

Both approaches take care of the array storage memory problem but fail when it comes to encoding in JSON.

I've looked into implementing JsonSerializable but as it forces users to return the result which is then encoded in Json I'm back to

public function jsonSerialize() {
    return $this->toArray();
}

which has the same memory problems.

zendframework/Component_ZendJson looks promising as it looks for objects having a toJson() method to provide their own encoding as stringinstead of object.

I'm wondering if there are better options that don't give memory issues?

  • 写回答

2条回答 默认 最新

  • dql1978dql1978 2014-05-29 10:57
    关注

    In my investigation I've looked at 5 different approaches for storing large arrays of tuples in memory, summarized here with their results (sampled at 50k records):

    1. Naive

      Exporting json is straightforward with json_encode using array(array(), array())

      Memory: 18.5MB (huge)
      Time: ~100ms to build and dump the array (Windows PC)

    2. SPL Library

      This approach stores everything in nested SPLFixedArrays: SPLFixedArray[SPLFixedArray]. JSON export was done extending Zend\Json\Encoder by implementing the toJson method.

      Memory: 15.5MB (still large)
      Time: ~1.3s, x10 slower

    3. SPL Library

      Similar to 2, but instead of the inner SPLFixedArray uses packed strings from PHP's pack() function.

      Memory: 3.5MB (5 times smaller)
      Time: ~1.3s, x10 slower - apparently pack() is similarly slow as nested array.

    4. SPL Library

      Similar to 2, but instead of the inner SPLFixedArray the actual Tuples are simply written as sequential values to the root array.

      Memory: 3.25MB (again smaller)
      Time: ~0.7s, only x6 slower - do we have a winner here?

    5. pack()

      Similar to 3, but instead of the root SPLFixedArray pack everything into a single string using PHP's pack() function. This does obviously need knowledge about and a fixed, identical structure of the individual arrays.

      Memory: 1.25MB (really small - only 1/12th of original memory)
      Time: ~1.7s, x16 slower

    CONCLUSION

    While (5) offers best memory utilization it is also extremely slow. For my purposes I've settled on (4) which is about 20% of original memory consumption but- when JSON encoding is taken into account- also 5~6 times slower. An acceptable compromise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序