Java Code Examples for com.fasterxml.jackson.databind.JsonNode#isBigInteger()

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#isBigInteger() . 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: JsonUtils.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
public static String toString(JsonNode node) {
    if (node == null || node.isNull()) {
        return null;
    }
    if (node.isValueNode()) {
        if (node.isBoolean()) {
            return String.valueOf(node.asBoolean());
        } else if (node.isBigInteger()) {
            return String.valueOf(node.bigIntegerValue());
        } else if (node.isDouble()) {
            return String.valueOf(node.asDouble());
        } else if (node.isInt()) {
            return String.valueOf(node.intValue());
        } else if (node.isLong()) {
            return String.valueOf(node.asLong());
        } else if (node.isShort()) {
            return String.valueOf(node.shortValue());
        } else {
            return node.asText();
        }
    }
    return node.toString();
}
 
Example 2
Source File: JsonUtils.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
public static Object parserValue(JsonNode node) {
    if (node == null || node.isNull()) {
        return null;
    }
    if (node.isArray()) {
        return parserArrayNode((ArrayNode) node);
    } else if (node.isObject()) {
        return parserMap((ObjectNode) node);
    } else {
        if (node.isBigDecimal() || node.isBigInteger() || node.isLong()) {
            return node.asLong();
        } else if (node.isFloat() || node.isDouble()) {
            return node.asDouble();
        } else if (node.isInt() || node.isNumber() || node.isShort()) {
            return node.asInt();
        } else if (node.isBoolean()) {
            return node.asBoolean();
        } else if (node.isTextual()) {
            return node.asText();
        } else {// 其他类型
            return node.textValue();
        }
    }
}
 
Example 3
Source File: GenericJsonRecord.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public Object getField(String fieldName) {
    JsonNode fn = jn.get(fieldName);
    if (fn.isContainerNode()) {
        AtomicInteger idx = new AtomicInteger(0);
        List<Field> fields = Lists.newArrayList(fn.fieldNames())
            .stream()
            .map(f -> new Field(f, idx.getAndIncrement()))
            .collect(Collectors.toList());
        return new GenericJsonRecord(schemaVersion, fields, fn);
    } else if (fn.isBoolean()) {
        return fn.asBoolean();
    } else if (fn.isFloatingPointNumber()) {
        return fn.asDouble();
    } else if (fn.isBigInteger()) {
        if (fn.canConvertToLong()) {
            return fn.asLong();
        } else {
            return fn.asText();
        }
    } else if (fn.isNumber()) {
        return fn.numberValue();
    } else {
        return fn.asText();
    }
}
 
Example 4
Source File: MessageFormats.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
@Override
public String asString(Object arg) {
  if (arg instanceof JsonNode) {
    JsonNode node = (JsonNode) arg;
    JsonNode decimal = currency(node);
    if (!decimal.isMissingNode()) {
      return decimal.asText();
    }
    switch (node.getNodeType()) {
      case BOOLEAN:
        return node.asBoolean() ? "true" : "false";
      case NULL:
      case MISSING:
        return "";
      case NUMBER:
        if (node.isBigInteger() || node.isBigDecimal()) {
          return node.asText();
        }
        return node.isIntegralNumber() ? Long.toString(node.longValue()) : Double.toString(node.doubleValue());
      case ARRAY:
      case OBJECT:
      case STRING:
      default:
        return node.asText();
    }
  }
  try {
    return super.asString(arg);
  } catch (Exception e) {
    return "";
  }
}
 
Example 5
Source File: JSONConfigParser.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Map<String, Object> getParams(JsonNode next) {
	if (next.has("params")) {
		Iterator<Entry<String, JsonNode>> it2 = next.get("params").fields();
		Map<String, Object> params = new HashMap<String, Object>();
		while (it2.hasNext()) {
			Entry<String, JsonNode> param = it2.next();
			JsonNode value = param.getValue();
			if (value.isTextual()) {
				params.put(param.getKey(), value.asText());
			} else if (value.isInt()) {
				params.put(param.getKey(), value.asInt());
			} else if (value.isBoolean()) {
				params.put(param.getKey(), value.asBoolean());
			} else if (value.isDouble() || value.isFloat() || value.isBigDecimal()) {
				params.put(param.getKey(), value.asDouble());
			} else if (value.isLong() || value.isBigInteger()) {
				params.put(param.getKey(), value.asLong());
			} else {
				params.put(param.getKey(), value);
			}
			params.put(param.getKey(), param.getValue().asText());
		}
		return params;

	}
	return null;
}
 
