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.gw2InfoViewer.services.json.JsonConversionService.java

License:Open Source License

public static EventList parseEventListWithoutNames(InputStream json) throws IOException {
    List<Event> events;

    events = new ArrayList<Event>();

    JsonReader reader = new JsonReader(new InputStreamReader(json));
    reader.beginObject();//  w  w  w .j av  a 2  s . co m
    if (reader.nextName().equals("events")) {
        reader.beginArray();
        while (reader.peek() != JsonToken.END_ARRAY) {
            Event event = gsonBuilder.create().fromJson(reader, Event.class);
            events.add(event);
        }
        reader.endArray();
        reader.endObject();
    }
    return new EventList(events);
}

From source file:org.gw2InfoViewer.services.json.typeadaptors.EventAdapter.java

License:Open Source License

@Override
public Event read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/* w ww . ja v a 2  s.  c  o  m*/
        return null;
    }

    Event event = new Event();

    reader.beginObject();
    reader.nextName(); //world id
    event.setWorldId(reader.nextInt());
    reader.nextName(); //map id
    event.setMapId(reader.nextInt());
    reader.nextName(); //event id
    event.setEventId(reader.nextString());
    reader.nextName();
    event.setState(EventState.valueOf(reader.nextString())); //state
    reader.endObject();

    return event;
}

From source file:org.hibernate.search.backend.elasticsearch.document.model.impl.esnative.ElasticsearchRoutingTypeJsonAdapter.java

License:LGPL

@Override
public RoutingType read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();// w w w .ja  va  2 s  .co m
        return null;
    }

    RoutingType value = null;
    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        switch (name) {
        case "required":
            if (in.nextBoolean()) {
                value = RoutingType.REQUIRED;
            } else {
                value = RoutingType.OPTIONAL;
            }
            break;
        default:
            throw new AssertionFailure(
                    "Unexpected property for attribute of type " + RoutingType.class + ": " + name);
        }
    }

    return value;
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractExtraPropertiesJsonAdapter.java

License:LGPL

@Override
public T read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from  w w  w  . j  a  v  a  2  s . com*/
        return null;
    }

    T instance = createInstance();
    try {
        in.beginObject();
        while (in.hasNext()) {
            String name = in.nextName();
            FieldAdapter<? super T> fieldAdapter = fieldAdapters.get(name);
            if (fieldAdapter == null) {
                extraPropertyAdapter.readOne(in, name, instance);
            } else {
                fieldAdapter.read(in, instance);
            }
        }
        in.endObject();
    } catch (IllegalStateException e) {
        throw new JsonSyntaxException(e);
    }

    return instance;
}

From source file:org.hillview.table.rows.GuessSchema.java

License:Open Source License

/**
 * Throws if the value is not valid JSON
 * The gson parser is not strict enough: it parses
 * unquoted strings as JSON.String, so we have to do this manually.
 * Returns true if this is a complex json value, false otherwise.
 *//*w w  w  .ja  v a  2s. c o  m*/
private static boolean isJsonValid(final JsonReader jsonReader) throws IOException {
    JsonToken token;
    boolean isComplex = false;
    loop: while ((token = jsonReader.peek()) != JsonToken.END_DOCUMENT && token != null) {
        switch (token) {
        case BEGIN_ARRAY:
            isComplex = true;
            jsonReader.beginArray();
            break;
        case END_ARRAY:
            isComplex = true;
            jsonReader.endArray();
            break;
        case BEGIN_OBJECT:
            isComplex = true;
            jsonReader.beginObject();
            break;
        case END_OBJECT:
            isComplex = true;
            jsonReader.endObject();
            break;
        case NAME:
            jsonReader.nextName();
            break;
        case STRING:
        case NUMBER:
        case BOOLEAN:
        case NULL:
            jsonReader.skipValue();
            break;
        case END_DOCUMENT:
            break loop;
        default:
            throw new AssertionError(token);
        }
    }
    return isComplex;
}

From source file:org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.files.JsonFile.java

License:Open Source License

/**
 * Read the config File into RAM.// www  .  j a  v a 2 s.  com
 */
public void readFile() {
    try {
        JsonReader reader = new JsonReader(new FileReader(jsonFile));

        reader.beginObject();

        while (reader.hasNext())
            jsonFileContent.put(reader.nextName(), reader.nextString());

        reader.endObject();
        reader.close();
    } catch (IOException ex) {
        IMP.log.error("JsonFile.java > readFile() > Unable to read " + jsonFile + ". Disabling this plugin ..");
        IMP.log.error("JsonFile.java > readFile() > Error: " + ex.getMessage());
        IMP.disable();
    }
}

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 ww  . j a  v a2s .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 w w .  j a v  a2s . co 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();/*  w ww  .j av a2  s . c  o m*/
    JsonToken token = reader.peek();
    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 w  w.j a  va2  s.c om*/
    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();
}