Example usage for com.google.gson.stream JsonToken BOOLEAN

List of usage examples for com.google.gson.stream JsonToken BOOLEAN

Introduction

In this page you can find the example usage for com.google.gson.stream JsonToken BOOLEAN.

Prototype

JsonToken BOOLEAN

To view the source code for com.google.gson.stream JsonToken BOOLEAN.

Click Source Link

Document

A JSON true or false .

Usage

From source file:com.sap.core.odata.core.ep.consumer.JsonPropertyConsumer.java

License:Apache License

private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
        final Object typeMapping) throws EdmException, EntityProviderException, IOException {
    final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
    Object value = null;//from  w  ww  .j  ava 2  s . c  o m
    final JsonToken tokenType = reader.peek();
    if (tokenType == JsonToken.NULL) {
        reader.nextNull();
    } else {
        switch (EdmSimpleTypeKind.valueOf(type.getName())) {
        case Boolean:
            if (tokenType == JsonToken.BOOLEAN) {
                value = reader.nextBoolean();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Byte:
        case SByte:
        case Int16:
        case Int32:
            if (tokenType == JsonToken.NUMBER) {
                value = reader.nextInt();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        default:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        }
    }

    final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
    return type.valueOfString((String) value, EdmLiteralKind.JSON, entityPropertyInfo.getFacets(),
            typeMappingClass);
}

From source file:com.splunk.ResultsReaderJson.java

License:Apache License

/**
 * Skip the next value, whether it is atomic or compound, in the JSON
 * stream.//from  w w w  .j  a va2 s .c o  m
 */
private void skipEntity() throws IOException {
    if (jsonReader.peek() == JsonToken.STRING) {
        jsonReader.nextString();
    } else if (jsonReader.peek() == JsonToken.BOOLEAN) {
        jsonReader.nextBoolean();
    } else if (jsonReader.peek() == JsonToken.NUMBER) {
        jsonReader.nextDouble();
    } else if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (jsonReader.peek() == JsonToken.NAME) {
        jsonReader.nextName();
    } else if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.peek() != JsonToken.END_ARRAY) {
            skipEntity();
        }
        jsonReader.endArray();
    } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.peek() != JsonToken.END_OBJECT) {
            skipEntity();
        }
        jsonReader.endObject();
    }
}

From source file:json.tests.reader.JSONStreamReader.java

License:Open Source License

/**
 * {@inheritDoc}/*  www  . j av a2  s .  co  m*/
 */
@Override
public boolean nextBoolean() throws IOException {
    if (this.currentToken == JsonToken.BOOLEAN) {
        boolean tempElementValue = (boolean) this.currentElementValue;
        goToNextToken(false, false);
        return tempElementValue;
    } else {
        throw new IllegalStateException("Expected a BOOLEAN token but found " + this.currentToken);
    }
}

From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java

License:Apache License

private void readName() throws IOException, XMLStreamException {
    nextName();/*from   www . j a  va 2s.  c o  m*/
    tokenType = jsonReader.peek();
    if (tokenType == JsonToken.BEGIN_OBJECT) {
        beginObject();
        state = JsonState.NameName;
    } else if (tokenType == JsonToken.BEGIN_ARRAY) {
        beginArray();
        tokenType = jsonReader.peek();
        if (tokenType == JsonToken.BEGIN_OBJECT) {
            beginObject();
            state = JsonState.NameName;
        } else {
            state = JsonState.NameValue;
        }
    } else if (tokenType == JsonToken.STRING || tokenType == JsonToken.NUMBER || tokenType == JsonToken.BOOLEAN
            || tokenType == JsonToken.NULL) {
        state = JsonState.NameValue;
    }
}

From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java

License:Apache License

private void readValue() throws IOException {
    nextValue();/*from   w  ww. j ava 2 s  .  co  m*/
    tokenType = jsonReader.peek();
    if (tokenType == JsonToken.NAME) {
        state = JsonState.ValueName_END;
    } else if (tokenType == JsonToken.STRING || tokenType == JsonToken.NUMBER || tokenType == JsonToken.BOOLEAN
            || tokenType == JsonToken.NULL) {
        state = JsonState.ValueValue_END;
    } else if (tokenType == JsonToken.END_ARRAY) {
        state = JsonState.ValueEndArray;
    } else if (tokenType == JsonToken.END_OBJECT) {
        state = JsonState.ValueEndObject_END_1;
    }
}

