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

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

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

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  a2 s . c  o m*/
    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 .ja v a 2s  .  c om*/

    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;
}

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();/*www.jav  a2  s.  com*/

    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 .  j  av a 2  s. co  m
    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 Map<MetaKey, CacheEntryInformation> readCacheEntries(final JsonReader jr) throws IOException {
    final Map<MetaKey, CacheEntryInformation> result = new HashMap<>();

    jr.beginObject();//ww  w  .  j a v  a 2s  .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.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();/*  w w w  .java 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.MultisetTypeAdapterFactory.java

License:Open Source License

private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(final TypeAdapter<E> elementAdapter) {
    return new TypeAdapter<Multiset<E>>() {
        public void write(JsonWriter out, Multiset<E> multiset) throws IOException {
            if (multiset == null) {
                out.nullValue();//from   ww w .  j ava 2  s  .co  m
                return;
            }

            out.beginArray();
            for (Entry<E> entry : multiset.entrySet()) {
                out.beginObject();
                out.name("id");
                elementAdapter.write(out, entry.getElement());
                out.name("count");
                out.value(entry.getCount());
                out.endObject();
            }
            out.endArray();
        }

        public Multiset<E> read(JsonReader in) throws IOException {
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
                return null;
            }

            Multiset<E> result = LinkedHashMultiset.create();
            in.beginArray();
            while (in.hasNext()) {
                in.beginObject();
                in.nextName(); // "id"
                E element = elementAdapter.read(in);
                in.nextName(); // "count"
                int count = in.nextInt();
                result.add(element, count);
                in.endObject();
            }
            in.endArray();
            return result;
        }
    };
}

From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private CordovaRegistryPluginInfo parseCordovaRegistryPluginInfo(JsonReader reader) throws IOException {
    CordovaRegistryPluginInfo info = new CordovaRegistryPluginInfo();
    reader.beginObject();/*from  w  w w . j a  v  a  2 s  .  c o m*/
    reader.skipValue(); // name
    reader.beginArray();
    reader.nextString(); //ecosystem:cordova
    info.setName(safeReadStringValue(reader));
    info.setDescription(safeReadStringValue(reader));
    reader.endArray();
    reader.nextName();
    reader.nextInt();
    reader.endObject();
    return info;
}

From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void readVersionInfo(JsonReader reader, RegistryPluginVersion version) throws IOException {
    Assert.isNotNull(version);// w w  w.  j ava 2 s . c o  m
    reader.beginObject();
    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        switch (token) {
        case NAME:
            String name = reader.nextName();
            if ("dist".equals(name)) {
                parseDistDetails(reader, version);
                break;
            }
            break;

        default:
            reader.skipValue();
            break;
        }
    }
    reader.endObject();
}

From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void readPluginInfo(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException {
    Assert.isNotNull(plugin);//from  www .  j a va2 s .  c  om
    reader.beginObject();

    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        switch (token) {
        case NAME: {
            String name = reader.nextName();
            if ("name".equals(name)) {
                plugin.setName(reader.nextString());
                break;
            }
            if ("description".equals(name)) {
                plugin.setDescription(reader.nextString());
                break;
            }
            if ("keywords".equals(name)) {
                parseKeywords(reader, plugin);
                break;
            }
            if ("maintainers".equals(name)) {
                parseMaintainers(reader, plugin);
                break;
            }
            if ("dist-tags".equals(name)) {
                parseLatestVersion(reader, plugin);
                break;
            }
            if ("versions".equals(name)) {
                parseVersions(reader, plugin);
                break;
            }
            if ("license".equals(name)) {
                plugin.setLicense(reader.nextString());
                break;
            }
            break;
        }

        default:
            reader.skipValue();
            break;
        }
    }
    reader.endObject();
}