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

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

Introduction

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

Prototype

public void beginArray() throws IOException 

Source Link

Document

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

Usage

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   w w w  . j  a  v  a  2s  . c o  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.smarthome.storage.json.PropertiesTypeAdapter.java

License:Open Source License

@Override
public Map<String, Object> read(JsonReader in) throws IOException {
    // gson implementation code is modified when deserializing numbers
    JsonToken peek = in.peek();//from   ww  w. java  2s. c om
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    Map<String, Object> map = constructor.get(TOKEN).construct();

    if (peek == JsonToken.BEGIN_ARRAY) {
        in.beginArray();
        while (in.hasNext()) {
            in.beginArray(); // entry array
            String key = keyAdapter.read(in);

            // modification
            Object value = getValue(in);

            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
            in.endArray();
        }
        in.endArray();
    } else {
        in.beginObject();
        while (in.hasNext()) {
            JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
            String key = keyAdapter.read(in);

            // modification
            Object value = getValue(in);

            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
        }
        in.endObject();
    }
    return map;
}

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

License:Open Source License

public List<CordovaRegistryPluginInfo> retrievePluginInfos(IProgressMonitor monitor) throws CoreException {

    if (monitor == null)
        monitor = new NullProgressMonitor();

    monitor.beginTask("Retrieve plug-in registry catalog", 10);
    DefaultHttpClient theHttpClient = new DefaultHttpClient();
    HttpUtil.setupProxy(theHttpClient);/*  w ww .  java  2 s .c o  m*/
    HttpClient client = new CachingHttpClient(theHttpClient, new HeapResourceFactory(),
            new BundleHttpCacheStorage(HybridCore.getContext().getBundle()), getCacheConfig());
    JsonReader reader = null;
    try {
        if (monitor.isCanceled()) {
            return null;
        }
        String url = REGISTRY_URL
                + "-/_view/byKeyword?startkey=%5B%22ecosystem:cordova%22%5D&endkey=%5B%22ecosystem:cordova1%22%5D&group_level=3";
        HttpGet get = new HttpGet(URI.create(url));
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        monitor.worked(7);
        reader = new JsonReader(new InputStreamReader(stream));
        reader.beginObject();//start the Registry
        final ArrayList<CordovaRegistryPluginInfo> plugins = new ArrayList<CordovaRegistryPluginInfo>();
        while (reader.hasNext()) {
            JsonToken token = reader.peek();
            switch (token) {
            case BEGIN_ARRAY:
                reader.beginArray();
                break;
            case BEGIN_OBJECT:
                plugins.add(parseCordovaRegistryPluginInfo(reader));
                break;
            default:
                reader.skipValue();
                break;
            }

        }
        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 {
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {
                /*ignored*/ }
        monitor.done();
    }
}

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 ww.  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 parseMaintainers(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException {
    reader.beginArray();
    String name = null, email = null;
    JsonToken token = reader.peek();/*from   ww w.java2s . com*/

    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.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseKeywords(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        plugin.addKeyword(reader.nextString());
    }/*from   w w w . j av  a  2  s  .com*/
    reader.endArray();
}

From source file:org.eclipse.tm4e.core.internal.parser.json.JSONPListParser.java

License:Open Source License

public T parse(InputStream contents) throws Exception {
    PList<T> pList = new PList<T>(theme);
    JsonReader reader = new JsonReader(new InputStreamReader(contents, StandardCharsets.UTF_8));
    // reader.setLenient(true);
    boolean parsing = true;
    while (parsing) {
        JsonToken nextToken = reader.peek();
        switch (nextToken) {
        case BEGIN_ARRAY:
            pList.startElement(null, "array", null, null);
            reader.beginArray();
            break;
        case END_ARRAY:
            pList.endElement(null, "array", null);
            reader.endArray();//from w w  w . ja  v a  2 s . c  o  m
            break;
        case BEGIN_OBJECT:
            pList.startElement(null, "dict", null, null);
            reader.beginObject();
            break;
        case END_OBJECT:
            pList.endElement(null, "dict", null);
            reader.endObject();
            break;
        case NAME:
            String lastName = reader.nextName();
            pList.startElement(null, "key", null, null);
            pList.characters(lastName.toCharArray(), 0, lastName.length());
            pList.endElement(null, "key", null);
            break;
        case NULL:
            reader.nextNull();
            break;
        case BOOLEAN:
            reader.nextBoolean();
            break;
        case NUMBER:
            reader.nextLong();
            break;
        case STRING:
            String value = reader.nextString();
            pList.startElement(null, "string", null, null);
            pList.characters(value.toCharArray(), 0, value.length());
            pList.endElement(null, "string", null);
            break;
        case END_DOCUMENT:
            parsing = false;
            break;
        default:
            break;
        }
    }
    reader.close();
    return pList.getResult();
}

From source file:org.fao.fenix.wds.core.datasource.DatasourcePool.java

License:Open Source License

/**
 * @throws FileNotFoundException/*  w  w w. j a  va 2s  .  c  om*/
 * @throws IOException
 *
 * This method is invoked by Spring at the start-up. This function retrieves
 * all the files stored in the <code>datasourcePath</code> and populate the
 * <code>datasources</code> map where the key is the datasource name (e.g. 'FAOSTAT')
 * and the value is the <code>DatasourceBean</code>.
 */
public void init() throws FileNotFoundException, IOException {
    Gson g = new Gson();
    File root = new File(this.datasourcePath);
    File[] files = root.listFiles();
    for (int i = 0; i < files.length; i++) {
        InputStream is = new FileInputStream(files[i].getAbsoluteFile());
        JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
        reader.beginArray();
        while (reader.hasNext()) {
            DatasourceBean b = readMessage(reader);
            this.datasources.put(b.getId(), b);
        }
        reader.endArray();
    }
}

From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java

License:Apache License

private void consumeVariants(JsonReader reader, MutableModuleComponentResolveMetadata metadata)
        throws IOException {
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        consumeVariant(reader, metadata);
    }//from   w  ww  .j  av  a 2 s .  c o  m
    reader.endArray();
}

From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java

License:Apache License

private List<ModuleDependency> consumeDependencies(JsonReader reader) throws IOException {
    List<ModuleDependency> dependencies = new ArrayList<ModuleDependency>();
    reader.beginArray();
    while (reader.peek() != END_ARRAY) {
        reader.beginObject();//from  www .j av  a2 s .  com
        String group = null;
        String module = null;
        String reason = null;
        ImmutableAttributes attributes = ImmutableAttributes.EMPTY;
        VersionConstraint version = DefaultImmutableVersionConstraint.of();
        ImmutableList<ExcludeMetadata> excludes = ImmutableList.of();
        while (reader.peek() != END_OBJECT) {
            String name = reader.nextName();
            if (name.equals("group")) {
                group = reader.nextString();
            } else if (name.equals("module")) {
                module = reader.nextString();
            } else if (name.equals("version")) {
                version = consumeVersion(reader);
            } else if (name.equals("excludes")) {
                excludes = consumeExcludes(reader);
            } else if (name.equals("reason")) {
                reason = reader.nextString();
            } else if (name.equals("attributes")) {
                attributes = consumeAttributes(reader);
            } else {
                consumeAny(reader);
            }
        }
        dependencies.add(new ModuleDependency(group, module, version, excludes, reason, attributes));
        reader.endObject();
    }
    reader.endArray();
    return dependencies;
}