Java Code Examples for com.fasterxml.jackson.core.JsonParser#getValueAsDouble()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getValueAsDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LngLatAltDeserializer.java    From geojson-jackson with Apache License 2.0 6 votes vote down vote up
private double extractDouble(JsonParser jp, DeserializationContext ctxt, boolean optional) throws IOException {
    JsonToken token = jp.nextToken();
    if (token == null) {
        if (optional)
            return Double.NaN;
        else
            throw ctxt.mappingException("Unexpected end-of-input when binding data into LngLatAlt");
    } else {
        switch (token) {
            case END_ARRAY:
                if (optional)
                    return Double.NaN;
                else
                    throw ctxt.mappingException("Unexpected end-of-input when binding data into LngLatAlt");
            case VALUE_NUMBER_FLOAT:
                return jp.getDoubleValue();
            case VALUE_NUMBER_INT:
                return jp.getLongValue();
            case VALUE_STRING:
                return jp.getValueAsDouble();
            default:
                throw ctxt.mappingException(
                        "Unexpected token (" + token.name() + ") when binding data into LngLatAlt");
        }
    }
}
 
Example 2
Source File: JsonSampler.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Get Node type from the parser
 *
 * @param jsonParser
 * @return Node - return Node type based on json token
 */
private Node getValue( JsonParser jsonParser, String key ) {
  try {
    switch ( jsonParser.currentToken() ) {
      case START_OBJECT:
        return new ObjectNode( key );
      case START_ARRAY:
        return new ArrayNode( key );
      case VALUE_STRING:
        return new ValueNode<>( key, jsonParser.getValueAsString() );
      case VALUE_TRUE:
      case VALUE_FALSE:
        return new ValueNode<>( key, jsonParser.getValueAsBoolean() );
      case VALUE_NULL:
        return new ValueNode<>( key, null );
      case VALUE_NUMBER_FLOAT:
        return new ValueNode<>( key, jsonParser.getValueAsDouble() );
      case VALUE_NUMBER_INT:
        return new ValueNode<>( key, jsonParser.getValueAsInt() );
    }
  } catch ( IOException ioe ) {
    return null;
  }
  return null;
}
 
Example 3
Source File: AbstractMinMaxNodeCalc.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
static void parseFieldName(JsonParser parser, JsonToken token, ParsingContext context) throws IOException {
    String fieldName = parser.getCurrentName();
    if ("value".equals(fieldName)) {
        parser.nextValue();
        context.value = parser.getValueAsDouble();
    } else {
        if (context.child == null) {
            context.child = NodeCalc.parseJson(parser, token);
        } else {
            throw new TimeSeriesException("Only 1 operand expected for a min/max");
        }
    }
}
 
Example 4
Source File: JsonTerminologyIO.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
private static Object readTokenObject(JsonParser jp, JsonToken token) throws IOException {
	switch (token) {
	case VALUE_NUMBER_INT: return jp.getValueAsInt();
	case VALUE_NUMBER_FLOAT: return jp.getValueAsDouble();
	case VALUE_FALSE: return false;
	case VALUE_TRUE: return true;
	case VALUE_NULL: return null;
	case VALUE_STRING: return jp.getValueAsString();
	default:
		throw new IllegalStateException("Token type not allowed in value collections: " + token);
	}
}
 
Example 5
Source File: ParseDouble.java    From robe with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Double parse(JsonParser parser, Field field) throws IOException {
    return isValid(parser) ? new Double(parser.getValueAsDouble()) : null;
}
 
Example 6
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
@Override
    public Object read(JSONReader reader, JsonParser p) throws IOException
    {
        switch (_typeId) {

        case SER_INT_ARRAY:
            return _readIntArray(p);

        case SER_TREE_NODE:
            return reader.readTree();

        // Textual types, related:
        case SER_STRING:
        case SER_CHARACTER_SEQUENCE:
            return p.getValueAsString();
        case SER_CHAR_ARRAY:
            return p.getValueAsString().toCharArray();
        case SER_BYTE_ARRAY:
            return _readBinary(p);

        // Number types:
            
        case SER_NUMBER_FLOAT: // fall through
            return Float.valueOf((float) p.getValueAsDouble());
        case SER_NUMBER_DOUBLE:
            return p.getValueAsDouble();

        case SER_NUMBER_BYTE: // fall through
            return (byte) p.getValueAsInt();
            
        case SER_NUMBER_SHORT: // fall through
            return (short) p.getValueAsInt();
        case SER_NUMBER_INTEGER:
            return p.getValueAsInt();
        case SER_NUMBER_LONG:
            return p.getValueAsLong();

        case SER_NUMBER_BIG_DECIMAL:
            return p.getDecimalValue();

        case SER_NUMBER_BIG_INTEGER:
            return p.getBigIntegerValue();

        // Other scalar types:

        case SER_BOOLEAN:
            return p.getValueAsBoolean();
            
        case SER_CHAR:
            {
                String str = p.getValueAsString();
                return (str == null || str.isEmpty()) ? ' ' : str.charAt(0);
            }
            
        case SER_CALENDAR:
            {
                long l = _fetchLong(p);
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(l);
                return cal;
            }

        case SER_DATE:
            return new Date(_fetchLong(p));

        case SER_CLASS:
        {
            String v = p.getValueAsString();
            try {
                return Class.forName(v);
            } catch (Exception e) {
                throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'");
            }
        }
        case SER_FILE:
            return new File(p.getValueAsString());
        case SER_UUID:
            return UUID.fromString(p.getValueAsString());
        case SER_URL:
            return new URL(p.getValueAsString());
        case SER_URI:
        
            return URI.create(p.getValueAsString());

//        case SER_MAP:
//        case SER_LIST:
//        case SER_COLLECTION:
//        case SER_OBJECT_ARRAY:
            // should never get here: we have dedicated readers
        default: // types that shouldn't get here
        //case SER_ENUM:
        }
        
        throw JSONObjectException.from(p,
                "Can not create a "+_valueType.getName()+" instance out of "+_tokenDesc(p));
    }
 
Example 7
Source File: JsonValue.java    From RedReader with GNU General Public License v3.0 4 votes vote down vote up
protected JsonValue(final JsonParser jp, final JsonToken firstToken) throws IOException {

		switch(firstToken) {

			case START_OBJECT:
				type = TYPE_OBJECT;
				value = new JsonBufferedObject();
				this.jp = jp;
				break;

			case START_ARRAY:
				type = TYPE_ARRAY;
				value = new JsonBufferedArray();
				this.jp = jp;
				break;

			case VALUE_FALSE:
				type = TYPE_BOOLEAN;
				value = false;
				break;

			case VALUE_TRUE:
				type = TYPE_BOOLEAN;
				value = true;
				break;

			case VALUE_NULL:
				type = TYPE_NULL;
				value = null;
				break;

			case VALUE_STRING:
				type = TYPE_STRING;
				value = jp.getValueAsString();
				break;

			case VALUE_NUMBER_FLOAT:

				//noinspection FloatingPointEquality,UnnecessaryExplicitNumericCast
				if(jp.getValueAsDouble() == (double)jp.getValueAsLong()) {
					type = TYPE_INTEGER;
					value = jp.getValueAsLong();
				} else {
					type = TYPE_FLOAT;
					value = jp.getValueAsDouble();
				}

				break;

			case VALUE_NUMBER_INT:
				type = TYPE_INTEGER;
				value = jp.getValueAsLong();
				break;

			default:
				throw new JsonParseException(jp, "Expecting an object, literal, or array", jp.getCurrentLocation());
		}
	}