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.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();
    in.nextName();/*ww  w  .  j a  v  a2 s  .  c  om*/
    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 {//from  www . j a v  a  2s  . com
        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 .jav  a 2  s  .  co 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}//from   w  w w.jav a 2  s  .  c  om
 *
 * @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   ww  w  .j a  v  a 2  s  . com*/
    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.AbstractEntitySerializer.java

License:Open Source License

protected void beginRead(final JsonReader in) throws IOException {
    in.beginObject();
}

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

License:Open Source License

/**
 * {@inheritDoc}//from   ww  w  .ja  v  a 2  s  .  co  m
 *
 * @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
 */
@Override
public KomodoConnectionAttributes read(final JsonReader in) throws IOException {
    final KomodoConnectionAttributes ConnectionAttr = createEntity();
    in.beginObject();

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

        switch (name) {
        case KomodoConnectionAttributes.JNDI_LABEL:
            ConnectionAttr.setJndi(in.nextString());
            break;
        case KomodoConnectionAttributes.JDBC_LABEL:
            ConnectionAttr.setJdbc(in.nextBoolean());
            break;
        case KomodoConnectionAttributes.DRIVER_LABEL:
            ConnectionAttr.setDriver(in.nextString());
            break;
        case KomodoConnectionAttributes.PARAMETERS_LABEL:
            Map<String, String> parameters = BUILDER.fromJson(in, Map.class);
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                ConnectionAttr.setParameter(parameter.getKey(), parameter.getValue());
            }
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return ConnectionAttr;
}

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

License:Open Source License

/**
 * {@inheritDoc}// w  w  w  .  j  a  v  a 2s  . c  o  m
 *
 * @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
 */
@Override
public KomodoDataserviceUpdateAttributes read(final JsonReader in) throws IOException {
    final KomodoDataserviceUpdateAttributes updateAttrs = new KomodoDataserviceUpdateAttributes();
    in.beginObject();

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

        switch (name) {
        case KomodoDataserviceUpdateAttributes.DATASERVICE_NAME_LABEL:
            updateAttrs.setDataserviceName(in.nextString());
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_TABLE_PATH_LABEL:
            updateAttrs.setTablePath(in.nextString());
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_MODEL_SOURCE_PATH_LABEL:
            updateAttrs.setModelSourcePath(in.nextString());
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_COLUMN_NAMES_LABEL:
            List<String> colNames = BUILDER.fromJson(in, List.class);
            updateAttrs.setColumnNames(colNames);
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_RH_TABLE_PATH_LABEL:
            updateAttrs.setRhTablePath(in.nextString());
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_RH_MODEL_SOURCE_PATH_LABEL:
            updateAttrs.setRhModelSourcePath(in.nextString());
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_RH_COLUMN_NAMES_LABEL:
            List<String> rhColNames = BUILDER.fromJson(in, List.class);
            updateAttrs.setRhColumnNames(rhColNames);
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_JOIN_TYPE_LABEL:
            updateAttrs.setJoinType(in.nextString());
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_CRITERIA_PREDICATES_LABEL:
            List<Map<String, String>> predicates = BUILDER.fromJson(in, List.class);
            updateAttrs.setCriteriaPredicates(predicates);
            break;
        case KomodoDataserviceUpdateAttributes.DATASERVICE_VIEW_DDL_LABEL:
            updateAttrs.setViewDdl(in.nextString());
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return updateAttrs;
}

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

License:Open Source License

/**
 * {@inheritDoc}/*w ww. j a v a  2 s  .com*/
 *
 * @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
 */
@Override
public KomodoDataSourceJdbcTableAttributes read(final JsonReader in) throws IOException {
    final KomodoDataSourceJdbcTableAttributes updateAttrs = new KomodoDataSourceJdbcTableAttributes();
    in.beginObject();

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

        switch (name) {
        case KomodoDataSourceJdbcTableAttributes.DATA_SOURCE_NAME_LABEL:
            updateAttrs.setDataSourceName(in.nextString());
            break;
        case KomodoDataSourceJdbcTableAttributes.CATALOG_FILTER_LABEL:
            updateAttrs.setCatalogFilter(in.nextString());
            break;
        case KomodoDataSourceJdbcTableAttributes.SCHEMA_FILTER_LABEL:
            updateAttrs.setSchemaFilter(in.nextString());
            break;
        case KomodoDataSourceJdbcTableAttributes.TABLE_FILTER_LABEL:
            updateAttrs.setTableFilter(in.nextString());
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return updateAttrs;
}

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

License:Open Source License

/**
 * {@inheritDoc}/*from   w  w w  .  j  a  v a 2  s .c o m*/
 *
 * @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
 */
@Override
public KomodoFileAttributes read(final JsonReader in) throws IOException {
    final KomodoFileAttributes fileAttr = new KomodoFileAttributes();
    in.beginObject();

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

        if (readContent(in, fileAttr, name) != null)
            continue;

        switch (name) {
        case KomodoFileAttributes.NAME_LABEL:
            fileAttr.setName(in.nextString());
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return fileAttr;
}