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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getDoubleValue() . 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: JsonUtil.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Double currentTokenAsDouble(JsonParser parser)
        throws IOException
{
    switch (parser.currentToken()) {
        case VALUE_NULL:
            return null;
        case VALUE_STRING:
        case FIELD_NAME:
            return VarcharOperators.castToDouble(Slices.utf8Slice(parser.getText()));
        case VALUE_NUMBER_FLOAT:
            return parser.getDoubleValue();
        case VALUE_NUMBER_INT:
            // An alternative is calling getLongValue and then BigintOperators.castToDouble.
            // It doesn't work as well because it can result in overflow and underflow exceptions for large integral numbers.
            return parser.getDoubleValue();
        case VALUE_TRUE:
            return BooleanOperators.castToDouble(true);
        case VALUE_FALSE:
            return BooleanOperators.castToDouble(false);
        default:
            throw new JsonCastException(format("Unexpected token when cast to %s: %s", StandardTypes.DOUBLE, parser.getText()));
    }
}
 
Example 2
Source File: PropertyValue.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@Override
public PropertyValue deserialize(JsonParser parser, DeserializationContext ctxt)
        throws IOException {
    JsonToken token = parser.getCurrentToken();
    switch (token) {
        case VALUE_FALSE:
        case VALUE_TRUE:
            return new PropertyValue(parser.getBooleanValue());
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            return new PropertyValue(parser.getDoubleValue());
        case VALUE_STRING:
            return new PropertyValue(parser.getText());
        case START_ARRAY:
            List<String> list = Lists.newArrayList();
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                list.add(parser.getText());
            }
            return new PropertyValue(list);
        default:
            throw new AssertionError("Unexpected json type: " + token);
    }
}
 
Example 3
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 4
Source File: DoubleBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 parsing
 */
