Example usage for org.json.simple.parser ParseException ERROR_UNEXPECTED_CHAR

List of usage examples for org.json.simple.parser ParseException ERROR_UNEXPECTED_CHAR

Introduction

In this page you can find the example usage for org.json.simple.parser ParseException ERROR_UNEXPECTED_CHAR.

Prototype

int ERROR_UNEXPECTED_CHAR

To view the source code for org.json.simple.parser ParseException ERROR_UNEXPECTED_CHAR.

Click Source Link

Usage

From source file:com.conwet.xjsp.json.JSONUtil.java

/**
 * Extracts from the buffer a whole stanza or an stream finalizer "]}".
 * The recognized text is removed from the buffer.
 * /*from w  w w .j av  a2 s.c om*/
 * Stanza recognition is implemented as a FSM. States:
 * <ul>
 *      <li>0. starting</li>
 *      <li>1. accepting text</li>
 *      <li>2. within an string</li>
 *      <li>3. finalizer</li>
 * </ul>
 * 
 * <img src="../../../../../resources/fsm.png"/>
 * 
 * @return Recognized text or <code>null</code>
 */
public static String extractStanza(StringBuilder buffer) throws ParseException {
    discardSpaces(buffer);

    int state = 0;
    int pos = 0;
    int level = 1;

    while (pos < buffer.length()) {
        char c = buffer.charAt(pos);
        switch (state) {
        case 0:
            switch (c) {
            case '{':
                state = 1;
                break;
            case ']':
                state = 3;
                break;
            default:
                throw new ParseException(ParseException.ERROR_UNEXPECTED_CHAR);
            }
            break;

        case 1:
            switch (c) {
            case '{':
                level++;
                break;
            case '}':
                level--;
                if (level == 0) {
                    String stanza = buffer.substring(0, pos + 1);
                    buffer.replace(0, pos + 1, "");
                    return stanza;
                }
                break;

            case '"':
                state = 2;
                break;

            default:
                // nothing
            }
            break;

        case 2:
            switch (c) {
            case '\\':
                pos++;
                break;

            case '"':
                state = 1;
                break;

            default:
                // nothing
            }
            break;

        case 3:
            if (isSpace(c)) {
                pos++;
            } else if (c == '}') {
                buffer.replace(0, pos + 1, "");
                return "]}";
            }

        default:
            throw new IllegalStateException();
        }
        pos++;
    }

    return null;
}

From source file:com.tobedevoured.json.SimpleStream.java

/**
 * Process the buffer into json entities. When allow for multiple attempts for malformed json, which
 * can be caused by a json fragment in the stream.
 *
 * @param allowedMalformedAttempts int/*from   w  ww  .j  a  v a  2  s . c o m*/
 * @return List
 * @throws StreamException parser error
 */
public List processBuffer(int allowedMalformedAttempts) throws StreamException {
    JSONParser parser = new JSONParser();
    JsonStreamHandler streamHandler = new JsonStreamHandler();

    List entities = new ArrayList();

    int pos = 0;

    // Check if the buffer is crammed to the brim
    boolean bufferOverflow = buffer.length() > bufferSize;

    while (pos < buffer.length() - 1) {
        String fragment = buffer.substring(pos);

        try {
            parser.parse(fragment, streamHandler);
        } catch (ParseException e) {

            if (ParseException.ERROR_UNEXPECTED_CHAR == e.getErrorType()) {
                // Check if should wait for more streaming json before declaring it malformed
                if (malformedFragmentAttempts < allowedMalformedAttempts) {
                    malformedFragmentAttempts++;
                } else {
                    throw new StreamException(e);
                }

            } else if (ParseException.ERROR_UNEXPECTED_TOKEN != e.getErrorType()) {
                throw new StreamException(e);
            }

            logger.debug("detected json fragment, buffering for the rest: {}", fragment);
            return entities;
        }

        // increment the position in the buffer
        int currentPos = pos;
        pos = pos + parser.getPosition() + 1;

        // Create entity from buffer
        Object val = null;
        try {
            val = parser.parse(buffer.substring(currentPos, pos).trim());
            buffer.delete(currentPos, pos);
            pos = 0;
        } catch (ParseException e) {
            throw new StreamException(e);
        }

        // valid entity created, reset the malformed counter
        malformedFragmentAttempts = 0;

        if (callback != null) {
            logger.info("Executing callback with {}", val);
            try {
                callback.apply(val);
            } catch (Exception e) {
                logger.error("Failed to execute callback", e);
                throw new StreamException(e);
            }
        }

        entities.add(val);
    }

    // Compact the buffer after an overflow
    if (buffer.length() < bufferSize && bufferOverflow) {
        buffer.setLength(bufferSize);
    }

    return entities;
}