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

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

Introduction

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

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

Usage

From source file:org.eclipse.epp.internal.logging.aeri.ui.utils.UuidTypeAdapter.java

License:Open Source License

@Override
public UUID read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else {/*from w  w  w  .j  ava 2s .  co m*/
        return UUID.fromString(in.nextString());
    }
}

From source file:org.eclipse.lsp4j.jsonrpc.debug.adapters.DebugEnumTypeAdapter.java

License:Open Source License

@Override
public T read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*  w  ww. j av a2  s .com*/
    return serializedFormToEnum.get(in.nextString());
}

From source file:org.eclipse.lsp4j.jsonrpc.debug.adapters.DebugMessageTypeAdapter.java

License:Open Source License

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

    in.beginObject();
    String messageType = null, method = null, message = null;
    int seq = 0, request_seq = 0;
    Boolean rawSuccess = null;
    Object rawParams = null;
    Object rawBody = null;

    try {

        while (in.hasNext()) {
            String name = in.nextName();
            switch (name) {
            case "seq": {
                seq = in.nextInt();
                break;
            }
            case "request_seq": {
                // on responses we treat the request_seq as the id
                request_seq = in.nextInt();
                break;
            }
            case "type": {
                messageType = in.nextString();
                break;
            }
            case "success": {
                rawSuccess = in.nextBoolean();
                break;
            }
            case "command": {
                method = in.nextString();
                break;
            }
            case "event": {
                method = in.nextString();
                break;
            }
            case "message": {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                } else {
                    message = in.nextString();
                }
                break;
            }
            case "arguments": {
                rawParams = parseParams(in, method);
                break;
            }
            case "body": {
                rawBody = parseBody(in, messageType, request_seq, method, rawSuccess);
                break;
            }
            default:
                in.skipValue();
            }
        }
        boolean success = rawSuccess != null ? rawSuccess : false;
        Object params = parseParams(rawParams, method);
        Object body = parseBody(rawBody, messageType, request_seq, method, success);

        in.endObject();
        return createMessage(messageType, seq, request_seq, method, success, message, params, body);

    } catch (JsonSyntaxException | MalformedJsonException | EOFException exception) {
        if ("request".equals(messageType) || "event".equals(messageType) || "response".equals(messageType)) {
            // Create a message and bundle it to an exception with an issue that wraps the original exception
            boolean success = rawSuccess != null ? rawSuccess : false;
            Message resultMessage = createMessage(messageType, seq, request_seq, method, success, message,
                    rawParams, rawBody);
            MessageIssue issue = new MessageIssue("Message could not be parsed.",
                    ResponseErrorCode.ParseError.getValue(), exception);
            throw new MessageIssueException(resultMessage, issue);
        } else {
            throw exception;
        }
    }
}

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

License:Open Source License

@Override
public Collection<E> read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();/*from w w w  .j a va  2 s  . com*/
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        Collection<E> collection = constructor.get();
        in.beginArray();
        while (in.hasNext()) {
            E instance = elementTypeAdapter.read(in);
            collection.add(instance);
        }
        in.endArray();
        return collection;
    } else {
        Collection<E> collection = constructor.get();
        E instance = elementTypeAdapter.read(in);
        collection.add(instance);
        return collection;
    }
}

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

License:Open Source License

@Override
public Either<L, R> read(JsonReader in) throws IOException {
    JsonToken next = in.peek();/*from   w  w w  .  j a  va 2s  . c  o m*/
    if (next == JsonToken.NULL) {
        in.nextNull();
        return null;
    }
    return create(next, in);
}

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

License:Open Source License

@Override
public T read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();/*from ww w .  j  a v  a2  s .com*/
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else if (peek == JsonToken.NUMBER) {
        return valueToConstant.get(in.nextInt());
    } else {
        String string = in.nextString();
        try {
            return valueToConstant.get(Integer.parseInt(string));
        } catch (NumberFormatException e) {
            return nameToConstant.get(string);
        }
    }
}

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();
        return null;
    }//from  www  .j  a v a  2  s .  co m

    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./* w w  w . ja  va2s  .  com*/
 *
 * 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/*from w  ww.java 2 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 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();
        return null;
    } else if (peek == JsonToken.STRING) {
        final String str = jr.nextString();

        try {/*from ww w  .  j a v a  2 s .  co  m*/
            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));
    }
}