Compressed JSON parser
Posted: Wed Jan 06, 2016 2:05 am
My work on Station Dock Control has led me to research ways of making JSON less bulky. The way JSON stores arrays of objects is to store the property names and values for every record. That's a lot of repeated text, and in the example of SDC it can be almost 2mb of data.
I came across this article http://stevehanov.ca/blog/index.php?id=104 which has a downloadable example of compressing a JSON string by deriving the schema and storing it independently of the data. The article describes the process better than I just did.
In my tests this halves the space usage of SDC (my 2mb save file was reduced to 1mb), and all the data appears to be present and usable the same as before. Here's a link (https://app.box.com/s/oq8clah7yf28eqe4xjtf7jhnebpvbm5b) to a tweaked version of Steve's code that can be dropped into an OXP project and activated by adding "compressed_json.js" to the worldscripts.plist file, and then changing any instances of "JSON.stringify" or "JSON.parse" to "CJSON.stringify" and "CJSON.parse".
So I have a couple of questions for those who know more about Javascript and the Oolite Javascript object model than I do:
1. Have I broken something somewhere that I'm not aware of? I'm attaching the CJSON objects to the "global" object, but maybe there's a better way. The original code had "window" (as it was designed for web usage), but I'm not sure what's a better alternative in the Oolite object model.
2. Is this of value? Does it matter if save game sizes reach 2mb or more? If it is of value, would it be worth making it a separate OXP? Can you see any pitfalls or gotcha's in the code?
3. The article and code was written 5 years ago, so I'm not sure if there's something better out there. Is there something else I should be looking at?
I don't know if this should be in Expansion Pack or not. Moderators, feel free to move it to a more logical spot.
I came across this article http://stevehanov.ca/blog/index.php?id=104 which has a downloadable example of compressing a JSON string by deriving the schema and storing it independently of the data. The article describes the process better than I just did.
In my tests this halves the space usage of SDC (my 2mb save file was reduced to 1mb), and all the data appears to be present and usable the same as before. Here's a link (https://app.box.com/s/oq8clah7yf28eqe4xjtf7jhnebpvbm5b) to a tweaked version of Steve's code that can be dropped into an OXP project and activated by adding "compressed_json.js" to the worldscripts.plist file, and then changing any instances of "JSON.stringify" or "JSON.parse" to "CJSON.stringify" and "CJSON.parse".
So I have a couple of questions for those who know more about Javascript and the Oolite Javascript object model than I do:
1. Have I broken something somewhere that I'm not aware of? I'm attaching the CJSON objects to the "global" object, but maybe there's a better way. The original code had "window" (as it was designed for web usage), but I'm not sure what's a better alternative in the Oolite object model.
2. Is this of value? Does it matter if save game sizes reach 2mb or more? If it is of value, would it be worth making it a separate OXP? Can you see any pitfalls or gotcha's in the code?
3. The article and code was written 5 years ago, so I'm not sure if there's something better out there. Is there something else I should be looking at?
I don't know if this should be in Expansion Pack or not. Moderators, feel free to move it to a more logical spot.