Example usage for com.google.gson JsonIOException JsonIOException

List of usage examples for com.google.gson JsonIOException JsonIOException

Introduction

In this page you can find the example usage for com.google.gson JsonIOException JsonIOException.

Prototype

public JsonIOException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:com.jkoolcloud.tnt4j.streams.utils.Utils.java

License:Apache License

/**
 * Deserializes JSON data object ({@link String}, {@link java.io.Reader}, {@link java.io.InputStream}) into map
 * structured data.//from   w  ww  .  j av a2s. c  o m
 *
 * @param jsonData
 *            JSON format data object
 * @param jsonAsLine
 *            flag indicating that complete JSON data package is single line
 * @return data map parsed from JSON data object
 * @throws com.google.gson.JsonSyntaxException
 *             if there was a problem reading from the Reader
 * @throws com.google.gson.JsonIOException
 *             if json is not a valid representation for an object of type
 * 
 * @see com.google.gson.Gson#fromJson(String, Class)
 * @see com.google.gson.Gson#fromJson(java.io.Reader, Class)
 */
@SuppressWarnings("unchecked")
public static Map<String, ?> fromJsonToMap(Object jsonData, boolean jsonAsLine) {
    Map<String, ?> map = new LinkedTreeMap<String, Object>();
    Gson gson = new Gson();

    if (jsonAsLine) {
        try {
            map = (Map<String, ?>) gson.fromJson(getStringLine(jsonData), map.getClass());
        } catch (IOException ioe) {
            throw new JsonIOException(ioe);
        }
    } else {
        if (jsonData instanceof String) {
            map = (Map<String, ?>) gson.fromJson((String) jsonData, map.getClass());
        } else if (jsonData instanceof byte[]) {
            map = (Map<String, ?>) gson.fromJson(getString((byte[]) jsonData), map.getClass());
        } else if (jsonData instanceof Reader) {
            map = (Map<String, ?>) gson.fromJson((Reader) jsonData, map.getClass());
        } else if (jsonData instanceof InputStream) {
            map = (Map<String, ?>) gson.fromJson(
                    new BufferedReader(new InputStreamReader((InputStream) jsonData)), map.getClass());
        }
    }

    return map;
}

From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.java

License:Open Source License

/**
 * Convert the json input into the result object corresponding to the call made
 * by id.//  w w w. ja va 2s  . co m
 *
 * If the id is not known until after parsing, call
 * {@link #parseResult(Object, String)} on the return value of this call for a
 * second chance conversion.
 *
 * @param in
 *            json input to read from
 * @param id
 *            id of request message this is in response to
 * @return correctly typed object if the correct expected type can be
 *         determined, or a JsonElement representing the result
 */
protected Object parseResult(JsonReader in, String id) throws JsonIOException, JsonSyntaxException {
    Type type = null;
    MethodProvider methodProvider = handler.getMethodProvider();
    if (methodProvider != null && id != null) {
        String resolvedMethod = methodProvider.resolveMethod(id);
        if (resolvedMethod != null) {
            JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod);
            if (jsonRpcMethod != null) {
                type = jsonRpcMethod.getReturnType();
                if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) {
                    TypeAdapter<?> typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson,
                            TypeToken.get(type));
                    try {
                        if (typeAdapter != null)
                            return typeAdapter.read(in);
                    } catch (IOException exception) {
                        throw new JsonIOException(exception);
                    }
                }
            }
        }
    }
    return fromJson(in, type);
}

From source file:org.onos.yangtools.yang.data.codec.gson.JsonParserStream.java

License:Open Source License

public JsonParserStream parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
    // code copied from gson's JsonParser and Stream classes

    final boolean lenient = reader.isLenient();
    reader.setLenient(true);//from w  w  w  .j  a  va  2 s  . c  o  m
    boolean isEmpty = true;
    try {
        reader.peek();
        isEmpty = false;
        final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(
                parentNode);
        read(reader, compositeNodeDataWithSchema);
        compositeNodeDataWithSchema.write(writer);

        return this;
        // return read(reader);
    } catch (final EOFException e) {
        if (isEmpty) {
            return this;
            // return JsonNull.INSTANCE;
        }
        // The stream ended prematurely so it is likely a syntax error.
        throw new JsonSyntaxException(e);
    } catch (final MalformedJsonException e) {
        throw new JsonSyntaxException(e);
    } catch (final IOException e) {
        throw new JsonIOException(e);
    } catch (final NumberFormatException e) {
        throw new JsonSyntaxException(e);
    } catch (StackOverflowError | OutOfMemoryError e) {
        throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
    } finally {
        reader.setLenient(lenient);
    }
}

