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

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#isBigDecimal() . 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 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 2
Source File: JsonNodeELResolver.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * If the base object is a map, returns the value associated with the given key, as specified by
 * the property argument. If the key was not found, null is returned. If the base is a Map, the
 * propertyResolved property of the ELContext object must be set to true by this resolver,
 * before returning. If this property is not true after this method is called, the caller should
 * ignore the return value. Just as in java.util.Map.get(Object), just because null is returned
 * doesn't mean there is no mapping for the key; it's also possible that the Map explicitly maps
 * the key to null.
 * 
 * @param context
 *            The context of this evaluation.
 * @param base
 *            The map to analyze. Only bases of type Map are handled by this resolver.
 * @param property
 *            The key to return the acceptable type for. Ignored by this resolver.
 * @return If the propertyResolved property of ELContext was set to true, then the value
 *         associated with the given key or null if the key was not found. Otherwise, undefined.
 * @throws ClassCastException
 *             if the key is of an inappropriate type for this map (optionally thrown by the
 *             underlying Map).
 * @throws NullPointerException
 *             if context is null, or if the key is null and this map does not permit null keys
 *             (the latter is optionally thrown by the underlying Map).
 * @throws ELException
 *             if an exception was thrown while performing the property or variable resolution.
 *             The thrown exception must be included as the cause property of this exception, if
 *             available.
 */
@Override
public Object getValue(ELContext context, Object base, Object property) {
  if (context == null) {
    throw new NullPointerException("context is null");
  }
  Object result = null;
  if (isResolvable(base)) {
    JsonNode resultNode = ((JsonNode) base).get(property.toString());
    if (resultNode != null && resultNode.isValueNode()) {
      if (resultNode.isBoolean()) {
        result = resultNode.asBoolean();
      } else if (resultNode.isLong()) {
        result = resultNode.asLong();
      } else if (resultNode.isBigDecimal() || resultNode.isDouble()) {
        result = resultNode.asDouble();
      } else if (resultNode.isTextual()) {
        result = resultNode.asText();
      } else {
        result = resultNode.toString();
      }
      
    } else {
      result = resultNode;
    }
    context.setPropertyResolved(true);
  }
  return result;
}
 
Example 3
Source File: JsonNodeELResolver.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * If the base object is a map, returns the value associated with the given key, as specified by
 * the property argument. If the key was not found, null is returned. If the base is a Map, the
 * propertyResolved property of the ELContext object must be set to true by this resolver,
 * before returning. If this property is not true after this method is called, the caller should
 * ignore the return value. Just as in java.util.Map.get(Object), just because null is returned
 * doesn't mean there is no mapping for the key; it's also possible that the Map explicitly maps
 * the key to null.
 * 
 * @param context
 *            The context of this evaluation.
 * @param base
 *            The map to analyze. Only bases of type Map are handled by this resolver.
 * @param property
 *            The key to return the acceptable type for. Ignored by this resolver.
 * @return If the propertyResolved property of ELContext was set to true, then the value
 *         associated with the given key or null if the key was not found. Otherwise, undefined.
 * @throws ClassCastException
 *             if the key is of an inappropriate type for this map (optionally thrown by the
 *             underlying Map).
 * @throws NullPointerException
 *             if context is null, or if the key is null and this map does not permit null keys
 *             (the latter is optionally thrown by the underlying Map).
 * @throws ELException
 *             if an exception was thrown while performing the property or variable resolution.
 *             The thrown exception must be included as the cause property of this exception, if
 *             available.
 */
@Override
public Object getValue(ELContext context, Object base, Object property) {
  if (context == null) {
    throw new NullPointerException("context is null");
  }
  Object result = null;
  if (isResolvable(base)) {
    JsonNode resultNode = ((JsonNode) base).get(property.toString());
    if (resultNode != null && resultNode.isValueNode()) {
      if (resultNode.isBoolean()) {
        result = resultNode.asBoolean();
      } else if (resultNode.isLong()) {
        result = resultNode.asLong();
      } else if (resultNode.isBigDecimal() || resultNode.isDouble()) {
        result = resultNode.asDouble();
      } else if (resultNode.isTextual()) {
        result = resultNode.asText();
      } else {
        result = resultNode.toString();
      }
      
    } else {
      result = resultNode;
    }
    context.setPropertyResolved(true);
  }
  return result;
}
 
Example 4
Source File: JsonNodeELResolver.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * If the base object is a map, returns the value associated with the given key, as specified by the property argument. If the key was not found, null is returned. If the base is a Map, the
 * propertyResolved property of the ELContext object must be set to true by this resolver, before returning. If this property is not true after this method is called, the caller should ignore the
 * return value. Just as in java.util.Map.get(Object), just because null is returned doesn't mean there is no mapping for the key; it's also possible that the Map explicitly maps the key to null.
 * 
 * @param context
 *            The context of this evaluation.
 * @param base
 *            The map to analyze. Only bases of type Map are handled by this resolver.
 * @param property
 *            The key to return the acceptable type for. Ignored by this resolver.
 * @return If the propertyResolved property of ELContext was set to true, then the value associated with the given key or null if the key was not found. Otherwise, undefined.
 * @throws ClassCastException
 *             if the key is of an inappropriate type for this map (optionally thrown by the underlying Map).
 * @throws NullPointerException
 *             if context is null, or if the key is null and this map does not permit null keys (the latter is optionally thrown by the underlying Map).
 * @throws ELException
 *             if an exception was thrown while performing the property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.
 */
