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:
- Use
SPLFixedArray
- Use
string
andpack()
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 string
instead of object
.
I'm wondering if there are better options that don't give memory issues?