From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java

License:Apache License

private String nextValue() {
    try {/* w  w  w .j av a  2 s .  c om*/
        tokenType = jsonReader.peek();
        JsonObject peek = stackObj.peek();
        String valueType = peek.getValueType();
        if (!validateArgumentTypes(tokenType, valueType)) {
            log.error("Value type miss match, Expected value type - '" + valueType + "', but found - '"
                    + tokenType.toString() + "'");
            throw new IllegalArgumentException("Value type miss match, Expected value type - '" + valueType
                    + "', but found - '" + tokenType.toString() + "'");
        }
        if (tokenType == JsonToken.STRING) {
            value = jsonReader.nextString();
        } else if (tokenType == JsonToken.BOOLEAN) {
            value = String.valueOf(jsonReader.nextBoolean());
        } else if (tokenType == JsonToken.NUMBER) {
            if (valueType.equals("int")) {
                value = String.valueOf(jsonReader.nextInt());
            } else if (valueType.equals("long")) {
                value = String.valueOf(jsonReader.nextLong());
            } else if (valueType.equals("double")) {
                value = String.valueOf(jsonReader.nextDouble());
            } else if (valueType.equals("float")) {
                value = String.valueOf(jsonReader.nextDouble());
            }
        } else if (tokenType == JsonToken.NULL) {
            jsonReader.nextNull();
            value = null;
        } else {
            log.error("Couldn't read the value, Illegal state exception");
            throw new RuntimeException("Couldn't read the value, Illegal state exception");
        }
    } catch (IOException e) {
        log.error("IO error while reading json stream");
        throw new RuntimeException("IO error while reading json stream");
    }
    return value;
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonPropertyConsumer.java

License:Apache License

private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
        final Object typeMapping, final EntityProviderReadProperties readProperties)
        throws EdmException, EntityProviderException, IOException {
    final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
    Object value = null;// ww w . j ava  2 s .c  o m
    final JsonToken tokenType = reader.peek();
    if (tokenType == JsonToken.NULL) {
        reader.nextNull();
    } else {
        switch (EdmSimpleTypeKind.valueOf(type.getName())) {
        case Boolean:
            if (tokenType == JsonToken.BOOLEAN) {
                value = reader.nextBoolean();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Byte:
        case SByte:
        case Int16:
        case Int32:
            if (tokenType == JsonToken.NUMBER) {
                value = reader.nextInt();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Single:
        case Double:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else if (tokenType == JsonToken.NUMBER) {
                value = reader.nextDouble();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        default:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        }
    }

    final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
    final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets()
            ? entityPropertyInfo.getFacets()
            : null;
    return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}

From source file:org.arl.fjage.remote.GenericValueAdapterFactory.java

License:BSD License

public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!rawType.equals(GenericValue.class))
        return null;
    final GenericValueAdapterFactory parent = this;
    return new TypeAdapter<T>() {

        @Override//w w w  . ja  v  a  2s .co m
        public void write(JsonWriter out, T value) throws IOException {
            if (value == null) {
                out.nullValue();
                return;
            }
            Class type = ((GenericValue) value).getType();
            if (type == null) {
                out.nullValue();
                return;
            }
            if (Number.class.isAssignableFrom(type))
                out.value((Number) ((GenericValue) value).getValue());
            else if (type.equals(String.class))
                out.value((String) ((GenericValue) value).getValue());
            else if (type.equals(Boolean.class))
                out.value((Boolean) ((GenericValue) value).getValue());
            else {
                out.beginObject();
                out.name("clazz").value(type.getName());
                out.name("data");
                TypeAdapter delegate = gson.getAdapter(TypeToken.get(type));
                Object v = ((GenericValue) value).getValue();
                delegate.write(out, v);
                out.endObject();
            }
        }

        @Override
        public T read(JsonReader in) throws IOException {
            JsonToken tok = in.peek();
            if (tok == JsonToken.NULL) {
                in.nextNull();
                return null;
            }
            if (tok == JsonToken.NUMBER) {
                String s = in.nextString();
                try {
                    if (s.contains("."))
                        return (T) new GenericValue(Double.parseDouble(s));
                    else
                        return (T) new GenericValue(Long.parseLong(s));
                } catch (NumberFormatException ex) {
                    return (T) new GenericValue(null);
                }
            }
            if (tok == JsonToken.STRING)
                return (T) new GenericValue(in.nextString());
            if (tok == JsonToken.BOOLEAN)
                return (T) new GenericValue(in.nextBoolean());
            if (tok != JsonToken.BEGIN_OBJECT)
                return null;
            TypeToken tt = null;
            GenericValue rv = null;
            in.beginObject();
            while (in.hasNext()) {
                String name = in.nextName();
                if (name.equals("clazz")) {
                    try {
                        Class<?> cls = Class.forName(in.nextString());
                        tt = TypeToken.get(cls);
                    } catch (Exception ex) {
                        // do nothing
                    }
                } else if (name.equals("data") && tt != null) {
                    TypeAdapter delegate = gson.getAdapter(tt);
                    rv = new GenericValue(delegate.read(in));
                } else
                    in.skipValue();
            }
            in.endObject();
            return (T) rv;
        }

    };
}

From source file:org.bimserver.client.ClientIfcModel.java

License:Open Source License

private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException {
    EClassifier eClassifier = eStructuralFeature.getEType();
    if (eClassifier == EcorePackage.eINSTANCE.getEString()) {
        return jsonReader.nextString();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()
            || eClassifier == EcorePackage.eINSTANCE.getEDoubleObject()) {
        return jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEFloat()
            || eClassifier == EcorePackage.eINSTANCE.getEFloatObject()) {
        return (float) jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()
            || eClassifier == EcorePackage.eINSTANCE.getEBooleanObject()) {
        return jsonReader.nextBoolean();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()
            || eClassifier == EcorePackage.eINSTANCE.getEIntegerObject()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) {
        // TODO DOES THIS EVER HAPPEN??
        if (jsonReader.peek() == JsonToken.BOOLEAN) {
            EEnum eEnum = (EEnum) eStructuralFeature.getEType();
            return eEnum.getEEnumLiteral(("" + jsonReader.nextBoolean()).toUpperCase()).getInstance();
        } else {//from ww  w  .j  av a  2 s.c  o  m
            EEnum eEnum = (EEnum) eStructuralFeature.getEType();
            return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
        }
    } else if (eClassifier instanceof EClass) {
        throw new RuntimeException();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEByteArray()) {
        return Base64.decodeBase64(jsonReader.nextString().getBytes(Charsets.UTF_8));
    } else if (eClassifier instanceof EEnum) {
        if (jsonReader.peek() == JsonToken.BOOLEAN) {
            EEnum eEnum = (EEnum) eStructuralFeature.getEType();
            return eEnum.getEEnumLiteral(("" + jsonReader.nextBoolean()).toUpperCase()).getInstance();
        } else {
            EEnum eEnum = (EEnum) eStructuralFeature.getEType();
            return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
        }
    } else {
        throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName());
    }
}