Example 6
Source File: CassandraPersistenceUtils.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public static Object toStorableValue( Object obj ) {
    if ( obj == null ) {
        return null;
    }

    if ( isBasicType( obj.getClass() ) ) {
        return obj;
    }

    if ( obj instanceof ByteBuffer ) {
        return obj;
    }

    JsonNode json = toJsonNode( obj );
    if ( ( json != null ) && json.isValueNode() ) {
        if ( json.isBigInteger() ) {
            return json.asInt();
        }
        else if ( json.isNumber() || json.isBoolean() ) {
            return BigInteger.valueOf( json.asLong() );
        }
        else if ( json.isTextual() ) {
            return json.asText();
        }
        else if ( json.isBinary() ) {
            try {
                return wrap( json.binaryValue() );
            }
            catch ( IOException e ) {
            }
        }
    }

    return json;
}
 
Example 7
Source File: JSONConfigParser.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Map<String, Object> getParams(JsonNode next) {
	if (next.has("params")) {
		Iterator<Entry<String, JsonNode>> it2 = next.get("params").fields();
		Map<String, Object> params = new HashMap<String, Object>();
		while (it2.hasNext()) {
			Entry<String, JsonNode> param = it2.next();
			JsonNode value = param.getValue();
			if (value.isTextual()) {
				params.put(param.getKey(), value.asText());
			} else if (value.isInt()) {
				params.put(param.getKey(), value.asInt());
			} else if (value.isBoolean()) {
				params.put(param.getKey(), value.asBoolean());
			} else if (value.isDouble() || value.isFloat() || value.isBigDecimal()) {
				params.put(param.getKey(), value.asDouble());
			} else if (value.isLong() || value.isBigInteger()) {
				params.put(param.getKey(), value.asLong());
			} else {
				params.put(param.getKey(), value);
			}
			params.put(param.getKey(), param.getValue().asText());
		}
		return params;

	}
	return null;
}
 
Example 8
Source File: ExtractJsonPathsBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
private void resolve(JsonNode datum, Record record, String fieldName) { 
  if (datum == null) {
    return;
  }
  
  if (flatten) {
    flatten(datum, record.get(fieldName));
    return;
  }

  if (datum.isObject()) {
    record.put(fieldName, datum);
  } else if (datum.isArray()) {
    record.put(fieldName, datum);  
  } else if (datum.isTextual()) {
    record.put(fieldName, datum.asText());
  } else if (datum.isBoolean()) {
    record.put(fieldName, datum.asBoolean());
  } else if (datum.isInt()) {
    record.put(fieldName, datum.asInt());
  } else if (datum.isLong()) {
    record.put(fieldName, datum.asLong());
  } else if (datum.isShort()) {
    record.put(fieldName, datum.shortValue());
  } else if (datum.isDouble()) {
    record.put(fieldName, datum.asDouble());
  } else if (datum.isFloat()) {
    record.put(fieldName, datum.floatValue());
  } else if (datum.isBigInteger()) {
    record.put(fieldName, datum.bigIntegerValue());
  } else if (datum.isBigDecimal()) {
    record.put(fieldName, datum.decimalValue());
  } else if (datum.isNull()) {
    ; // ignore
  } else {
    record.put(fieldName, datum.toString());
  }
}
 
Example 9
Source File: ExtractJsonPathsBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void flatten(JsonNode datum, List list) { 
  if (datum == null) {
    return;
  }

  if (datum.isObject()) {
    for (JsonNode child : datum) {
      flatten(child, list);
    }
  } else if (datum.isArray()) {
    Iterator<JsonNode> iter = datum.elements();
    while (iter.hasNext()) {
      flatten(iter.next(), list);
    }        
  } else if (datum.isTextual()) {
    list.add(datum.asText());
  } else if (datum.isBoolean()) {
    list.add(datum.asBoolean());
  } else if (datum.isInt()) {
    list.add(datum.asInt());
  } else if (datum.isLong()) {
    list.add(datum.asLong());
  } else if (datum.isShort()) {
    list.add(datum.shortValue());
  } else if (datum.isDouble()) {
    list.add(datum.asDouble());
  } else if (datum.isFloat()) {
    list.add(datum.floatValue());
  } else if (datum.isBigInteger()) {
    list.add(datum.bigIntegerValue());
  } else if (datum.isBigDecimal()) {
    list.add(datum.decimalValue());
  } else if (datum.isNull()) {
    ; // ignore
  } else {
    list.add(datum.toString());
  }
}
 
Example 10
Source File: JsonFileReader.java    From kafka-connect-fs with Apache License 2.0 4 votes vote down vote up
private static Schema extractSchema(JsonNode jsonNode) {
    switch (jsonNode.getNodeType()) {
        case BOOLEAN:
            return Schema.OPTIONAL_BOOLEAN_SCHEMA;
        case NUMBER:
            if (jsonNode.isShort()) {
                return Schema.OPTIONAL_INT8_SCHEMA;
            } else if (jsonNode.isInt()) {
                return Schema.OPTIONAL_INT32_SCHEMA;
            } else if (jsonNode.isLong()) {
                return Schema.OPTIONAL_INT64_SCHEMA;
            } else if (jsonNode.isFloat()) {
                return Schema.OPTIONAL_FLOAT32_SCHEMA;
            } else if (jsonNode.isDouble()) {
                return Schema.OPTIONAL_FLOAT64_SCHEMA;
            } else if (jsonNode.isBigInteger()) {
                return Schema.OPTIONAL_INT64_SCHEMA;
            } else if (jsonNode.isBigDecimal()) {
                return Schema.OPTIONAL_FLOAT64_SCHEMA;
            } else {
                return Schema.OPTIONAL_FLOAT64_SCHEMA;
            }
        case STRING:
            return Schema.OPTIONAL_STRING_SCHEMA;
        case BINARY:
            return Schema.OPTIONAL_BYTES_SCHEMA;
        case ARRAY:
            Iterable<JsonNode> elements = jsonNode::elements;
            Schema arraySchema = StreamSupport.stream(elements.spliterator(), false)
                    .findFirst().map(JsonFileReader::extractSchema)
                    .orElse(SchemaBuilder.struct().build());
            return SchemaBuilder.array(arraySchema).build();
        case OBJECT:
            SchemaBuilder builder = SchemaBuilder.struct();
            jsonNode.fields()
                    .forEachRemaining(field -> builder.field(field.getKey(), extractSchema(field.getValue())));
            return builder.build();
        default:
            return SchemaBuilder.struct().optional().build();
    }
}
 
Example 11
Source File: JsonFileReader.java    From kafka-connect-fs with Apache License 2.0 4 votes vote down vote up
private Object mapValue(Schema schema, JsonNode value) {
    if (value == null) return null;

    switch (value.getNodeType()) {
        case BOOLEAN:
            return value.booleanValue();
        case NUMBER:
            if (value.isShort()) {
                return value.shortValue();
            } else if (value.isInt()) {
                return value.intValue();
            } else if (value.isLong()) {
                return value.longValue();
            } else if (value.isFloat()) {
                return value.floatValue();
            } else if (value.isDouble()) {
                return value.doubleValue();
            } else if (value.isBigInteger()) {
                return value.bigIntegerValue();
            } else {
                return value.numberValue();
            }
        case STRING:
            return value.asText();
        case BINARY:
            try {
                return value.binaryValue();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        case OBJECT:
        case POJO:
            Struct struct = new Struct(schema);
            Iterable<Map.Entry<String, JsonNode>> fields = value::fields;
            StreamSupport.stream(fields.spliterator(), false)
                    .forEach(field -> struct.put(field.getKey(),
                            mapValue(extractSchema(field.getValue()), field.getValue()))
                    );
            return struct;
        case ARRAY:
            Iterable<JsonNode> arrayElements = value::elements;
            return StreamSupport.stream(arrayElements.spliterator(), false)
                    .map(elm -> mapValue(schema, elm))
                    .collect(Collectors.toList());
        case NULL:
        case MISSING:
        default:
            return null;
    }
}
 
Example 12
Source File: JsonQLDataSource.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected Object getConvertedValue(JRJsonNode node, JRField jrField) throws JRException {
    JsonNode dataNode = node.getDataNode();
    Class<?> valueClass = jrField.getValueClass();

    if (log.isDebugEnabled()) {
        log.debug("attempting to convert: " + dataNode + " to class: " + valueClass);
    }

    if (Object.class.equals(valueClass)) {
        return dataNode;
    }

    Object result = null;

    if (!dataNode.isNull())  {
        try {
            if (Boolean.class.equals(valueClass) && dataNode.isBoolean()) {
                result = dataNode.booleanValue();

            } else if (BigDecimal.class.equals(valueClass) && dataNode.isBigDecimal()) {
                result = dataNode.decimalValue();

            } else if (BigInteger.class.equals(valueClass) && dataNode.isBigInteger()) {
                result = dataNode.bigIntegerValue();

            } else if (Double.class.equals(valueClass) && dataNode.isDouble()) {
                result = dataNode.doubleValue();

            } else if (Integer.class.equals(valueClass) && dataNode.isInt()) {
                result = dataNode.intValue();

            } else if (Number.class.isAssignableFrom(valueClass) && dataNode.isNumber()) {
                result = convertNumber(dataNode.numberValue(), valueClass);

            } else {
                result = convertStringValue(dataNode.asText(), valueClass);
            }

            if (result == null) {
                throw new JRException(EXCEPTION_MESSAGE_KEY_CANNOT_CONVERT_FIELD_TYPE,
                        new Object[]{jrField.getName(), valueClass.getName()});
            }

        } catch (Exception e) {
            throw new JRException(EXCEPTION_MESSAGE_KEY_JSON_FIELD_VALUE_NOT_RETRIEVED,
                    new Object[]{jrField.getName(), valueClass.getName()}, e);
        }
    }

    return result;
}
 
Example 13
Source File: QueueIndexUpdate.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * @param obj
 * @return
 */
public static Object toIndexableValue( Object obj ) {
    if ( obj == null ) {
        return null;
    }

    if ( obj instanceof String ) {
        return prepStringForIndex( ( String ) obj );
    }

    // UUIDs, and BigIntegers map to Cassandra UTF8Type and IntegerType
    if ( ( obj instanceof UUID ) || ( obj instanceof BigInteger ) ) {
        return obj;
    }

    // For any numeric values, turn them into a long
    // and make them BigIntegers for IntegerType
    if ( obj instanceof Number ) {
        return BigInteger.valueOf( ( ( Number ) obj ).longValue() );
    }

    if ( obj instanceof Boolean ) {
        return BigInteger.valueOf( ( ( Boolean ) obj ) ? 1L : 0L );
    }

    if ( obj instanceof Date ) {
        return BigInteger.valueOf( ( ( Date ) obj ).getTime() );
    }

    if ( obj instanceof byte[] ) {
        return wrap( ( byte[] ) obj );
    }

    if ( obj instanceof ByteBuffer ) {
        return obj;
    }

    JsonNode json = toJsonNode( obj );
    if ( ( json != null ) && json.isValueNode() ) {
        if ( json.isBigInteger() ) {
            return json.asInt();
            //return json.getBigIntegerValue();
        }
        else if ( json.isNumber() || json.isBoolean() ) {
            return BigInteger.valueOf( json.asLong() );
        }
        else if ( json.isTextual() ) {
            return prepStringForIndex( json.asText() );
        }
        else if ( json.isBinary() ) {
            try {
                return wrap( json.binaryValue());
            }
            catch ( IOException e ) {
            }
        }
    }

    return null;
}