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

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

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

public List<CordovaRegistryPluginInfo> retrievePluginInfos(IProgressMonitor monitor) throws CoreException {
    if (monitor == null)
        monitor = new NullProgressMonitor();

    HttpClient client = new DefaultHttpClient();
    String url = registry.endsWith("/") ? registry + "-/all" : registry + "/-/all";
    HttpGet get = new HttpGet(url);
    HttpResponse response;/*w  w  w.  j a  v a  2 s  .co m*/

    try {
        if (monitor.isCanceled()) {
            return null;
        }
        response = client.execute(get);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        monitor.worked(9);
        JsonReader reader = new JsonReader(new InputStreamReader(stream));
        reader.beginObject();//start the Registry
        plugins = new ArrayList<CordovaRegistryPluginInfo>();
        while (reader.hasNext()) {
            JsonToken token = reader.peek();
            switch (token) {
            case BEGIN_OBJECT:
                CordovaRegistryPluginInfo info = new CordovaRegistryPluginInfo();
                readPluginInfo(reader, info);
                plugins.add(info);
                break;
            case NAME:
                String name = reader.nextName();
                if (name.equals("_updated")) {
                    long newUpdate = reader.nextLong();
                    if (newUpdate == this.updated) {//No changes 
                        return plugins;
                    }

                }
                break;
            default:
                Assert.isTrue(false, "Unexpected token: " + token);
                break;
            }

        }
        reader.endObject();

        return plugins;

    } catch (ClientProtocolException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Can not retrieve plugin catalog", e));
    } catch (IOException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Can not retrieve plugin catalog", e));
    } finally {
        monitor.done();
    }

}

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void readPluginInfo(JsonReader reader, CordovaRegistryPluginInfo plugin) throws IOException {
    Assert.isNotNull(plugin);/*from  w ww .  j  av a2 s . c  o m*/
    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) && plugin instanceof CordovaRegistryPlugin) {
                parseDetailedVersions(reader, (CordovaRegistryPlugin) plugin);
                break;
            }
            if ("dist".equals(name) && plugin instanceof CordovaRegistryPluginVersion) {
                parseDistDetails(reader, (CordovaRegistryPluginVersion) plugin);
                break;
            }
            if ("license".equals(name) && plugin instanceof CordovaRegistryPluginVersion) {
                CordovaRegistryPluginVersion v = (CordovaRegistryPluginVersion) plugin;
                v.setLicense(reader.nextString());
                break;
            }
            break;
        }

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

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseDistDetails(JsonReader reader, CordovaRegistryPluginVersion plugin) throws IOException {
    reader.beginObject();
    JsonToken token = reader.peek();//from  w  w w  . j av a2  s . c om
    while (token != JsonToken.END_OBJECT) {
        switch (token) {
        case NAME:
            String name = reader.nextName();
            if ("shasum".equals(name)) {
                plugin.setDistributionSHASum(reader.nextString());
                break;
            }
            if ("tarball".equals(name)) {
                plugin.setDistributionTarball(reader.nextString());
                break;
            }
            break;

        default:
            reader.skipValue();
            break;
        }
        token = reader.peek();
    }
    reader.endObject();
}

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseDetailedVersions(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException {
    reader.beginObject();//versions
    JsonToken token = reader.peek();//  w ww.ja  v  a2 s.  co  m
    while (token != JsonToken.END_OBJECT) {
        switch (token) {
        case NAME:
            CordovaRegistryPluginVersion version = new CordovaRegistryPluginVersion();
            version.setVersionNumber(reader.nextName());
            readPluginInfo(reader, version);
            plugin.addVersion(version);
            break;

        default:
            reader.skipValue();
            break;
        }
        token = reader.peek();
    }
    reader.endObject();
}

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseLatestVersion(JsonReader reader, CordovaRegistryPluginInfo plugin) throws IOException {
    reader.beginObject();
    JsonToken token = reader.peek();/*from  www  . j  a v a  2 s  .co  m*/
    while (token != JsonToken.END_OBJECT) {
        switch (token) {
        case NAME:
            String tag = reader.nextName();
            if ("latest".equals(tag)) {
                plugin.setLatestVersion(reader.nextString());
            }
            break;

        default:
            reader.skipValue();
            break;
        }
        token = reader.peek();
    }
    reader.endObject();
}

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseMaintainers(JsonReader reader, CordovaRegistryPluginInfo plugin) throws IOException {
    reader.beginArray();/*from w w w  . ja v  a 2  s .  c o m*/
    String name = null, email = null;
    JsonToken token = reader.peek();

    while (token != JsonToken.END_ARRAY) {
        switch (token) {
        case BEGIN_OBJECT:
            reader.beginObject();
            name = email = null;
            break;
        case END_OBJECT:
            reader.endObject();
            plugin.addMaintainer(email, name);
            break;
        case NAME:
            String tagName = reader.nextName();
            if ("name".equals(tagName)) {
                name = reader.nextString();
                break;
            }
            if ("email".equals(tagName)) {
                email = reader.nextString();
                break;
            }
        default:
            Assert.isTrue(false, "Unexpected token");
            break;
        }
        token = reader.peek();
    }
    reader.endArray();
}

From source file:org.jclouds.http.functions.ParseFirstJsonValueNamed.java

License:Apache License

private JsonToken skipAndPeek(JsonToken token, JsonReader reader) throws IOException {
    switch (token) {
    case BEGIN_ARRAY:
        reader.beginArray();//from www . j  a va  2s.  c  o  m
        break;
    case END_ARRAY:
        reader.endArray();
        break;
    case BEGIN_OBJECT:
        reader.beginObject();
        break;
    case END_OBJECT:
        reader.endObject();
        break;
    case NAME:
        // NOTE that we have already advanced on NAME in the eval block;
        break;
    case STRING:
        reader.nextString();
        break;
    case NUMBER:
        reader.nextString();
        break;
    case BOOLEAN:
        reader.nextBoolean();
        break;
    case NULL:
        reader.nextNull();
        break;
    case END_DOCUMENT:
        break;
    }
    return reader.peek();
}

From source file:org.jclouds.json.internal.IgnoreNullMapTypeAdapterFactory.java

License:Apache License

private <K, V> TypeAdapter<Map<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter,
        final TypeAdapter<V> valueAdapter) {
    return new TypeAdapter<Map<K, V>>() {
        public void write(JsonWriter out, Map<K, V> value) throws IOException {
            out.beginObject();//from  w w  w.  j av a  2  s .  co m
            for (Map.Entry<K, V> element : value.entrySet()) {
                out.name(String.valueOf(element.getKey()));
                valueAdapter.write(out, element.getValue());
            }
            out.endObject();
        }

        public Map<K, V> read(JsonReader in) throws IOException {
            Map<K, V> result = Maps.newLinkedHashMap();
            in.beginObject();
            while (in.hasNext()) {
                JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
                K name = keyAdapter.read(in);
                V value = valueAdapter.read(in);
                if (value != null)
                    result.put(name, value);
            }
            in.endObject();
            return result;
        }
    }.nullSafe();
}

