Example usage for com.google.gson.stream JsonReader peek

List of usage examples for com.google.gson.stream JsonReader peek

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader peek.

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

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

License:Open Source License

@Override
public Message read(JsonReader in) throws IOException, JsonIOException, JsonSyntaxException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//  ww  w  . j  a  v  a2s  .c o m
        return null;
    }

    in.beginObject();
    String jsonrpc = null, method = null;
    Either<String, Number> id = null;
    Object rawParams = null;
    Object rawResult = null;
    ResponseError responseError = null;
    try {

        while (in.hasNext()) {
            String name = in.nextName();
            switch (name) {
            case "jsonrpc": {
                jsonrpc = in.nextString();
                break;
            }
            case "id": {
                if (in.peek() == JsonToken.NUMBER)
                    id = Either.forRight(in.nextInt());
                else
                    id = Either.forLeft(in.nextString());
                break;
            }
            case "method": {
                method = in.nextString();
                break;
            }
            case "params": {
                rawParams = parseParams(in, method);
                break;
            }
            case "result": {
                rawResult = parseResult(in, id != null ? id.get().toString() : null);
                break;
            }
            case "error": {
                responseError = gson.fromJson(in, ResponseError.class);
                break;
            }
            default:
                in.skipValue();
            }
        }
        Object params = parseParams(rawParams, method);
        Object result = parseResult(rawResult, id != null ? id.get().toString() : null);

        in.endObject();
        return createMessage(jsonrpc, id, method, params, result, responseError);

    } catch (JsonSyntaxException | MalformedJsonException | EOFException exception) {
        if (id != null || method != null) {
            // Create a message and bundle it to an exception with an issue that wraps the original exception
            Message message = createMessage(jsonrpc, id, method, rawParams, rawResult, responseError);
            MessageIssue issue = new MessageIssue("Message could not be parsed.",
                    ResponseErrorCode.ParseError.getValue(), exception);
            throw new MessageIssueException(message, issue);
        } else {
            throw exception;
        }
    }
}

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

License:Open Source License

/**
 * Convert the json input into the parameters object corresponding to the call
 * made by method.//from w  w w .  jav a 2 s .  c o m
 *
 * If the method is not known until after parsing, call
 * {@link #parseParams(Object, String)} on the return value of this call for a
 * second chance conversion.
 *
 * @param in
 *            json input to read from
 * @param method
 *            method name of request
 * @return correctly typed object if the correct expected type can be
 *         determined, or a JsonElement representing the parameters
 */
protected Object parseParams(JsonReader in, String method) throws IOException, JsonIOException {
    JsonToken next = in.peek();
    if (next == JsonToken.NULL) {
        in.nextNull();
        return null;
    }
    Type[] parameterTypes = getParameterTypes(method);
    if (parameterTypes.length == 1) {
        return fromJson(in, parameterTypes[0]);
    }
    if (parameterTypes.length > 1 && next == JsonToken.BEGIN_ARRAY) {
        List<Object> parameters = new ArrayList<Object>(parameterTypes.length);
        int index = 0;
        in.beginArray();
        while (in.hasNext()) {
            Type parameterType = index < parameterTypes.length ? parameterTypes[index] : null;
            Object parameter = fromJson(in, parameterType);
            parameters.add(parameter);
            index++;
        }
        in.endArray();
        while (index < parameterTypes.length) {
            parameters.add(null);
            index++;
        }
        return parameters;
    }
    return new JsonParser().parse(in);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w  ww. j ava2  s  . c  om
public Throwable read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    in.beginObject();
    String message = null;
    Throwable cause = null;
    while (in.hasNext()) {
        String name = in.nextName();
        switch (name) {
        case "message": {
            message = in.nextString();
            break;
        }
        case "cause": {
            cause = read(in);
            break;
        }
        default:
            in.skipValue();
        }
    }
    in.endObject();

    try {
        Constructor<Throwable> constructor;
        if (message == null && cause == null) {
            constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor();
            return constructor.newInstance();
        } else if (message == null) {
            constructor = (Constructor<Throwable>) typeToken.getRawType()
                    .getDeclaredConstructor(Throwable.class);
            return constructor.newInstance(cause);
        } else if (cause == null) {
            constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(String.class);
            return constructor.newInstance(message);
        } else {
            constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(String.class,
                    Throwable.class);
            return constructor.newInstance(message, cause);
        }
    } catch (NoSuchMethodException e) {
        if (message == null && cause == null)
            return new RuntimeException();
        else if (message == null)
            return new RuntimeException(cause);
        else if (cause == null)
            return new RuntimeException(message);
        else
            return new RuntimeException(message, cause);
    } catch (Exception e) {
        throw new JsonParseException(e);
    }
}

