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:com.ibm.og.json.type.FilesizeConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!FilesizeConfig.class.equals(rawType)) {
        return null;
    }/*  w w  w .  j ava  2 s  . c  o m*/

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.NUMBER == in.peek()) {
                return (T) new FilesizeConfig(in.nextDouble());
            }

            return delegate.read(in);
        }

    }.nullSafe();
}

From source file:com.ibm.og.json.type.OperationConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!OperationConfig.class.equals(rawType)) {
        return null;
    }//  w  w  w  .  ja  v a 2  s .  c  o  m

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.NUMBER == in.peek()) {
                return (T) new OperationConfig(in.nextDouble());
            }

            return delegate.read(in);
        }
    }.nullSafe();
}

From source file:com.ibm.watson.developer_cloud.alchemy.v1.util.TaxonomyTypeAdapter.java

License:Open Source License

@Override
public Taxonomy read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//ww  w . ja v  a2 s.  c o m
        return null;
    }

    final Taxonomy taxonomy = new Taxonomy();
    taxonomy.setConfident(true);

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

        if (name.equals("confident")) {
            final String confidentAsString = reader.nextString();
            taxonomy.setConfident(confidentAsString == null || !confidentAsString.equals("no"));
        } else if (name.equals("label")) {
            final String label = reader.nextString();
            taxonomy.setLabel(label);
        } else if (name.equals("score")) {
            final Double score = reader.nextDouble();
            taxonomy.setScore(score);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return taxonomy;
}

From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechTimestampTypeAdapter.java

License:Open Source License

@Override
public SpeechTimestamp read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from  www.java2 s .com
        return null;
    }

    final SpeechTimestamp speechTimestamp = new SpeechTimestamp();
    reader.beginArray();

    if (reader.peek() == JsonToken.STRING) {
        speechTimestamp.setWord(reader.nextString());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechTimestamp.setStartTime(reader.nextDouble());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechTimestamp.setEndTime(reader.nextDouble());
    }

    reader.endArray();
    return speechTimestamp;
}

From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechWordConfidenceTypeAdapter.java

License:Open Source License

@Override
public SpeechWordConfidence read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*  ww  w.j a v  a2  s.  c o m*/
        return null;
    }

    final SpeechWordConfidence speechWordConfidence = new SpeechWordConfidence();
    reader.beginArray();

    if (reader.peek() == JsonToken.STRING) {
        speechWordConfidence.setWord(reader.nextString());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechWordConfidence.setConfidence(reader.nextDouble());
    }

    reader.endArray();
    return speechWordConfidence;
}

From source file:com.ibm.watson.developer_cloud.tradeoff_analytics.v1.util.ColumnTypeAdapter.java

License:Open Source License

