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.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseLatestVersion(JsonReader reader, CordovaRegistryPluginInfo plugin) throws IOException {
    reader.beginObject();/*from   w  w  w .  ja va  2  s.  c om*/
    JsonToken token = reader.peek();
    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 2s .  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 boolean nnn(JsonReader reader, JsonToken token, AtomicReference<String> name) throws IOException {
    if (token == JsonToken.NAME) {
        String name2 = reader.nextName();
        if (nameChoices.contains(name2)) {
            name.set(name2);//from www .  j a va  2 s.  com
            return false;
        }
    }
    return true;

}

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();// w w w . ja v  a  2 s.c om
    while (in.hasNext()) {
        String claimName = in.nextName();
        String claimValue = in.nextString();
        builder.addClaim(claimName, claimValue);
    }
    in.endObject();
    return builder.build();
}

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

License:Apache License

@Override
public Header read(JsonReader in) throws IOException {
    Header.Builder builder = new Header.Builder();
    in.beginObject();//from   w w  w .  j ava 2  s . co m
    in.nextName();
    builder.signerAlgorithm(in.nextString());
    in.nextName();
    builder.type(in.nextString());
    in.endObject();
    return builder.build();
}

From source file:org.kairosdb.client.AbstractClient.java

License:Apache License

private List<String> readNameQueryResponse(InputStream stream) throws IOException {
    List<String> list = new ArrayList<String>();
    JsonReader reader = new JsonReader(new InputStreamReader(stream, "UTF-8"));

    try {/*  w  w w . ja va  2  s .  c  o m*/
        reader.beginObject();
        String container = reader.nextName();
        if (container.equals("results")) {
            reader.beginArray();
            while (reader.hasNext()) {
                list.add(reader.nextString());
            }
            reader.endArray();
            reader.endObject();
        }
    } finally {
        reader.close();
    }

    return list;
}

From source file:org.kairosdb.util.ResponseToMetricConverter.java

License:Apache License

public void convert(InputStream inputStream, OutputStream outStream) throws IOException {

    JsonReader reader = new JsonReader(new InputStreamReader(inputStream));
    JsonWriter writer = new JsonWriter(new OutputStreamWriter(outStream));

    try {/*from  w w  w .j  a va  2s.c  o  m*/
        writer.beginArray();

        // Queries array
        reader.beginObject();
        while (reader.hasNext()) {
            String token = reader.nextName();
            if (token.equals("queries")) {
                reader.beginArray();

                while (reader.hasNext()) {
                    reader.beginObject();
                    token = reader.nextName();
                    if (token.equals("results")) {
                        parseResults(reader, writer);
                    }
                    reader.endObject();
                }

                reader.endArray();
            }
        }
        reader.endObject();

        writer.endArray();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        reader.close();
        writer.close();
    }
}

From source file:org.komodo.rest.json.LinkSerializer.java

License:Open Source License

/**
 * {@inheritDoc}/* w  ww  .  jav a2s.  co m*/
 *
 * @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
 */
@Override
public RestLink read(final JsonReader in) throws IOException {
    final RestLink link = new RestLink();
    in.beginObject();

    while (in.hasNext()) {
        final String name = in.nextName();

        switch (name) {
        case JsonConstants.HREF:
            final String uri = in.nextString();
            link.setHref(UriBuilder.fromUri(uri).build());
            break;
        case JsonConstants.REL:
            final String rel = in.nextString();
            link.setRel(LinkType.fromString(rel));
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    if (!isComplete(link)) {
        throw new IOException(Messages.getString(INCOMPLETE_JSON, RestVdbImport.class.getSimpleName()));
    }

    return link;
}

From source file:org.komodo.rest.json.RestPropertySerializer.java

License:Open Source License

@Override
public RestProperty read(JsonReader in) throws IOException {

    String propName = null;//from   w  w  w  .ja  va  2  s . c o  m
    Object propValue = null;

    in.beginObject();

    while (in.hasNext()) {
        String name = in.nextName();
        if (RestProperty.NAME_LABEL.equals(name))
            propName = in.nextString();
        else if (RestProperty.VALUE_LABEL.equals(name)) {
            JsonToken token = in.peek();
            switch (token) {
            case BOOLEAN:
                propValue = in.nextBoolean();
                break;
            case NUMBER: {
                double value = in.nextDouble();
                if (value % 1 == 0)
                    propValue = (int) value;
                else
                    propValue = value;
                break;
            }
            case STRING:
                propValue = in.nextString();
                break;
            case NULL:
                in.nextNull();
                propValue = null;
                break;
            case BEGIN_ARRAY:
                final Object[] value = BUILDER.fromJson(in, Object[].class);

                //
                // BUILDER always converts json numbers to double regardless
                // of them being integers so need to do some checking and on-the-fly
                // conversion
                //
                for (int i = 0; i < value.length; ++i) {
                    if (value[i] instanceof Double && ((double) value[i] % 1) == 0)
                        value[i] = ((Double) value[i]).intValue();
                }

                propValue = value;
                break;
            default:
                throw new IOException(Messages.getString(Messages.Error.UNEXPECTED_JSON_TOKEN, name));
            }
        }
    }

    in.endObject();

    RestProperty property = new RestProperty(propName, propValue);
    if (!isComplete(property))
        throw new IOException(
                Messages.getString(Messages.Error.INCOMPLETE_JSON, RestProperty.class.getSimpleName()));

    return property;
}

From source file:org.komodo.rest.relational.json.BasicEntitySerializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w  ww  . j  ava2 s  .  c om
 *
 * @see org.komodo.rest.relational.json.AbstractEntitySerializer#read(com.google.gson.stream.JsonReader)
 */
@Override
public T read(final JsonReader in) throws IOException {
    final T entity = createEntity();

    beginRead(in);

    while (in.hasNext()) {
        final String name = in.nextName();

        if (readExtension(name, entity, in) != null)
            continue;

        if (PROPERTIES.equals(name))
            readProperties(in, entity);
        else if (LINKS.equals(name))
            readLinks(in, entity);
        else {
            JsonToken token = in.peek();
            switch (token) {
            case BOOLEAN:
                entity.addTuple(name, in.nextBoolean());
                break;
            case NUMBER: {
                double value = in.nextDouble();
                if (value % 1 == 0)
                    entity.addTuple(name, (int) value);
                else
                    entity.addTuple(name, value);
                break;
            }
            case STRING:
                entity.addTuple(name, in.nextString());
                break;
            case NULL:
                in.nextNull();
                entity.addTuple(name, null);
                break;
            case BEGIN_ARRAY:
                final Object[] value = BUILDER.fromJson(in, Object[].class);

                //
                // BUILDER always converts json numbers to double regardless
                // of them being integers so need to do some checking and on-the-fly
                // conversion
                //
                for (int i = 0; i < value.length; ++i) {
                    if (value[i] instanceof Double && ((double) value[i] % 1) == 0)
                        value[i] = ((Double) value[i]).intValue();
                }

                entity.addTuple(name, value);
                break;
            default:
                throw new IOException(Messages.getString(Messages.Error.UNEXPECTED_JSON_TOKEN, name));
            }
        }
    }

    if (!isComplete(entity)) {
        throw new IOException(Messages.getString(INCOMPLETE_JSON, getClass().getSimpleName()));
    }

    endRead(in);
    return entity;
}