From source file:org.jclouds.json.internal.IgnoreNullMultimapTypeAdapterFactory.java

License:Apache License

private <K, V> TypeAdapter<Multimap<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter,
        final TypeAdapter<V> valueAdapter) {
    return new TypeAdapter<Multimap<K, V>>() {
        public void write(JsonWriter out, Multimap<K, V> map) throws IOException {
            out.beginObject();/*from   w w  w  .  j  a  v  a2s.  c o m*/
            for (K key : map.keySet()) {
                out.name(String.valueOf(key));
                out.beginArray();
                for (V value : map.get(key)) {
                    valueAdapter.write(out, value);
                }
                out.endArray();
            }
            out.endObject();
        }

        public Multimap<K, V> read(JsonReader in) throws IOException {
            ImmutableMultimap.Builder<K, V> result = ImmutableListMultimap.builder();
            in.beginObject();
            while (in.hasNext()) {
                JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
                K name = keyAdapter.read(in);
                in.beginArray();
                while (in.hasNext()) {
                    V value = valueAdapter.read(in);
                    if (value != null)
                        result.put(name, value);
                }
                in.endArray();
            }
            in.endObject();
            return result.build();
        }
    }.nullSafe();
}

From source file:org.jclouds.oauth.v2.json.ClaimSetTypeAdapter.java

License:Apache License

@Override
public ClaimSet read(JsonReader in) throws IOException {
    ClaimSet.Builder builder = new ClaimSet.Builder();
    in.beginObject();
    while (in.hasNext()) {
        String claimName = in.nextName();
        String claimValue = in.nextString();
        builder.addClaim(claimName, claimValue);
    }//from ww  w. j  a  v  a  2  s  . c o m
    in.endObject();
    return builder.build();
}