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

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

Introduction

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

Prototype

public String nextString() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#STRING string value of the next token, consuming it.

Usage

From source file:org.diorite.config.serialization.JsonStringSerializerImpl.java

License:Open Source License

@Override
public T read(JsonReader jsonReader) throws IOException {
    return this.stringSerializer.deserialize(jsonReader.nextString());
}

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();/* ww  w .j a  v a 2s . c o m*/
        return null;
    } else {
        return UUID.fromString(in.nextString());
    }
}

From source file:org.eclipse.lsp4j.adapters.HoverTypeAdapter.java

License:Open Source License

protected Either<List<Either<String, MarkedString>>, MarkupContent> readContents(final JsonReader in)
        throws IOException {
    final JsonToken nextToken = in.peek();
    boolean _equals = Objects.equal(nextToken, JsonToken.STRING);
    if (_equals) {
        final List<Either<String, MarkedString>> value = CollectionLiterals
                .<Either<String, MarkedString>>newArrayList(
                        Either.<String, MarkedString>forLeft(in.nextString()));
        return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value);
    } else {//w  ww .j  a  va  2 s. c  o m
        boolean _equals_1 = Objects.equal(nextToken, JsonToken.BEGIN_ARRAY);
        if (_equals_1) {
            final List<Either<String, MarkedString>> value_1 = this.gson
                    .<List<Either<String, MarkedString>>>fromJson(in,
                            HoverTypeAdapter.LIST_STRING_MARKEDSTRING.getType());
            return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_1);
        } else {
            JsonElement _parse = new JsonParser().parse(in);
            final JsonObject object = ((JsonObject) _parse);
            boolean _has = object.has("language");
            if (_has) {
                final List<Either<String, MarkedString>> value_2 = CollectionLiterals
                        .<Either<String, MarkedString>>newArrayList(Either.<String, MarkedString>forRight(
                                this.gson.<MarkedString>fromJson(object, MarkedString.class)));
                return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_2);
            } else {
                return Either.<List<Either<String, MarkedString>>, MarkupContent>forRight(
                        this.gson.<MarkupContent>fromJson(object, MarkupContent.class));
            }
        }
    }
}

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();/* w w w  .j  av a  2s  . c o m*/
        return null;
    }
    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();/*from   w w  w.ja va2  s. co m*/
        return null;
    }

    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.EnumTypeAdapter.java

License:Open Source License

@Override
public T read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();/* w  w  w.j  a  v  a2 s  . c  om*/
    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();/*from ww w .ja  va2 s.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.ThrowableTypeAdapter.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   w  w w .ja  v a 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 Map<String, String> readAspects(final JsonReader jr) throws IOException {
    final Map<String, String> result = new HashMap<>();

    jr.beginObject();/*from   w w  w.j a v a2s.  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<String, ArtifactInformation> readArtifacts(final JsonReader jr) throws IOException {
    jr.beginObject();//from ww w.j a  v  a 2 s  . c  o m

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

    while (jr.hasNext()) {
        final String id = jr.nextName();
        jr.beginObject();

        String name = null;
        Long size = null;
        Instant creationTimestamp = null;
        String parentId = null;
        Set<String> childIds = Collections.emptySet();
        Set<String> facets = Collections.emptySet();
        String virtualizerAspectId = null;
        List<ValidationMessage> validationMessages = Collections.emptyList();
        Map<MetaKey, String> extractedMetaData = Collections.emptyMap();
        Map<MetaKey, String> providedMetaData = Collections.emptyMap();

        while (jr.hasNext()) {
            final String ele = jr.nextName();
            switch (ele) {
            case "name":
                name = jr.nextString();
                break;
            case "size":
                size = jr.nextLong();
                break;
            case "date":
                creationTimestamp = readTime(jr);
                break;
            case "parentId":
                parentId = jr.nextString();
                break;
            case "childIds":
                childIds = readSet(jr);
                break;
            case "facets":
                facets = readSet(jr);
                break;
            case "virtualizerAspectId":
                virtualizerAspectId = jr.nextString();
                break;
            case "extractedMetaData":
                extractedMetaData = readMetadata(jr);
                break;
            case "providedMetaData":
                providedMetaData = readMetadata(jr);
                break;
            case "validationMessages":
                validationMessages = readValidationMessages(jr);
                break;
            default:
                jr.skipValue();
                break;
            }
        }
        jr.endObject();

        if (id == null || name == null || size == null || creationTimestamp == null) {
            throw new IOException("Missing values for artifact");
        }

        this.numberOfBytes += size;

        result.put(id, new ArtifactInformation(id, parentId, childIds, name, size, creationTimestamp, facets,
                validationMessages, providedMetaData, extractedMetaData, virtualizerAspectId));
    }

    jr.endObject();

    return result;
}