Stream Analyzers (JSON / XML) for AS3 / Flex / Adobe AIR Applications

Are there AS3 libraries for reading streams that parse libraries for json or xml formats? I am setting up a long polling application using URLStream / URLRequest. I have no control over the data that I receive, except for the choice between formats. I would like to have a parser that can process fragments at a time, which will allow me to fire custom events when certain full fragments become available. Thoughts? What are current AIR applications to handle this?

API Example:

var decoder:StreamingJSONDecoder = new StreamingJSONDecoder();
decoder.attachEvent("onobjectavailable", read_object); 

while (urlStream.bytesAvailable) 
{
  decoder.readBytes(get_bytes(urlStream)); 
}
+3
source share
3 answers
+1

AIR (v2.5) WebKit JSON JSON.stringify() JSON.parse().

+1

You could use a URLStream instance to gradually download data from a remote network, and then decode the JSON result when enough data is available.

Something like this (not tested, just to give you an idea):

var stream:URLStream = new URLStream();
stream.addEventListener( ProgressEvent.PROGRESS, handleProgress );
stream.load( new URLRequest( "/path/to/data" ) );

function handleProgress( event:ProgressEvent ):void
{
    // Attempt to read as much from the stream as we can at this point in time
    while ( stream.bytesAvailable )
    {
        // Look for a JSONParseError if the JSON is not complete or not
        // encoded correctly.
        // Look for an EOFError is we can't read a UTF string completely
        // from the stream.
        try
        {
            var result:* = JSON.decode( stream.readUTF() );
            // If we're here, we were able to read at least one decoded JSON object
            // while handling this progress event
        }
        catch ( e:Error )
        {
            // Can't read, abort the while loop and try again next time we
            // get download progress.
            break;
        }
    }   
}
0
source

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


All Articles