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: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  ww  w  .j  a  va2s.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());
    }
}

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  ww . ja  v  a  2 s . co  m*/
        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;//w w  w  .  j  av  a  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}/* www. j  av a2 s . com*/
 *
 * @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.lanternpowered.server.data.persistence.json.JsonDataFormat.java

License:MIT License

private static Number readNumber(JsonReader reader) throws IOException {
    // Similar to https://github.com/zml2008/configurate/blob/master/configurate-gson/src/main/java/ninja/leaping/configurate/gson/GsonConfigurationLoader.java#L113
    // Not sure what's the best way to detect the type of number
    double nextDouble = reader.nextDouble();
    int nextInt = (int) nextDouble;
    if (nextInt == nextDouble) {
        return nextInt;
    }/*from w  w w  .j a  v  a 2  s .c  o m*/

    long nextLong = (long) nextDouble;
    if (nextLong == nextDouble) {
        return nextLong;
    }

    return nextDouble;
}

From source file:org.openstreetmap.josm.plugins.openstreetcam.service.photo.adapter.ReaderUtil.java

License:LGPL

static Double readDouble(final JsonReader reader) throws IOException {
    Double value = null;//from  w ww  . ja  va  2  s  .co  m
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
    } else {
        value = reader.nextDouble();
    }
    return value;
}

From source file:org.openstreetmap.josm.plugins.openstreetcam.service.photo.adapter.ReaderUtil.java

License:LGPL

/**
 * Reads a geometry that has the following format: [[lat1,lon1], [lat2,lon2],...[latn,lonn]].
 *
 * @param reader a {@code JsonReader} object
 * @return a list of {@code LatLon} objects
 * @throws IOException if the read operation failed
 *//*from www .j  av  a 2  s  . co  m*/
static List<LatLon> readGeometry(final JsonReader reader) throws IOException {
    final List<LatLon> geometry = new ArrayList<>();
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
    } else {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginArray();
            final Double lat = reader.nextDouble();
            final Double lon = reader.nextDouble();
            geometry.add(new LatLon(lat, lon));
            reader.endArray();
        }
        reader.endArray();
    }
    return geometry;
}

From source file:org.openstreetmap.josm.plugins.openstreetcam.service.PhotoTypeAdapter.java

License:Apache License

private Double readDouble(final JsonReader reader) throws IOException {
    Double value = null;/*from   w  w w  . j a v  a  2s .  c  o  m*/
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
    } else {
        value = reader.nextDouble();
    }
    return value;
}

From source file:org.wso2.carbon.governance.rest.api.internal.JSONMessageBodyReader.java

License:Open Source License

/**
 * Traverses through a json object and maps the keys and values to a {@link Map}
 *
 * @param reader        {@link JsonReader}
 * @param map           map that the values to be added.
 * @param token         {@link JsonToken}
 * @param isArray       whether the object is inside a json array
 * @throws IOException  If unable to parse the json object
 *///from w  ww.j ava 2s . c o m
protected void handleObject(JsonReader reader, Map<String, Object> map, JsonToken token, boolean isArray)
        throws IOException {
    String key = null;
    while (true) {
        if (token == null) {
            token = reader.peek();
        }

        if (JsonToken.BEGIN_OBJECT.equals(token)) {
            reader.beginObject();

        } else if (JsonToken.END_OBJECT.equals(token)) {
            reader.endObject();

        } else if (JsonToken.NAME.equals(token)) {
            key = reader.nextName();

        } else if (JsonToken.STRING.equals(token)) {
            String value = reader.nextString();
            handleValue(key, value, map, isArray);
            key = null;

        } else if (JsonToken.NUMBER.equals(token)) {
            Double value = reader.nextDouble();
            handleValue(key, value, map, isArray);
            key = null;

        } else if (token.equals(JsonToken.BEGIN_ARRAY)) {
            Map<String, Object> values = handleArray(reader);
            if (key != null) {
                map.put(key, values);
            }

        } else {
            reader.skipValue();
        }

        if (reader.hasNext()) {
            token = reader.peek();
        } else {
            break;
        }
    }
}

From source file:persistance.JSONHandler.java

private static Bee parseUnspecifiedBee(JsonReader jsonReader) throws IOException {
    int primaryFertility = 0;
    double primaryLifespan = 0;
    double primaryPollination = 0;
    String primarySpecies = null;
    double primaryWorkspeed = 0;
    int secondaryFertility = 0;
    double secondaryLifespan = 0;
    double secondaryPollination = 0;
    String secondarySpecies = null;
    double secondaryWorkspeed = 0;

    jsonReader.beginObject();/*from w ww. ja  v a 2 s .  co  m*/
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
        case "primaryFertility":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryFertility = jsonReader.nextInt();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primaryLifespan":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryLifespan = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primaryPollination":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryPollination = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primarySpecies":
            primarySpecies = jsonReader.nextString();
            break;
        case "primaryWorkspeed":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryWorkspeed = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryFertility":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryFertility = jsonReader.nextInt();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryLifespan":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryLifespan = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryPollination":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryPollination = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondarySpecies":
            secondarySpecies = jsonReader.nextString();
            break;
        case "secondaryWorkspeed":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryWorkspeed = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        default:
            jsonReader.skipValue();
            break;
        }
    }
    jsonReader.endObject();

    Random random = new Random();

    if (random.nextInt(50) == 0) {
        return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed,
                secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies,
                secondaryWorkspeed);
    }

    return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed,
            secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed);
}