Maximum length of string as3 adobe JSON

I wrote a Socket Communication server with Java and an AIR program with AS3 using a Socket connection. Communication through socket connections is done using JSON serialization.

Sometimes with very long JSON strung on a socket, AS3 code says there is a JSON parsing error.

Each JSON line ends with an end line to tell the program that this is not the end of the message, so this is not a problem with the AIR program reading the message in parts.

The error occurs only with a real long json string, for example a string of length 78031. Are there any restrictions for serializing JSON?

+4
source share
2 answers

According to Adobe , it looks like you are not facing a JSON problem, but instead a Socket restriction.

The string you can send via Socket via writeUTF and readUTF is limited to 65.535 bytes . This is because the string is added with a 16-bit unsigned integer, and not with a null terminated string.

0
source

I had the same problem. The problem is reading Flash data from the socket.

The fact is that the Flash ProgressEvent.SOCKET_DATA event is triggered even if the server did not send all the data and something remains (especially when the data is large and the connection is slow). So, something like {"key": "value"} comes in two (or more) parts, for example: {"key": "val and ue"} . Also sometimes you can get several attached JSON in one message, for example {"json1key": "value"} {"json2key": "value"} - the built-in Flash JSON parser will also not be able to process them.

To combat this, I recommend that you modify the SocketData handler in your Flash application to add a cache for the received strings. Like this:

// declaring vars private var _socket:Socket; private var _cache: String = ""; // adding EventListener _socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); private function onSocketData(e: Event):void { // take the incoming data from socket var fromServer: ByteArray = new ByteArray; while (_socket.bytesAvailable) { _socket.readBytes(fromServer); } var receivedToString: String = fromServer.toString(); _cache += receivedToString; if (receivedToString.length == 0) return; // nothing to parse // convert that long string to the Vector of JSONs // here is very small and not fail-safe alghoritm of detecting separate JSONs in one long String var jsonPart: String = ""; var jsonVector: Vector.<String> = new Vector.<String>; var bracketsCount: int = 0; var endOfLastJson: int = 0; for (var i: int = 0; i < _cache.length; i++) { if (_cache.charAt(i) == "{") bracketsCount += 1; if (bracketsCount > 0) jsonPart = jsonPart.concat(_cache.charAt(i)); if (_cache.charAt(i) == "}") { bracketsCount -= 1; if (bracketsCount == 0) { jsonVector.push(jsonPart); jsonPart = ""; endOfLastJson = i; } } } // removing part that isn't needed anymore if (jsonVector.length > 0) { _cache = _cache.substr(endOfLastJson + 1); } for each (var part: String in jsonVector) { trace("RECEIVED: " + part); // voila! here is the full received JSON } } 
0
source

Source: https://habr.com/ru/post/1496819/


All Articles