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

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

Introduction

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

Prototype

public double nextDouble() throws IOException 

Source Link

Document

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

Usage

From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Collection<Object> parseArray(final JsonReader jsonReader) throws IOException {
    assert null != jsonReader : "DEBUG: The json Reader is null";

    Collection<Object> l = new ArrayList<>();
    JsonToken tok;//from   www . ja  va 2s  .  c  o m

    //
    while (jsonReader.hasNext()) {

        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            l.add(parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            l.add(parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            l.add(jsonReader.nextString());
            break;
        case BOOLEAN:
            l.add(jsonReader.nextBoolean());
            break;
        case NUMBER:
            l.add(jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            l.add(null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return l;
}

From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Map<String, Object> parseObject(final JsonReader jsonReader) throws IOException {

    Map<String, Object> map = new HashMap<>();
    String tempKey;/*from ww  w .ja  va 2 s. c  o  m*/
    JsonToken tok;
    //
    while (jsonReader.hasNext()) {

        // since we want to restore a map we assue a key/value pair
        tempKey = jsonReader.nextName();
        //
        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            map.put(tempKey, parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            map.put(tempKey, parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            map.put(tempKey, jsonReader.nextString());
            break;
        case BOOLEAN:
            map.put(tempKey, jsonReader.nextBoolean());
            break;
        case NUMBER:
            map.put(tempKey, jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            map.put(tempKey, null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return map;
}

From source file:io.grpc.internal.JsonParser.java

License:Apache License

private static Object parseRecursive(JsonReader jr) throws IOException {
    checkState(jr.hasNext(), "unexpected end of JSON");
    switch (jr.peek()) {
    case BEGIN_ARRAY:
        return parseJsonArray(jr);
    case BEGIN_OBJECT:
        return parseJsonObject(jr);
    case STRING:/*from  w w w.  j a  v  a 2 s. c  o  m*/
        return jr.nextString();
    case NUMBER:
        return jr.nextDouble();
    case BOOLEAN:
        return jr.nextBoolean();
    case NULL:
        return parseJsonNull(jr);
    default:
        throw new IllegalStateException("Bad token: " + jr.getPath());
    }
}

From source file:it.units.inginf.male.outputs.gson.DoubleTypeAdapter.java

License:Open Source License

@Override
public Double read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*ww  w . j  a v a  2  s .c o m*/
        return null;
    }
    Double nextDouble;
    try {
        //nextDouble parses as a double or fallback to parsing a string using Java parseDouble which MANAGES NaN
        nextDouble = in.nextDouble();
    } catch (NumberFormatException ex) {
        nextDouble = Double.NaN;
    }
    return nextDouble;
}

From source file:net.visualillusionsent.newu.NewUStation.java

License:Open Source License

NewUStation(String stationName, JsonReader reader) throws IOException {
    String tempName = stationName == null ? UUID.randomUUID().toString() : stationName;
    this.discoverers = Collections.synchronizedList(new ArrayList<String>());

    this.station = new Location(0, 0, 0); // Initialize
    while (reader.hasNext()) {
        String object = reader.nextName();

        if (object.equals("Name")) {
            tempName = reader.nextString();
        }/*from   www.  j  av  a 2  s  . co  m*/
        if (object.equals("World")) {
            station.setWorldName(reader.nextString());
        } else if (object.equals("Dimension")) {
            station.setType(DimensionType.fromName(reader.nextString()));
        } else if (object.equals("X")) {
            station.setX(reader.nextDouble());
        } else if (object.equals("Y")) {
            station.setY(reader.nextDouble());
        } else if (object.equals("Z")) {
            station.setZ(reader.nextDouble());
        } else if (object.equals("RotX")) {
            station.setRotation((float) reader.nextDouble());
        } else if (object.equals("RotY")) {
            station.setPitch((float) reader.nextDouble());
        } else {
            reader.skipValue(); // Unknown
        }
    }
    this.name = tempName;
}

From source file:ninja.leaping.configurate.gson.GsonConfigurationLoader.java

License:Apache License

private void parseValue(JsonReader parser, ConfigurationNode node) throws IOException {
    JsonToken token = parser.peek();/*  w ww  .java2s. co m*/
    switch (token) {
    case BEGIN_OBJECT:
        parseObject(parser, node);
        break;
    case BEGIN_ARRAY:
        parseArray(parser, node);
        break;
    case NUMBER:
        double nextDouble = parser.nextDouble();
        int nextInt = (int) nextDouble;
        long nextLong = (long) nextDouble;
        if (nextInt == nextDouble) {
            node.setValue(nextInt); // They don't do much for us here in Gsonland
        } else if (nextLong == nextDouble) {
            node.setValue(nextLong);
        } else {
            node.setValue(nextDouble);
        }
        break;
    case STRING:
        node.setValue(parser.nextString());
        break;
    case BOOLEAN:
        node.setValue(parser.nextBoolean());
        break;
    case NULL: // Ignored values
    case NAME:
        break;
    default:
        throw new IOException("Unsupported token type: " + token);
    }
}

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;//from  w  w w  . java  2s  .  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.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 w w  w  . j  a va  2 s . co 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.client.ClientIfcModel.java

License:Open Source License

@SuppressWarnings({ "unused", "resource" })
public void getGeometry(IfcProduct ifcProduct) {
    try {/*from  w  w w  .j a  va  2 s .  c  om*/
        Long downloadByOids = bimServerClient.getBimsie1ServiceInterface().downloadByOids(
                Collections.singleton(roid), Collections.singleton(ifcProduct.getOid()),
                getJsonGeometrySerializerOid(), true, false);
        InputStream downloadData = bimServerClient.getDownloadData(downloadByOids,
                getJsonGeometrySerializerOid());
        JsonReader jsonReader = new JsonReader(new InputStreamReader(downloadData));
        jsonReader.beginObject();
        if (jsonReader.nextName().equals("geometry")) {
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                jsonReader.beginObject();
                if (jsonReader.nextName().equals("material")) {
                    String material = jsonReader.nextString();
                    if (jsonReader.nextName().equals("type")) {
                        String type = jsonReader.nextString();
                        if (jsonReader.nextName().equals("coreId")) {
                            String coreid = jsonReader.nextString();
                            if (jsonReader.nextName().equals("primitive")) {
                                String primitive = jsonReader.nextString();
                                if (jsonReader.nextName().equals("positions")) {
                                    jsonReader.beginArray();
                                    while (jsonReader.hasNext()) {
                                        double position = jsonReader.nextDouble();
                                    }
                                    jsonReader.endArray();
                                    if (jsonReader.nextName().equals("normals")) {
                                        jsonReader.beginArray();
                                        while (jsonReader.hasNext()) {
                                            double normal = jsonReader.nextDouble();
                                        }
                                        jsonReader.endArray();
                                        if (jsonReader.nextName().equals("nrindices")) {
                                            int nrindices = jsonReader.nextInt();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                jsonReader.endObject();
            }
            jsonReader.endArray();
        }
        jsonReader.endObject();
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}

From source file:org.bimserver.deserializers.JsonDeserializer.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.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 {/*www  . ja  v a2 s.co  m*/
            throw new RuntimeException();
        }
    } else if (eClassifier instanceof EEnum) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
    } else {
        throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName());
    }
}