Skip to content
Snippets Groups Projects
Commit 971f2e12 authored by mcmlxxix's avatar mcmlxxix
Browse files

add 'reviver' function call to JSON parser in Socket object to convert null values to undefined

parent 7aa636b6
No related branches found
No related tags found
No related merge requests found
......@@ -87,10 +87,16 @@ function JSONClient(serverAddr,serverPort) {
this.callback;
this.updates=[];
/* convert null values to undefined when parsing */
Socket.prototype.reviver = function(k,v) { if(v == null) return undefined; return v; };
/* create new socket connection to server */
this.connect = function() {
this.socket=new Socket();
this.socket.connect(this.serverAddr,this.serverPort,this.settings.CONNECTION_TIMEOUT);
if(!this.socket.is_connected)
throw("error connecting to server");
}
this.disconnect = function() {
......@@ -161,6 +167,8 @@ function JSONClient(serverAddr,serverPort) {
/* receive a data packet */
this.receive=function() {
if(!this.socket.is_connected)
throw("socket disconnected");
if(!this.socket.data_waiting)
return false;
else
......
......@@ -14,7 +14,7 @@ Socket.prototype.ping_sent = 0;
/* socket prototype to automatically encode JSON data */
Socket.prototype.sendJSON = function(object) {
try {
var data=JSON.stringify(object)+"\r\n";
var data=JSON.stringify(object,this.replacer,this.space)+"\r\n";
this.send(data);
log(LOG_DEBUG,"-->" + this.descriptor + ": " + data);
} catch(e) {
......@@ -28,7 +28,7 @@ Socket.prototype.recvJSON = function() {
if(data != null) {
log(LOG_DEBUG,"<--" + this.descriptor + ": " + data);
try {
data=JSON.parse(data);
data=JSON.parse(data,this.reviver);
}
catch(e) {
log(LOG_ERROR,e);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment