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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getFloatValue() . 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: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueFloatSet parsing
 */
protected HashSet<Float> parseValueFloatSet(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<Float> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<Float> collection=new HashSet<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 2
Source File: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 parsing
 */
public static Float[] 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();
    Float[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asFloatArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 3
Source File: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value parsing
 */
public static float[] 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();
    float[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asFloatTypeArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 4
Source File: Point3BindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Point3 parseOnJackson(JsonParser jacksonParser) throws Exception {
  Point3 instance = new Point3();
  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 "x":
          // field x (mapped with "x")
          instance.x=jacksonParser.getFloatValue();
        break;
        case "y":
          // field y (mapped with "y")
          instance.y=jacksonParser.getFloatValue();
        break;
        case "z":
          // field z (mapped with "z")
          instance.z=jacksonParser.getFloatValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 5
Source File: FloatDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param parser2 parsing
 */
private Float[] parser2(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();
    Float[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asFloatArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 6
Source File: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 parsing
 */
public static Float[] 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();
    Float[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asFloatArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 7
Source File: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value parsing
 */
public static float[] 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();
    float[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asFloatTypeArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 8
Source File: FloatDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param parser2 parsing
 */
private Float[] parser2(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();
    Float[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asFloatArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 9
Source File: BindBeanSharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueFloatSet parsing
 */
protected HashSet<Float> parseValueFloatSet(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<Float> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<Float> collection=new HashSet<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 10
Source File: Bean2Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueFloatSet parsing
 */
public static HashSet<Float> parseValueFloatSet(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<Float> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<Float> collection=new HashSet<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 11
Source File: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value parsing
 */
public static List<Float> 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<Float> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Float> collection=new ArrayList<>();
      Float item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getFloatValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 12
Source File: Vector3BindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Vector3 parseOnJackson(JsonParser jacksonParser) throws Exception {
  Vector3 instance = new Vector3();
  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 "x":
          // field x (mapped with "x")
          instance.x=jacksonParser.getFloatValue();
        break;
        case "y":
          // field y (mapped with "y")
          instance.y=jacksonParser.getFloatValue();
        break;
        case "z":
          // field z (mapped with "z")
          instance.z=jacksonParser.getFloatValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 13
Source File: AbstractEmoFieldUDF.java    From emodb with Apache License 2.0 5 votes vote down vote up
protected FloatWritable narrowToFloat(JsonParser parser)
        throws IOException {
    switch (parser.getCurrentToken()) {
        case VALUE_NUMBER_INT:
            return new FloatWritable(parser.getIntValue());
        case VALUE_NUMBER_FLOAT:
            return new FloatWritable(parser.getFloatValue());
        default:
            return null;
    }
}
 
Example 14
Source File: AbstractEmoFieldUDF.java    From emodb with Apache License 2.0 5 votes vote down vote up
protected IntWritable narrowToInt(JsonParser parser)
        throws IOException {
    switch (parser.getCurrentToken()) {
        case VALUE_NUMBER_INT:
            return new IntWritable(parser.getIntValue());
        case VALUE_NUMBER_FLOAT:
            return new IntWritable((int) parser.getFloatValue());
        default:
            return null;
    }
}
 
Example 15
Source File: AbstractEmoFieldUDF.java    From emodb with Apache License 2.0 5 votes vote down vote up
protected BooleanWritable narrowToBoolean(JsonParser parser)
        throws IOException {
    switch (parser.getCurrentToken()) {
        case VALUE_TRUE:
            return new BooleanWritable(true);
        case VALUE_FALSE:
            return new BooleanWritable(false);
        case VALUE_NUMBER_INT:
            return new BooleanWritable(parser.getIntValue() != 0);
        case VALUE_NUMBER_FLOAT:
            return new BooleanWritable(parser.getFloatValue() != 0);
        default:
            return null;
    }
}
 
Example 16
Source File: FloatNodeCalc.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 FloatNodeCalc(parser.getFloatValue());
        } else if (token == JsonToken.VALUE_NUMBER_INT) {
            return new FloatNodeCalc(parser.getIntValue());
        } else {
            throw NodeCalc.createUnexpectedToken(token);
        }
    }
    throw new TimeSeriesException("Invalid float node calc JSON");
}
 
Example 17
Source File: StoneSerializers.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public Float deserialize(JsonParser p) throws IOException, JsonParseException {
    Float value = p.getFloatValue();
    p.nextToken();
    return value;
}
 
Example 18
Source File: PrimitiveKVHandler.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
public float value(DeserializationContext ctx, JsonParser parser) throws IOException {
    return parser.getFloatValue();
}
 
Example 19
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;
}
 
Example 20
Source File: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
private Object readObject(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {

        MarshallingType<?> type = field.marshallingType();
        switch (p.currentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE: {
                if (type.equals(MarshallingType.BOOLEAN)) {
                    return p.getBooleanValue();
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got boolean field value");
            }

            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT: {
                if (type.equals(MarshallingType.INTEGER)) {
                    return p.getIntValue();
                } else if (type.equals(MarshallingType.LONG)) {
                    return p.getLongValue();
                } else if (type.equals(MarshallingType.FLOAT)) {
                    return p.getFloatValue(); // coerce should work
                } else if (type.equals(MarshallingType.DOUBLE)) {
                    return p.getDoubleValue(); // coerce should work
                } else if (type.equals(MarshallingType.BIG_DECIMAL)) {
                    return p.getDecimalValue(); // coerce should work
                } else if (type.equals(MarshallingType.INSTANT)) { // we serialize as BigDecimals
                    JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(Instant.class));
                    return deser.deserialize(p, ctxt);
                }
                throw new JsonMappingException(p,
                                               "Type mismatch, expecting " + type + " got int/float/double/big_decimal/instant");
            }

            case VALUE_STRING: {
                if (type.equals(MarshallingType.STRING)) {
                    return p.getText();
                } else if (type.equals(MarshallingType.SDK_BYTES)) {
                    byte[] bytes = p.getBinaryValue();
                    return SdkBytes.fromByteArray(bytes);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got string/bytes");
            }

            case START_OBJECT: {
                if (type.equals(MarshallingType.MAP)) {
                    return readMap(field, p, ctxt);
                } else if (type.equals(MarshallingType.SDK_POJO)) {
                    return readPojo(field.constructor().get(), p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo");
            }

            case START_ARRAY: {
                if (type.equals(MarshallingType.LIST)) {
                    return readList(field, p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got List type");
            }

            case VALUE_NULL:
                return null;

            default:
                throw new JsonMappingException(p, "Can not map type " + type + " Token = " + p.currentToken());
        }
    }