@Override
public Column read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from ww w. j  ava  2  s .co  m
        return null;
    }

    ColumnType type = ColumnType.TEXT;
    Goal goal = null;
    Boolean objective = null;

    String key = null, format = null, description = null, fullName = null, low = null, high = null;
    Double significantGain = null, significantLoss = null, insignificantLoss = null;

    List<String> categoricalRange = null, categoricalPreference = null;

    reader.beginObject();

    while (reader.hasNext()) {
        String name = reader.nextName();

        if (name.equals(TYPE2)) {
            type = ColumnType.fromString(reader.nextString());
        } else if (name.equals(KEY)) {
            key = reader.nextString();
        } else if (name.equals(GOAL)) {
            goal = Goal.fromString(reader.nextString());
        } else if (name.equals(IS_OBJECTIVE)) {
            objective = reader.nextBoolean();
        } else if (name.equals(FORMAT)) {
            format = reader.nextString();
        } else if (name.equals(DESCRIPTION)) {
            description = reader.nextString();
        } else if (name.equals(FULL_NAME)) {
            fullName = reader.nextString();
        } else if (name.equals(SIGNIFICANT_GAIN)) {
            significantGain = reader.nextDouble();
        } else if (name.equals(SIGNIFICANT_LOSS)) {
            significantLoss = reader.nextDouble();
        } else if (name.equals(INSIGNIFICANT_LOSS)) {
            insignificantLoss = reader.nextDouble();
        } else if (name.equals("preference")) {
            reader.beginArray();
            categoricalPreference = new ArrayList<String>();
            while (reader.hasNext()) {
                categoricalPreference.add(reader.nextString());
            }
            reader.endArray();
        } else if (name.equals(RANGE)) {
            if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) {
                reader.beginArray();
                categoricalRange = new ArrayList<String>();
                while (reader.hasNext()) {
                    categoricalRange.add(reader.nextString());
                }
                reader.endArray();
            } else {
                reader.beginObject();
                while (reader.hasNext()) {
                    name = reader.nextName();
                    if (name.equals(LOW)) {
                        low = reader.nextString();
                    } else if (name.equals(HIGH)) {
                        high = reader.nextString();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            }
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    Column column;
    if (type == ColumnType.CATEGORICAL) {
        column = new CategoricalColumn();
        if (categoricalRange != null) {
            ((CategoricalColumn) column).setRange(categoricalRange);
        }
        if (categoricalPreference != null) {
            ((CategoricalColumn) column).setRange(categoricalPreference);
        }
    } else if (type == ColumnType.DATETIME) {
        column = new DateColumn();
        if (low != null) {
            try {
                ((DateColumn) column).withRange(DATE_FORMATTER.parse(low), DATE_FORMATTER.parse(high));
            } catch (final ParseException e) {
                LOG.log(Level.SEVERE, "Error parsing the date", e);
            }
        }
    } else if (type == ColumnType.NUMERIC) {
        column = new NumericColumn();
        if (low != null) {
            ((NumericColumn) column).range(Double.valueOf(low), Double.valueOf(high));
        }
    } else {
        column = new TextColumn();
    }

    column.setKey(key);

    if (description != null) {
        column.setDescription(description);
    }

    if (format != null) {
        column.setFormat(format);
    }

    if (objective != null) {
        column.setObjective(objective);
    }

    if (fullName != null) {
        column.setFullName(fullName);
    }

    if (goal != null) {
        column.setGoal(goal);
    }

    if (key != null) {
        column.setKey(key);
    }

    if (significantGain != null) {
        column.setSignificantGain(significantGain);
    }

    if (significantLoss != null) {
        column.setSignificantLoss(insignificantLoss);
    }

    if (insignificantLoss != null) {
        column.setInsignificantLoss(insignificantLoss);
    }

    return column;
}

From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java

License:Open Source License

/**
 * Checks the next {@link JsonToken} to decide the next appropriate parsing method.
 *
 * @param in {@link JsonReader} object used for parsing
 * @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object
 * @throws IOException signals that there has been an IO exception
 *///from www . j a  va  2s  .c o m
private void parseNext(JsonReader in, HashMap<String, Object> objMap) throws IOException {
    JsonToken token = in.peek();

    String lastName = "";
    if (token == JsonToken.NAME) {
        lastName = in.nextName();
        token = in.peek();
    }

    switch (token) {
    case BEGIN_ARRAY:
        parseArray(in, objMap, lastName);
        break;
    case BEGIN_OBJECT:
        parseObject(in, objMap, lastName);
        break;
    case STRING:
        objMap.put(lastName, in.nextString());
        break;
    case NUMBER:
        objMap.put(lastName, in.nextDouble());
        break;
    case BOOLEAN:
        objMap.put(lastName, in.nextBoolean());
        break;
    default:
        throw new IOException("Unexpected JSON token encountered");
    }

    collapseMap(objMap);
}

From source file:com.nridge.core.io.gson.RangeJSON.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into a field range.
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 *//*from   ww  w  .j a v a2 s  .co  m*/
public FieldRange load(JsonReader aReader) throws IOException {
    String jsonName;

    boolean isFirst = true;
    Date firstDate = new Date();
    long firstLong = Long.MIN_VALUE;
    int firstInt = Integer.MIN_VALUE;
    double firstDouble = Double.MIN_VALUE;

    Field.Type rangeType = Field.Type.Text;
    FieldRange fieldRange = new FieldRange();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            rangeType = Field.stringToType(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_DELIMITER_MEMBER_NAME))
            fieldRange.setDelimiterChar(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_VALUE_MEMBER_NAME))
            fieldRange.setItems(StrUtl.expandToList(aReader.nextString(), fieldRange.getDelimiterChar()));
        else if (StringUtils.equals(jsonName, "min")) {
            switch (rangeType) {
            case Long:
                if (isFirst) {
                    isFirst = false;
                    firstLong = aReader.nextLong();
                } else
                    fieldRange = new FieldRange(aReader.nextLong(), firstLong);
                break;
            case Integer:
                if (isFirst) {
                    isFirst = false;
                    firstInt = aReader.nextInt();
                } else
                    fieldRange = new FieldRange(aReader.nextInt(), firstInt);
                break;
            case Double:
                if (isFirst) {
                    isFirst = false;
                    firstDouble = aReader.nextDouble();
                } else
                    fieldRange = new FieldRange(aReader.nextDouble(), firstDouble);
                break;
            case DateTime:
                if (isFirst) {
                    isFirst = false;
                    firstDate = Field.createDate(aReader.nextString());
                } else
                    fieldRange = new FieldRange(Field.createDate(aReader.nextString()), firstDate);
                break;
            default:
                aReader.skipValue();
                break;
            }
        } else if (StringUtils.equals(jsonName, "max")) {
            switch (rangeType) {
            case Long:
                if (isFirst) {
                    isFirst = false;
                    firstLong = aReader.nextLong();
                } else
                    fieldRange = new FieldRange(firstLong, aReader.nextLong());
                break;
            case Integer:
                if (isFirst) {
                    isFirst = false;
                    firstInt = aReader.nextInt();
                } else
                    fieldRange = new FieldRange(firstInt, aReader.nextInt());
                break;
            case Double:
                if (isFirst) {
                    isFirst = false;
                    firstDouble = aReader.nextDouble();
                } else
                    fieldRange = new FieldRange(firstDouble, aReader.nextDouble());
                break;
            case DateTime:
                if (isFirst) {
                    isFirst = false;
                    firstDate = Field.createDate(aReader.nextString());
                } else
                    fieldRange = new FieldRange(firstDate, Field.createDate(aReader.nextString()));
                break;
            default:
                aReader.skipValue();
                break;
            }
        } else
            aReader.skipValue();
    }

    aReader.endObject();

    return fieldRange;
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Double readNullSafeDouble(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from   ww  w.j  a v  a2  s . c o  m
        return null;
    } else {
        return reader.nextDouble();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Float readNullSafeFloat(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from  w  w w.  j  a v  a 2s .  com*/
        return null;
    } else {
        return (float) reader.nextDouble();
    }
}