The problem is that sending large serialized JSON (over 16,000 characters) over a network socket is split into pieces. Each piece fires an event dataon the receiving side. Thus, simply starting JSON.parse()on incoming data may fail SyntaxError: Unexpected end of input.
The work that I still managed to find is to add a null character ( '\u0000') to the end of the serialized JSON and check it on the receiving side. Here is an example:
var partialData = '';
client.on( 'data', function( data ) {
data = data.toString();
if ( data.charCodeAt( data.length - 1 ) !== 0 ) {
partialData += data;
return;
} else {
partialData += data.substr( 0, data.length - 1 );
}
workWithData( JSON.parse( partialData ));
partialData = '';
});
One of the failures of this model is that the receiver is connected to several sockets, and each socket sends large JSON files.
, , - , , , . , . , : -, JSON Node.js? -, , , JSON ?