Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum as3 adobe JSON string length

I've written Socket Communication server with Java and a AIR programm with AS3, using Socket connection. The communication through socket connection is done with JSON serialization.

Sometimes with really long JSON strungs over socket, AS3 code says that there is a JSON parse error.

Each JSON string I end with end string to let programm know, that it is not the end of the message, so this is not the problem with AIR programm reading the message in parts.

The error occurs only with realy long json string, for example, string with 78031 length. Is there any limits for JSON serialization?

like image 962
LokoTerrorita Avatar asked Nov 12 '22 23:11

LokoTerrorita


1 Answers

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 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
    }
}
like image 174
Sergey Dragan Avatar answered Nov 15 '22 08:11

Sergey Dragan