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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getValueAsLong() . 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: JsonDatabaseReader.java    From dbsync with Apache License 2.0 6 votes vote down vote up
Map<String, Object> readRecord(JsonParser jp) throws IOException {

        Map<String, Object> record = new HashMap<>();
        String fielName =  "";
        Object value;
        JsonToken token;

        while((token = jp.nextToken()) != JsonToken.END_OBJECT) {
            if (token == JsonToken.FIELD_NAME) {
                fielName = jp.getCurrentName();
            } else if (token == JsonToken.VALUE_STRING) {
                value = jp.getValueAsString();
                record.put(fielName, value);
            } else if (token == JsonToken.VALUE_NUMBER_INT) {
                value = jp.getValueAsLong();
                record.put(fielName, value);
            }
        }

        //System.out.println(record.toString());
        return record;
    }
 
Example 2
Source File: JSonLongConverter.java    From dbsync with Apache License 2.0 5 votes vote down vote up
@Override
public Value jsonToColumnValue(final JsonParser parser, final ValueMetadata metadata) throws IOException {

    JsonToken token;
    Value value;

    // Go to the next token
    token = parser.nextToken();

    if (token != JsonToken.VALUE_NUMBER_INT && token != JsonToken.VALUE_NULL) {
        throw new IOException("Unable to parse field " + metadata.getName() + " expected int or null at line " + parser.getCurrentLocation().getLineNr());
    }

    // Can be a integer or null
    if (token == JsonToken.VALUE_NUMBER_INT) {
        value = new Value(parser.getValueAsLong(), metadata);
    } else {
        // null
        if (metadata.isNotNull()) {
            throw new IOException("Unable to parse field " + metadata.getName() + " expected int bu found null at line " + parser.getCurrentLocation().getLineNr());
        }

        value = new Value(metadata);
    }

    return value;
}
 
Example 3
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
private final long _nextLong(JsonParser p) throws IOException {
    long l = p.nextLongValue(-2L);
    if (l != -2L) {
        return l;
    }
    return p.getValueAsLong();
}
 
Example 4
Source File: MillisecTimestampDeserializer.java    From zheshiyigeniubidexiangmu with MIT License 4 votes vote down vote up
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
  return new Date(jp.getValueAsLong());
}
 
Example 5
Source File: UnixTimestampDeserializer.java    From zheshiyigeniubidexiangmu with MIT License 4 votes vote down vote up
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
  return new Date(jp.getValueAsLong() * 1000L);
}
 
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());
		}
	}