Search in sources :

Example 1 with GwtTestJSONException

use of com.googlecode.gwt.test.exceptions.GwtTestJSONException in project gwt-test-utils by gwt-test-utils.

the class JSONParserPatcher method evaluate.

@PatchMethod
static JSONValue evaluate(String json, boolean strict) {
    JsonParser jp = null;
    try {
        JsonFactory f = new JsonFactory();
        if (!strict) {
            f.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
            f.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            f.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            f.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
            f.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            f.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            f.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        }
        jp = f.createJsonParser(json);
        // will return JsonToken.START_OBJECT (verify?)
        jp.nextToken();
        JSONObject jsonObject = extractJSONObject(json, jp);
        return jsonObject;
    } catch (Exception e) {
        if (e instanceof GwtTestException) {
            throw (GwtTestException) e;
        }
        throw new GwtTestJSONException("Error while parsing JSON string '" + json + "'", e);
    } finally {
        if (jp != null) {
            try {
                // ensure resources get cleaned up timely and properly
                jp.close();
            } catch (IOException e) {
            // should never happen
            }
        }
    }
}
Also used : GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) JsonFactory(org.codehaus.jackson.JsonFactory) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) IOException(java.io.IOException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) JsonParser(org.codehaus.jackson.JsonParser) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod)

Example 2 with GwtTestJSONException

use of com.googlecode.gwt.test.exceptions.GwtTestJSONException in project gwt-test-utils by gwt-test-utils.

the class JsonUtilsPatcher method eval.

private static <T extends JavaScriptObject> T eval(String json) {
    JsonParser jp = null;
    try {
        jp = getFactory().createJsonParser(json);
        // will return JsonToken.START_OBJECT (verify?)
        jp.nextToken();
        return extractJso(json, jp.getCurrentToken(), jp).<T>cast();
    } catch (Exception e) {
        if (e instanceof GwtTestException) {
            throw (GwtTestException) e;
        }
        throw new GwtTestJSONException("Error while parsing JSON string '" + json + "'", e);
    } finally {
        if (jp != null) {
            try {
                // ensure resources get cleaned up timely and properly
                jp.close();
            } catch (IOException e) {
            // should never happen
            }
        }
    }
}
Also used : GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) IOException(java.io.IOException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) IOException(java.io.IOException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) JsonParseException(org.codehaus.jackson.JsonParseException) JsonParser(org.codehaus.jackson.JsonParser)

Aggregations

GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)2 GwtTestJSONException (com.googlecode.gwt.test.exceptions.GwtTestJSONException)2 IOException (java.io.IOException)2 JsonParseException (org.codehaus.jackson.JsonParseException)2 JsonParser (org.codehaus.jackson.JsonParser)2 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)1 JsonFactory (org.codehaus.jackson.JsonFactory)1