public static LinkedList<Double> parseValue2(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    LinkedList<Double> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      LinkedList<Double> collection=new LinkedList<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 5
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public Double read(JsonParser parser)
        throws IOException, JsonReadException
{
    double v = parser.getDoubleValue();
    parser.nextToken();
    return v;
}
 
Example 6
Source File: DoubleDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param parser1 parsing
 */
private double[] parser1(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    double[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Double> collection=new ArrayList<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asDoubleTypeArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 7
Source File: JsonUtilsTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Map<Id, Delta> decode(byte[] json) throws IOException {
  Map<Id, Delta> values = new HashMap<>();
  JsonParser parser = FACTORY.createParser(json);

  // Array start
  Assertions.assertEquals(JsonToken.START_ARRAY, parser.nextToken());

  // String table
  String[] strings = new String[parser.nextIntValue(-1)];
  for (int i = 0; i < strings.length; ++i) {
    strings[i] = parser.nextTextValue();
  }

  // Read measurements
  parser.nextToken();
  while (parser.currentToken() != JsonToken.END_ARRAY) {
    int n = parser.getIntValue();
    Map<String, String> tags = new HashMap<>(n);
    for (int i = 0; i < n; ++i) {
      String k = strings[parser.nextIntValue(-1)];
      String v = strings[parser.nextIntValue(-1)];
      tags.put(k, v);
    }
    String name = tags.get("name");
    tags.remove("name");
    Id id = registry.createId(name).withTags(tags);
    int op = parser.nextIntValue(-1);
    parser.nextToken();
    double value = parser.getDoubleValue();
    values.put(id, new Delta(op, value));

    parser.nextToken();
  }

  return values;
}
 
Example 8
Source File: DoubleBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 parsing
 */
public static Double[] parseValue2(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    Double[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Double> collection=new ArrayList<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asDoubleArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 9
Source File: DoubleDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param parser1 parsing
 */
private double[] parser1(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    double[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Double> collection=new ArrayList<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asDoubleTypeArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 10
Source File: Bean01BindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Bean01 parseOnJackson(JsonParser jacksonParser) throws Exception {
  Bean01 instance = new Bean01();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "id":
          // field id (mapped with "id")
          instance.setId(jacksonParser.getLongValue());
        break;
        case "text":
          // field text (mapped with "text")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.setText(jacksonParser.getText());
          }
        break;
        case "value":
          // field value (mapped with "value")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.value=jacksonParser.getDoubleValue();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 11
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueDoubleSet parsing
 */
public static HashSet<Double> parseValueDoubleSet(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    HashSet<Double> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<Double> collection=new HashSet<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 12
Source File: Bean2Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueDoubleSet parsing
 */
public static HashSet<Double> parseValueDoubleSet(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    HashSet<Double> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<Double> collection=new HashSet<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 13
Source File: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueDoubleSet parsing
 */
protected HashSet<Double> parseValueDoubleSet(String input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    HashSet<Double> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<Double> collection=new HashSet<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 14
Source File: JsonTerminologyIO.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
private static <T extends Enum<T> & Property<?>> Object readPropertyValue(JsonParser jp, 
		T property) throws IOException {
	if(property.getRange().equals(Double.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_FLOAT);
		return jp.getDoubleValue();
	} else if(property.getRange().equals(Float.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_FLOAT);
		return (float)jp.getDoubleValue();
	} else if(property.getRange().equals(Integer.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_INT);
		return jp.getIntValue();
	} else if(property.getRange().equals(Long.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_INT);
		return jp.getLongValue();
	} else if(property.getRange().equals(Boolean.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_FALSE, JsonToken.VALUE_TRUE);
		return jp.getBooleanValue();
	} else if(property.getRange().equals(String.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_STRING);
		return jp.getValueAsString();
	} else if(Set.class.isAssignableFrom(property.getRange())) {
		checkToken(property, jp.currentToken(), JsonToken.START_ARRAY);
		HashSet<Object> values = new HashSet<>();
		readCollection(jp, values);
		return values;

	} else if(property.getRange().isEnum()) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_STRING);
		PropertyValue theValue;
		String jsonString = jp.getValueAsString();
		theValue = loadEnumConstant(property, jsonString);
		return (Comparable<?>) theValue;
	} else {
		throw new UnsupportedOperationException(String.format(
				"Unsupported property range <%s> in property %s",property.getRange(), property));
	}
}
 
Example 15
Source File: DoubleNodeCalc.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
static NodeCalc parseJson(JsonParser parser) throws IOException {
    JsonToken token;
    while ((token = parser.nextToken()) != null) {
        if (token == JsonToken.VALUE_NUMBER_FLOAT) {
            return new DoubleNodeCalc(parser.getDoubleValue());
        } else if (token == JsonToken.VALUE_NUMBER_INT) {
            return new DoubleNodeCalc(parser.getIntValue());
        } else {
            throw NodeCalc.createUnexpectedToken(token);
        }
    }
    throw new TimeSeriesException("Invalid double node calc JSON");
}
 
Example 16
Source File: DoubleBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value parsing
 */
public static List<Double> parseValue(byte[] input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    List<Double> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Double> collection=new ArrayList<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 17
Source File: AvroObjectDeserializer.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
Object deserialize(JsonParser p) throws IOException {
  while (p.currentToken() != null) {
    switch (p.currentToken()) {
      case VALUE_STRING:
        return p.getText();
      case VALUE_NUMBER_FLOAT:
        return p.getDoubleValue();
      case VALUE_NUMBER_INT:
        return p.getLongValue();
      case VALUE_FALSE:
      case VALUE_TRUE:
        return p.getBooleanValue();
      case VALUE_NULL:
        return null;
      case START_OBJECT:
        return deserializeObject(p);
      case START_ARRAY:
        return deserializeArray(p);
      case FIELD_NAME:
      case END_OBJECT:
      case END_ARRAY:
        //ignore
        break;
      default:
        throw new IllegalStateException("Unexpected token: " + p.currentToken());
    }
    p.nextToken();
  }
  throw new IllegalStateException("Unexpectedly finished processing stream");
}
 
Example 18
Source File: StoneSerializers.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public Double deserialize(JsonParser p) throws IOException, JsonParseException {
    Double value = p.getDoubleValue();
    p.nextToken();
    return value;
}
 
Example 19
Source File: PrimitiveKVHandler.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
public double value(DeserializationContext ctx, JsonParser parser) throws IOException {
    return parser.getDoubleValue();
}
 
Example 20
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private Object handlePrimitive(JsonParser parser, FieldDescriptor field) throws IOException {
    Object value = null;

    JsonToken token = parser.getCurrentToken();

    if (token.equals(JsonToken.VALUE_NULL)) {
        return value;
    }

    switch (field.getType()) {
        case INT32:
        case SINT32:
        case SFIXED32:
        	value = parser.getIntValue();
            break;

        case INT64:
        case SINT64:
        case SFIXED64:
        	value = parser.getLongValue();
            break;

        case UINT32:
        case FIXED32:
        	long valueLong = parser.getLongValue();
        	if (valueLong < 0 || valueLong > MAX_UINT_VALUE) {
        		throw new NumberFormatException("Number must be positive: " + valueLong);
        	}
        	value = (int) valueLong;
            break;

        case UINT64:
        case FIXED64:
        	BigInteger valueBigInt = parser.getBigIntegerValue();
            // valueBigInt < 0 || valueBigInt > MAX_ULONG_VALUE
        	if (valueBigInt.compareTo(BigInteger.ZERO) == -1 || valueBigInt.compareTo(MAX_ULONG_VALUE) == 1) {
        		throw new NumberFormatException("Number must be positive: " + valueBigInt);
        	}
        	value = valueBigInt.longValue();
            break;

        case FLOAT:
        	value = parser.getFloatValue();
            break;

        case DOUBLE:
        	value = parser.getDoubleValue();
            break;

        case BOOL:
        	value = parser.getBooleanValue();
            break;

        case STRING:
        	value = parser.getText();
            break;

        case BYTES:
        	value = ByteString.copyFrom(parser.getBinaryValue());
            break;

        case ENUM: {
            EnumDescriptor enumType = field.getEnumType();
            if (token.equals(JsonToken.VALUE_NUMBER_INT)) {
                int number = parser.getIntValue();
                value = enumType.findValueByNumber(number);
                if (value == null) {
                    throw new RuntimeException("Enum type \""
                    		+ enumType.getFullName()
                    		+ "\" has no value with number "
                    		+ number + ".");
                }
            } else {
                String id = parser.getText();
                value = enumType.findValueByName(id);
                if (value == null) {
                	throw new RuntimeException("Enum type \""
                			+ enumType.getFullName()
                			+ "\" has no value named \""
                			+ id + "\".");
                }
            }
            break;
        }

        case MESSAGE:
        case GROUP:
            throw new RuntimeException("Can't get here.");
    }
    return value;
}