@Override
public Object getValue(ELContext context, Object base, Object property) {
    if (context == null) {
        throw new NullPointerException("context is null");
    }
    Object result = null;
    if (isResolvable(base)) {
        JsonNode resultNode = getResultNode((JsonNode) base, property, context);
        if (resultNode != null && resultNode.isValueNode()) {
            if (resultNode.isBoolean()) {
                result = resultNode.asBoolean();
            } else if (resultNode.isShort() || resultNode.isInt()) {
                result = resultNode.asInt();
            } else if (resultNode.isLong()) {
                result = resultNode.asLong();
            } else if (resultNode.isBigDecimal() || resultNode.isDouble() || resultNode.isFloat()) {
                result = resultNode.asDouble();
            } else if (resultNode.isTextual()) {
                result = resultNode.asText();
            } else if (resultNode.isNull()) {
                result = null;
            } else {
                result = resultNode.toString();
            }

        } else {
            result = resultNode;
        }
        context.setPropertyResolved(true);
    }
    return result;
}
 
Example 5
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 6
Source File: JsonDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EdmPrimitiveTypeKind guessPrimitiveTypeKind(final JsonNode node) {
  return node.isShort() ? EdmPrimitiveTypeKind.Int16 :
    node.isInt() ? EdmPrimitiveTypeKind.Int32 :
      node.isLong() ? EdmPrimitiveTypeKind.Int64 :
        node.isBoolean() ? EdmPrimitiveTypeKind.Boolean :
          node.isFloat() ? EdmPrimitiveTypeKind.Single :
            node.isDouble() ? EdmPrimitiveTypeKind.Double :
              node.isBigDecimal() ? EdmPrimitiveTypeKind.Decimal :
                EdmPrimitiveTypeKind.String;
}
 
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: 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 9
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 10
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 11
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 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: RestDMLServiceImpl.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
protected void callDefaultProcedure(PrintWriter writer, HttpServletRequest request, String jsonpArgName,
                                    JDBCCallableStatement call, Map<String,List<String>> queryParams, String jsonParams) throws SQLException {
    if (queryParams != null) {
        for (Map.Entry<String,List<String>> entry : queryParams.entrySet()) {
            if (jsonpArgName.equals(entry.getKey()))
                continue;
            if (entry.getValue().size() != 1)
                throw new WrongExpressionArityException(1, entry.getValue().size());
            call.setString(entry.getKey(), entry.getValue().get(0));
        }
    }
    if (jsonParams != null) {
        JsonNode parsed;
        try {
            parsed = jsonParser(jsonParams).readValueAsTree();
        }
        catch (IOException ex) {
            throw new AkibanInternalException("Error reading from string", ex);
        }
        if (parsed.isObject()) {
            Iterator<String> iter = parsed.fieldNames();
            while (iter.hasNext()) {
                String field = iter.next();
                JsonNode value = parsed.get(field);
                if (value.isBigDecimal()) {
                    call.setBigDecimal(field, value.decimalValue());
                }
                else if (value.isBoolean()) {
                    call.setBoolean(field, value.asBoolean());
                }
                else if (value.isDouble()) {
                    call.setDouble(field, value.asDouble());
                }
                else if (value.isInt()) {
                    call.setInt(field, value.asInt());
                }
                else if (value.isLong()) {
                    call.setLong(field, value.asLong());
                }
                else {
                    call.setString(field, value.textValue());
                }
            }
        }
        else {
            throw new InvalidArgumentTypeException("JSON must be object or array");
        }
    }
    boolean results = call.execute();
    AkibanAppender appender = AkibanAppender.of(writer);
    appender.append('{');
    boolean first = true;
    JDBCParameterMetaData md = (JDBCParameterMetaData)call.getParameterMetaData();
    for (int i = 1; i <= md.getParameterCount(); i++) {
        String name;
        switch (md.getParameterMode(i)) {
        case ParameterMetaData.parameterModeOut:
        case ParameterMetaData.parameterModeInOut:
            name = md.getParameterName(i);
            if (name == null)
                name = String.format("arg%d", i);
            if (first)
                first = false;
            else
                appender.append(',');
            appender.append('"');
            Quote.DOUBLE_QUOTE.append(appender, name);
            appender.append("\":");
            call.formatAsJson(i, appender, options);
            break;
        }
    }
    int nresults = 0;
    while(results) {
        beginResultSetArray(appender, first, nresults++);
        first = false;
        collectResults((JDBCResultSet) call.getResultSet(), appender, options);
        endResultSetArray(appender);
        results = call.getMoreResults();
    }
    appender.append('}');
}