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

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

Introduction

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

Prototype

public boolean nextBoolean() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#BOOLEAN boolean value of the next token, consuming it.

Usage

From source file:org.jsfr.json.GsonParser.java

License:Open Source License

private ResumableParser createResumableParserImpl(Reader reader, SurfingContext context) {

    final JsonReader jsonReader = this.jsonReaderFactory.createJsonReader(reader);
    final JsonProvider jsonProvider = context.getConfig().getJsonProvider();

    AbstractPrimitiveHolder stringHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override//from w w  w  . jav  a  2  s . com
        public Object doGetValue() throws IOException {
            return jsonProvider.primitive(jsonReader.nextString());
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    AbstractPrimitiveHolder numberHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override
        public Object doGetValue() throws IOException {
            return jsonProvider.primitive(jsonReader.nextDouble());
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    AbstractPrimitiveHolder booleanHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override
        public Object doGetValue() throws IOException {
            return jsonProvider.primitive(jsonReader.nextBoolean());
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    AbstractPrimitiveHolder nullHolder = new AbstractPrimitiveHolder(context.getConfig()) {
        @Override
        public Object doGetValue() throws IOException {
            jsonReader.nextNull();
            return jsonProvider.primitiveNull();
        }

        @Override
        public void doSkipValue() throws IOException {
            jsonReader.skipValue();
        }
    };
    return new GsonResumableParser(jsonReader, context, stringHolder, numberHolder, booleanHolder, nullHolder);
}

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  ava 2 s. co  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 ww  w. j ava2s  .  c  o  m
 *
 * @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;
}

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

License:Open Source License

/**
 * {@inheritDoc}/*from   w  w  w  .ja  v  a 2 s.c  o  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.ImportExportStatusSerializer.java

License:Open Source License

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

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

        switch (name) {
        case ImportExportStatus.NAME_LABEL:
            status.setName(in.nextString());
            break;
        case ImportExportStatus.TYPE_LABEL:
            status.setType(in.nextString());
            break;
        case ImportExportStatus.SUCCESS_LABEL:
            status.setSuccess(in.nextBoolean());
            break;
        case ImportExportStatus.DOWNLOADABLE_LABEL:
            status.setDownloadable(in.nextBoolean());
            break;
        case ImportExportStatus.CONTENT_LABEL:
            status.setContent(in.nextString());
            break;
        case ImportExportStatus.MESSAGE_LABEL:
            status.setMessage(in.nextString());
            break;
        case ImportExportStatus.DOWNLOADABLE_SIZE_LABEL:
            status.setDownloadableSize(in.nextLong());
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return status;
}

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

License:Open Source License

@Override
public RestStorageTypeDescriptor read(JsonReader in) throws IOException {
    final RestStorageTypeDescriptor storageTypeDescriptor = new RestStorageTypeDescriptor();
    in.beginObject();/*from w w  w .  j a v a  2s.co  m*/

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

        switch (name) {
        case RestStorageTypeDescriptor.NAME_LABEL:
            storageTypeDescriptor.setName(in.nextString());
            break;
        case RestStorageTypeDescriptor.DESCRIPTION_LABEL:
            storageTypeDescriptor.setDescription(in.nextString());
            break;
        case RestStorageTypeDescriptor.REQUIRED_LABEL:
            storageTypeDescriptor.setRequired(in.nextBoolean());
            break;
        case RestStorageTypeDescriptor.ENCODED_LABEL:
            storageTypeDescriptor.setEncoded(in.nextBoolean());
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return storageTypeDescriptor;
}

From source file:org.komodo.rest.relational.json.TeiidAttributesSerializer.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 KomodoTeiidAttributes read(final JsonReader in) throws IOException {
    final KomodoTeiidAttributes teiidAttrs = new KomodoTeiidAttributes();
    in.beginObject();

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

        switch (name) {
        case KomodoTeiidAttributes.ADMIN_USER_LABEL:
            teiidAttrs.setAdminUser(in.nextString());
            break;
        case KomodoTeiidAttributes.ADMIN_PASSWD_LABEL:
            teiidAttrs.setAdminPasswd(in.nextString());
            break;
        case KomodoTeiidAttributes.ADMIN_PORT_LABEL:
            teiidAttrs.setAdminPort(in.nextInt());
            break;
        case KomodoTeiidAttributes.ADMIN_SECURE_LABEL:
            teiidAttrs.setAdminSecure(in.nextBoolean());
            break;
        case KomodoTeiidAttributes.JDBC_USER_LABEL:
            teiidAttrs.setJdbcUser(in.nextString());
            break;
        case KomodoTeiidAttributes.JDBC_PASSWD_LABEL:
            teiidAttrs.setJdbcPasswd(in.nextString());
            break;
        case KomodoTeiidAttributes.JDBC_PORT_LABEL:
            teiidAttrs.setJdbcPort(in.nextInt());
            break;
        case KomodoTeiidAttributes.JDBC_SECURE_LABEL:
            teiidAttrs.setJdbcSecure(in.nextBoolean());
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return teiidAttrs;
}

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

License:Open Source License

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

    beginRead(in);

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

        if (RestTeiidVdbStatus.VDBS_LABEL.equals(name)) {
            final RestTeiidVdbStatusVdb[] vdbStatuses = BUILDER.fromJson(in, RestTeiidVdbStatusVdb[].class);
            entity.setVdbProperties(Arrays.asList(vdbStatuses));
        } 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:
                entity.addTuple(name, in.nextInt());
                break;
            case STRING:
                entity.addTuple(name, in.nextString());
                break;
            case NULL:
                in.nextNull();
                entity.addTuple(name, null);
                break;
            case BEGIN_ARRAY:
                final String[] value = BUILDER.fromJson(in, String[].class);
                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;
}

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

License:Open Source License

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

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

        switch (name) {
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_NAME:
            vdb.setName(in.nextString());
            break;
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_DEPLOYED_NAME:
            vdb.setDeployedName(in.nextString());
            break;
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_VERSION:
            vdb.setVersion(in.nextString());
            break;
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_ACTIVE:
            vdb.setActive(in.nextBoolean());
            break;
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_LOADING:
            vdb.setLoading(in.nextBoolean());
            break;
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_FAILED:
            vdb.setFailed(in.nextBoolean());
            break;
        case RestTeiidVdbStatusVdb.TEIID_VDB_STATUS_ERROR:
            final String[] errors = BUILDER.fromJson(in, String[].class);
            vdb.setErrors(Arrays.asList(errors));
            break;
        default:
            throw new IOException(Messages.getString(UNEXPECTED_JSON_TOKEN, name));
        }
    }

    in.endObject();

    return vdb;
}

From source file:org.lanternpowered.server.data.persistence.json.JsonDataFormat.java

License:MIT License

@Nullable
private static Object read0(JsonReader reader) throws IOException {
    final JsonToken token = reader.peek();
    switch (token) {
    case BEGIN_OBJECT:
        return readContainer(reader);
    case BEGIN_ARRAY:
        return readArray(reader);
    case BOOLEAN:
        return reader.nextBoolean();
    case NULL://from  ww w  .ja  v  a 2  s  .  c o  m
        reader.nextNull();
        return null;
    case STRING:
        return readString(reader);
    case NUMBER:
        return readNumber(reader);
    default:
        throw new IOException("Unexpected token: " + token);
    }
}