From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java

License:Open Source License

private Map<String, String> readAspects(final JsonReader jr) throws IOException {
    final Map<String, String> result = new HashMap<>();

    jr.beginObject();//from  ww  w.j  a  v  a 2s  .com
    while (jr.hasNext()) {
        switch (jr.nextName()) {
        case "map":
            jr.beginObject();
            while (jr.hasNext()) {
                final String id = jr.nextName();
                String value = null;
                if (jr.peek() == JsonToken.STRING) {
                    value = jr.nextString();
                } else {
                    jr.skipValue();
                }
                result.put(id, value);
            }
            jr.endObject();
            break;
        }
    }
    jr.endObject();

    return result;
}

From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java

License:Open Source License

private Map<MetaKey, String> readMetadata(final JsonReader jr) throws IOException {
    final Map<MetaKey, String> result = new HashMap<>();

    jr.beginObject();//from  w ww  .j av  a  2s .c o m

    while (jr.hasNext()) {
        final String name = jr.nextName();
        if (jr.peek() == JsonToken.NULL) {
            jr.skipValue();
        } else {
            final String value = jr.nextString();
            result.put(MetaKey.fromString(name), value);
        }
    }

    jr.endObject();

    return result;
}

From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java

License:Open Source License

private Instant readTime(final JsonReader jr) throws IOException {
    final JsonToken peek = jr.peek();
    if (peek == JsonToken.NUMBER) {
        return Instant.ofEpochMilli(jr.nextLong());
    } else if (peek == JsonToken.NULL) {
        jr.nextNull();// w ww. j a va 2 s .c  o  m
        return null;
    } else if (peek == JsonToken.STRING) {
        final String str = jr.nextString();

        try {
            return Instant.ofEpochMilli(Long.parseLong(str));
        } catch (final NumberFormatException e) {
            try {
                return this.dateFormat.parse(str).toInstant();
            } catch (final ParseException e2) {
                throw new IOException(e2);
            }
        }
    } else {
        throw new IOException(String.format("Invalid timestamp token: %s", peek));
    }
}

From source file:org.eclipse.packagedrone.repo.gson.MetaKeyTypeAdapter.java

License:Open Source License

@Override
public MetaKey read(final JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        return null;
    } else {/*w  ww  .  java 2s.  c o m*/
        return MetaKey.fromString(reader.nextString());
    }
}

From source file:org.eclipse.packagedrone.repo.MetaKeys.java

License:Open Source License

public static Map<MetaKey, String> readJson(final Reader input) throws IOException {
    @SuppressWarnings("resource")
    final JsonReader reader = new JsonReader(input);

    final Map<MetaKey, String> result = new HashMap<>();

    reader.beginObject();//from  w  w w .  j  a va  2 s  .co  m

    while (reader.hasNext()) {
        final String name = reader.nextName();
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
        } else {
            result.put(MetaKey.fromString(name), reader.nextString());
        }
    }

    reader.endObject();

    return result;
}

From source file:org.eclipse.recommenders.utils.gson.FieldNameTypeAdapter.java

License:Open Source License

@Override
public IFieldName read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//from  w w w.  j a  va 2 s .c  o  m
        return null;
    } else {
        String identifier = in.nextString();
        return VmFieldName.get(identifier);
    }
}

From source file:org.eclipse.recommenders.utils.gson.FileTypeAdapter.java

License:Open Source License

@Override
public File read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*  ww w . j ava  2  s. c o m*/
        return null;
    } else {
        return new File(in.nextString());
    }
}