I create a lot of different APIs in my work, several of them send out fairly large arrays of objects where the property names of these objects are the bulk of the data being transfered. This is mostly mitigated by compressing the data, but I figured, why not try and see if I can shrink it down a bit more. Most of the APIs are consumed on mobile devices and every second counts. This means that any byte we can save over the line has to be weighed against the cost of reconstructing the objects on the client.
My proposed format is to send the definition of an object as the first item of an array, with every actual item being sent as a subarray with the data in the same order as the properties in the definition. Something like this:
Normal JSON server code:
This is just a normal ASP.NET api where I create 5000 strongly typed objects and returns them as a list that is then serialized to JSON and sent to the client.
Compact JSON server code:
This code creates the same data but we send the object definition as the first item in the array, and every object as a subarray only containing the data.This means that we can only send objects with the same definition this way, but that covers 90% of my endpoints. The code shown here is only a mock that creates the data for measure, a live example would user a nice serializer that could take any type of object.
This code removes the definition from the received array and uses it to in place replace every subarray containing the objects data with real objects.
###So what does this work give us?
- 5000 items normal JSON, GZIPed ~118KB, no compression ~572KB
- 5000 items compact JSON array, GZIPed ~88KB, no compression ~187KB
I have tried a few different configurations and the saving is ~25% in most cases, this will not hold true if your objects contains lots of data as the property:data ratio will be different, but it produces significant savings in many of the APIs that I create.
These tests show that serializing data like this can improve performance in applications that send out lots of items containing very small data.