Created
May 10, 2015 11:22
-
-
Save tomfa/706d10fed78c497731ac to your computer and use it in GitHub Desktop.
JSON to 8-bit-integer parsing (and visa versa)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JSON to Uint8Array parsing and visa versa | |
// (Intended Bluetooth communication on Cordova) | |
var JsonToArray = function(json) | |
{ | |
var str = JSON.stringify(json, null, 0); | |
var ret = new Uint8Array(str.length); | |
for (var i = 0; i < str.length; i++) { | |
ret[i] = str.charCodeAt(i); | |
} | |
return ret | |
}; | |
var binArrayToJson = function(binArray) | |
{ | |
var str = ""; | |
for (var i = 0; i < binArray.length; i++) { | |
str += String.fromCharCode(parseInt(binArray[i])); | |
} | |
return JSON.parse(str) | |
} |
It sounds like you want to reduce network traffic between the server and client. This is well known and optimized problem, and there's various solutions to go for depending on your priorities / most pressing motivation.
- are you trying to learn about computer science?
- are you trying to minimize costs?
- do you consider your time spent on the optimization a cost?
- do you want to minimize experienced latency?
- are you trying to minimize network traffic?
I would not consider doing what you're thinking of in 99.9% of the cases of 2, 3 or 4.
Give me some more info about your situation, and maybe I can give better advice. You could also consider installing Cursor and asking AI - which has better answers and response times than most internet randos like myself
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i don't need payload to be human readable and i thought i coudl benefit some reduction from the payload size. is there any good way to do this that you could suggest? i don't think im the first one to think of such a thing. ive researched grpc but i guess its only used in backend between different services.