Maximum as3 adobe JSON string length Maximum as3 adobe JSON string length json json

Maximum as3 adobe JSON string length


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

The point is that Flash ProgressEvent.SOCKET_DATA event fires even when server didn't send all the data, and something is left (especially when the data is big and the connection is slow).So something like {"key":"value"} comes in two (or more) parts, like: {"key":"val and ue"}. Also sometimes you might receive several joined JSONs in one message like {"json1key":"value"}{"json2key":"value"} - built-in Flash JSON parser cannot handle these too.

To fight this I recommend you to modify your SocketData handler in the Flash app to add a cache for received strings. Like this:

// declaring varsprivate 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    }}


According to Adobe, it appears that you are not facing a JSON problem but instead a Socket limitation.

A String you may send over a Socket via writeUTF and readUTF is limited by 65,535 bytes. This is due to the string being prepended with a 16 bit unsigned integer rather than a null terminated string.