From source file:org.bimserver.emf.SharedJsonDeserializer.java

License:Open Source License

private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException {
    EClassifier eClassifier = eStructuralFeature.getEType();
    if (eClassifier == EcorePackage.eINSTANCE.getEString()) {
        return jsonReader.nextString();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()) {
        return jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()) {
        return jsonReader.nextBoolean();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getELong()) {
        return jsonReader.nextLong();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEIntegerObject()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEByteArray()) {
        return Base64.decodeBase64(jsonReader.nextString());
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDate()) {
        long timestamp = jsonReader.nextLong();
        return new Date(timestamp);
    } else if (eClassifier == EcorePackage.eINSTANCE.getEFloat()) {
        return (float) jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
    } else if (eClassifier instanceof EClass) {
        if (eStructuralFeature.getEType().getName().equals("IfcGloballyUniqueId")) {
            IfcGloballyUniqueId ifcGloballyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId();
            ifcGloballyUniqueId.setWrappedValue(jsonReader.nextString());
            return ifcGloballyUniqueId;
        } else {//from  w  w  w  .  j  a  v  a2s. c  o  m
            throw new RuntimeException();
        }
    } else if (eClassifier instanceof EEnum) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        if (jsonReader.peek() == JsonToken.BOOLEAN) {
            return eEnum.getEEnumLiteral(jsonReader.nextBoolean() ? "TRUE" : "FALSE").getInstance();
        } else {
            return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
        }
    } else {
        throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName());
    }
}