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.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  w  w . j a  v a  2 s  . co  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 ValidationMessage readValidationMessage(final JsonReader jr) throws IOException {
    String aspectId = null;//from ww  w  .  ja  va  2 s  .com
    Severity severity = null;
    String message = null;
    Set<String> artifactIds = Collections.emptySet();

    jr.beginObject();
    while (jr.hasNext()) {
        final String name = jr.nextName();
        switch (name) {
        case "aspectId":
            aspectId = jr.nextString();
            break;
        case "severity":
            severity = Severity.valueOf(jr.nextString());
            break;
        case "message":
            message = jr.nextString();
            break;
        case "artifactIds":
            artifactIds = readSet(jr);
            break;
        }
    }
    jr.endObject();

    if (aspectId == null || severity == null || message == null) {
        throw new IOException("Missing values in validation message");
    }

    return new ValidationMessage(aspectId, severity, message, artifactIds);
}

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

License:Open Source License

private Set<String> readSet(final JsonReader jr) throws IOException {
    final Set<String> result = new HashSet<>();

    jr.beginArray();//  w ww . j  a  va  2  s  .  c  o m
    while (jr.hasNext()) {
        result.add(jr.nextString());
    }
    jr.endArray();

    return result;
}

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

License:Open Source License

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

    jr.beginObject();//  w  w  w  . j  av a2s.  c  o  m
    while (jr.hasNext()) {
        final String entryName = jr.nextName();
        jr.beginObject();

        String name = null;
        Long size = null;
        String mimeType = null;
        Instant timestamp = null;

        while (jr.hasNext()) {
            final String ele = jr.nextName();
            switch (ele) {
            case "name":
                name = jr.nextString();
                break;
            case "size":
                size = jr.nextLong();
                break;
            case "mimeType":
                mimeType = jr.nextString();
                break;
            case "timestamp":
                timestamp = readTime(jr);
                break;
            default:
                jr.skipValue();
                break;
            }
        }

        if (name == null || size == null || mimeType == null || timestamp == null) {
            throw new IOException("Invalid format");
        }

        jr.endObject();

        final MetaKey key = MetaKey.fromString(entryName);

        result.put(key, new CacheEntryInformation(key, name, size, mimeType, timestamp));
    }
    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();/*from w  w  w  . j a v  a2 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 w  w  .  jav a  2  s.  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  www .  ja v a 2s  .  c om

    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.internal.coordinates.rcp.ProjectCoordinateJsonTypeAdapter.java

License:Open Source License

@Override
public ProjectCoordinate read(JsonReader in) throws IOException {
    String projectCoordinateString = in.nextString();
    return ProjectCoordinate.valueOf(projectCoordinateString);
}

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 www.j  a  v a2  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();//w ww .jav a  2  s. c  om
        return null;
    } else {
        return new File(in.nextString());
    }
}