From source file:org.opendaylight.controller.sal.rest.gson.JsonParser.java

License:Open Source License

public JsonElement parse(JsonReader reader) throws JsonIOException, JsonSyntaxException {
    // code copied from gson's JsonParser and Stream classes

    boolean lenient = reader.isLenient();
    reader.setLenient(true);/*from ww  w.  j a va  2 s .c  o  m*/
    boolean isEmpty = true;
    try {
        reader.peek();
        isEmpty = false;
        return read(reader);
    } catch (EOFException e) {
        if (isEmpty) {
            return JsonNull.INSTANCE;
        }
        // The stream ended prematurely so it is likely a syntax error.
        throw new JsonSyntaxException(e);
    } catch (MalformedJsonException e) {
        throw new JsonSyntaxException(e);
    } catch (IOException e) {
        throw new JsonIOException(e);
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    } catch (StackOverflowError | OutOfMemoryError e) {
        throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
    } finally {
        reader.setLenient(lenient);
    }
}

From source file:org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.java

License:Open Source License

public JsonParserStream parse(final JsonReader reader) {
    // code copied from gson's JsonParser and Stream classes

    final boolean lenient = reader.isLenient();
    reader.setLenient(true);//from   w  ww . j  a va 2s .  co  m
    boolean isEmpty = true;
    try {
        reader.peek();
        isEmpty = false;
        final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(
                parentNode);
        read(reader, compositeNodeDataWithSchema);
        compositeNodeDataWithSchema.write(writer);

        return this;
    } catch (final EOFException e) {
        if (isEmpty) {
            return this;
        }
        // The stream ended prematurely so it is likely a syntax error.
        throw new JsonSyntaxException(e);
    } catch (final MalformedJsonException | NumberFormatException e) {
        throw new JsonSyntaxException(e);
    } catch (final IOException e) {
        throw new JsonIOException(e);
    } catch (StackOverflowError | OutOfMemoryError e) {
        throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
    } finally {
        reader.setLenient(lenient);
    }
}

From source file:org.spongepowered.plugin.meta.McModInfo.java

License:MIT License

public List<PluginMetadata> fromJson(String json) {
    try {/*from  w w  w.j  a  va 2 s  . co  m*/
        return this.adapter.fromJson(json);
    } catch (IOException e) {
        throw new JsonIOException(e);
    }
}

From source file:org.spongepowered.plugin.meta.McModInfo.java

License:MIT License

public String toJson(List<PluginMetadata> meta) {
    StringWriter writer = new StringWriter();
    try {//from   w  ww  .ja v a 2s .  co  m
        write(writer, meta);
    } catch (IOException e) {
        throw new JsonIOException(e);
    }
    return writer.toString();
}

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.imported.RuntimeTypeAdapterFactory.java

License:Apache License

/**
 * Takes a reader in any state and returns the next value as a JsonElement.
 *//*from ww  w. j a v a 2  s  .com*/
private static JsonElement parse(JsonReader reader) throws JsonParseException {
    boolean isEmpty = true;
    try {
        reader.peek();
        isEmpty = false;
        return RuntimeTypeAdapterFactory.JSON_ELEMENT.read(reader);
    } catch (EOFException e) {
        /*
         * For compatibility with JSON 1.5 and earlier, we return a JsonNull for
         * empty documents instead of throwing.
         */
        if (isEmpty) {
            return JsonNull.INSTANCE;
        }
        // The stream ended prematurely so it is likely a syntax error.
        throw new JsonSyntaxException(e);
    } catch (MalformedJsonException e) {
        throw new JsonSyntaxException(e);
    } catch (IOException e) {
        throw new JsonIOException(e);
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}