下面列出了怎么用com.fasterxml.jackson.databind.node.NumericNode的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Points deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
TreeNode n = jp.getCodec().readTree(jp);
Map<String, Object> points = new HashMap<>();
Iterator<String> namesIter = n.fieldNames();
while(namesIter.hasNext()) {
String field = namesIter.next();
TreeNode child = n.get(field);
Object o;
if (child instanceof DoubleNode || child instanceof FloatNode) {
o = ((NumericNode) child).doubleValue();
} else if (child instanceof IntNode || child instanceof LongNode) {
o = ((NumericNode) child).longValue();
} else {
throw new MergeException("Unsupported Type, " + child.getClass());
}
points.put(field, o);
}
return new Points(points);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
LOGGER.trace(">>>> BsonObjectIdToStringDeserializer::deserialize");
JsonNode bsonNode = p.getCodec().readTree(p);
JsonNode nodeValue = bsonNode.get(NODE_KEY);
if(nodeValue == null) {
// parse this as a regular String
if(bsonNode instanceof NumericNode) {
return bsonNode.asText();
}
else if(bsonNode instanceof ObjectNode) {
return (String) jsonNodeDeserializer.deserializeJsonNode(bsonNode);
}
return bsonNode.textValue();
}
LOGGER.trace("<<<< BsonObjectIdToStringDeserializer::deserialize");
return nodeValue.textValue();
}
public static Matcher<JsonNode> jsonNumber(final NumericNode value) {
final JsonParser.NumberType numberType = value.numberType();
switch (numberType) {
case INT:
return jsonInt(value.asInt());
case LONG:
return jsonLong(value.asLong());
case BIG_INTEGER:
return jsonBigInteger(value.bigIntegerValue());
case FLOAT:
return jsonFloat(value.floatValue());
case DOUBLE:
return jsonDouble(value.doubleValue());
case BIG_DECIMAL:
return jsonBigDecimal(value.decimalValue());
default:
throw new UnsupportedOperationException("Unsupported number type " + numberType);
}
}
@Override
public String toString(T input) {
JsonNode jsonNode = mapper.valueToTree(input);
if (jsonNode instanceof TextNode) {
return jsonNode.textValue();
}
if (jsonNode instanceof NumericNode) {
return jsonNode.asText();
}
// fallback to String for complex type
return input.toString();
}
protected BigInteger getQuantity(JsonNode node) {
if (node instanceof NumericNode) {
return BigInteger.valueOf(node.longValue());
}
String value = getHexString(node);
if (value == null) return null;
if (!value.startsWith("0x")) {
return new BigInteger(value, 10);
}
return HexEncoding.fromHex(value);
}
private static Map<Type, GraphQLScalarType> getScalarMapping() {
Map<Type, GraphQLScalarType> scalarMapping = new HashMap<>();
scalarMapping.put(TextNode.class, JsonTextNode);
scalarMapping.put(BooleanNode.class, JsonBooleanNode);
scalarMapping.put(BinaryNode.class, JsonBinaryNode);
scalarMapping.put(BigIntegerNode.class, JsonBigIntegerNode);
scalarMapping.put(IntNode.class, JsonIntegerNode);
scalarMapping.put(ShortNode.class, JsonShortNode);
scalarMapping.put(DecimalNode.class, JsonDecimalNode);
scalarMapping.put(FloatNode.class, JsonFloatNode);
scalarMapping.put(DoubleNode.class, JsonDoubleNode);
scalarMapping.put(NumericNode.class, JsonDecimalNode);
return Collections.unmodifiableMap(scalarMapping);
}
@Override
public void setUp(SetupContext context) {
if (!getTypeMappers().isEmpty()) {
context.getSchemaGenerator().withTypeMappersPrepended(getTypeMappers().toArray(new TypeMapper[0]));
}
if (!getOutputConverters().isEmpty()) {
context.getSchemaGenerator().withOutputConvertersPrepended(getOutputConverters().toArray(new OutputConverter[0]));
}
if (!getInputConverters().isEmpty()) {
context.getSchemaGenerator().withInputConvertersPrepended(getInputConverters().toArray(new InputConverter[0]));
}
context.getSchemaGenerator().withTypeComparators(new SynonymBaseTypeComparator(ObjectNode.class, POJONode.class));
context.getSchemaGenerator().withTypeComparators(new SynonymBaseTypeComparator(DecimalNode.class, NumericNode.class));
}
@Override
protected boolean matchesNode(NumericNode node, Description mismatchDescription) {
final Object number = projection.apply(node);
if (numberMatcher.matches(number)) {
return true;
} else {
mismatchDescription.appendText("was a number node with value that ");
numberMatcher.describeMismatch(number, mismatchDescription);
return false;
}
}
private static Matcher<JsonNode> createNodeMatcher(final JsonNode value) {
final JsonNodeType nodeType = value.getNodeType();
switch (nodeType) {
case ARRAY:
return IsJsonArray.jsonArray((ArrayNode) value);
case BINARY:
throw new UnsupportedOperationException(
"Expected value contains a binary node, which is not implemented.");
case BOOLEAN:
return IsJsonBoolean.jsonBoolean((BooleanNode) value);
case MISSING:
return IsJsonMissing.jsonMissing((MissingNode) value);
case NULL:
return IsJsonNull.jsonNull((NullNode) value);
case NUMBER:
return IsJsonNumber.jsonNumber((NumericNode) value);
case OBJECT:
return IsJsonObject.jsonObject((ObjectNode) value);
case POJO:
throw new UnsupportedOperationException(
"Expected value contains a POJO node, which is not implemented.");
case STRING:
return IsJsonText.jsonText((TextNode) value);
default:
throw new UnsupportedOperationException("Unsupported node type " + nodeType);
}
}
/**
* Convert an opaque JSON node to Decimal using the most correct
* conversion method.
*/
public static Decimal nodeToDecimal(JsonNode node) {
JsonNodeType type = node.getNodeType();
if (type == JsonNodeType.NUMBER) {
return numericToDecimal((NumericNode)node);
} else {
try {
return new Decimal(node.asText());
} catch (ArithmeticException | NumberFormatException e) {
// Fall through..
}
}
return null;
}
/**
* Convert an opaque JSON node to BigDecimal using the most correct
* conversion method.
*/
public static BigDecimal nodeToBigDecimal(JsonNode node) {
JsonNodeType type = node.getNodeType();
if (type == JsonNodeType.NUMBER) {
return numericToBigDecimal((NumericNode)node);
} else {
try {
return new BigDecimal(node.asText());
} catch (ArithmeticException | NumberFormatException e) {
// Fall through..
}
}
return null;
}
/**
* Convert a numeric JSON node to Decimal using the most correct
* conversion method.
*/
private static Decimal numericToDecimal(NumericNode node) {
switch (node.numberType()) {
case INT:
case LONG:
return new Decimal(node.asLong());
case FLOAT:
case DOUBLE:
return new Decimal(node.asDouble());
case BIG_DECIMAL:
case BIG_INTEGER:
default:
return new Decimal(node.decimalValue().toString());
}
}
/**
* Convert a numeric JSON node to BigDecimal using the most correct
* conversion method.
*/
private static BigDecimal numericToBigDecimal(NumericNode node) {
switch (node.numberType()) {
case INT:
case LONG:
return BigDecimal.valueOf(node.asLong());
case FLOAT:
case DOUBLE:
return BigDecimal.valueOf(node.asDouble());
case BIG_DECIMAL:
case BIG_INTEGER:
default:
return node.decimalValue();
}
}
@Override
public Long getLong(String key) {
return Optional.ofNullable(mappings.get(key))
.map(NumericNode.class::cast)
.map(NumericNode::longValue)
.orElse(null);
}
public InvocationMessage(long requestID, NumericNode registrationID, ObjectNode details, ArrayNode arguments, ObjectNode argumentsKw){
this.requestID = requestID;
this.registrationID = registrationID;
this.details = details;
this.arguments = arguments;
this.argumentsKw = argumentsKw;
}
@Override
public CustomAttribute deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
CustomAttribute cda = null;
final String currentName = jp.getParsingContext().getCurrentName();
final ObjectMapper mapper = (ObjectMapper) jp.getCodec();
final ValueNode vNode = mapper.readTree(jp);
if (vNode.asToken().isScalarValue()) {
if (vNode.getNodeType() == JsonNodeType.BOOLEAN) {
cda = new CustomAttribute<Boolean>(currentName, vNode.asBoolean(), Boolean.class);
} else if (vNode.getNodeType() == JsonNodeType.STRING) {
cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
} else if (vNode.getNodeType() == JsonNodeType.NUMBER) {
final NumericNode nNode = (NumericNode) vNode;
if (currentName.endsWith("_at")) {
cda = new CustomAttribute<Long>(currentName, vNode.longValue(), Long.class);
} else if (nNode.isInt()) {
cda = new CustomAttribute<Integer>(currentName, vNode.intValue(), Integer.class);
} else if (nNode.isFloat()) {
cda = new CustomAttribute<Float>(currentName, vNode.floatValue(), Float.class);
} else if (nNode.isDouble()) {
cda = new CustomAttribute<Double>(currentName, vNode.doubleValue(), Double.class);
} else if (nNode.isLong()) {
cda = new CustomAttribute<Long>(currentName, vNode.longValue(), Long.class);
} else {
cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
}
} else {
cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
}
}
return cda;
}
/**
* Gets integer on specific json pointer.
*
* @param ptr json pointer
* @return integer on specific json pointer
* @throws WorkflowException workflow exception
*/
public Integer intAt(JsonPointer ptr) throws WorkflowException {
if (root == null || root instanceof MissingNode) {
throw new WorkflowException("Invalid root node");
}
JsonNode node = root.at(ptr);
if (node instanceof MissingNode) {
return null;
}
if (!(node instanceof NumericNode)) {
throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
}
return ((NumericNode) node).asInt();
}
/**
* Builds RestAccessInfo from json.
* @param root json root node for RestAccessinfo
* @return REST access information
* @throws WorkflowException workflow exception
*/
public static RestAccessInfo valueOf(JsonNode root) throws WorkflowException {
JsonNode node = root.at(ptr(REMOTE_IP));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid remoteIp for " + root);
}
IpAddress remoteIp = IpAddress.valueOf(node.asText());
node = root.at(ptr(PORT));
if (node == null || !(node instanceof NumericNode)) {
throw new WorkflowException("invalid port for " + root);
}
TpPort tpPort = TpPort.tpPort(node.asInt());
node = root.at(ptr(USER));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid user for " + root);
}
String strUser = node.asText();
node = root.at(ptr(PASSWORD));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid password for " + root);
}
String strPassword = node.asText();
return new RestAccessInfo(remoteIp, tpPort, strUser, strPassword);
}
/**
* Builds SshAccessInfo from json.
* @param root json root node for SshAccessinfo
* @return SSH access information
* @throws WorkflowException workflow exception
*/
public static SshAccessInfo valueOf(JsonNode root) throws WorkflowException {
JsonNode node = root.at(ptr(REMOTE_IP));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid remoteIp for " + root);
}
IpAddress sshIp = IpAddress.valueOf(node.asText());
node = root.at(ptr(PORT));
if (node == null || !(node instanceof NumericNode)) {
throw new WorkflowException("invalid port for " + root);
}
TpPort sshPort = TpPort.tpPort(node.asInt());
node = root.at(ptr(USER));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid user for " + root);
}
String sshUser = node.asText();
node = root.at(ptr(PASSWORD));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid password for " + root);
}
String sshPassword = node.asText();
node = root.at(ptr(KEYFILE));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid keyfile for " + root);
}
String sshKeyfile = node.asText();
return new SshAccessInfo(sshIp, sshPort, sshUser, sshPassword, sshKeyfile);
}
/**
* Builds NetconfAccessInfo from json.
* @param root json root node for NetconfAccessinfo
* @return NETCONF access information
* @throws WorkflowException workflow exception
*/
public static NetconfAccessInfo valueOf(JsonNode root) throws WorkflowException {
JsonNode node = root.at(ptr(REMOTE_IP));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid remoteIp for " + root);
}
IpAddress remoteIp = IpAddress.valueOf(node.asText());
node = root.at(ptr(PORT));
if (node == null || !(node instanceof NumericNode)) {
throw new WorkflowException("invalid port for " + root);
}
TpPort tpPort = TpPort.tpPort(node.asInt());
node = root.at(ptr(USER));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid user for " + root);
}
String strUser = node.asText();
node = root.at(ptr(PASSWORD));
if (node == null || !(node instanceof TextNode)) {
throw new WorkflowException("invalid password for " + root);
}
String strPassword = node.asText();
return new NetconfAccessInfo(remoteIp, tpPort, strUser, strPassword);
}
@Override
public int compare(JsonNode o1, JsonNode o2) {
if (o1.equals(o2)) {
return 0;
}
if ((o1 instanceof NumericNode) && (o2 instanceof NumericNode)) {
Double d1 = ((NumericNode) o1).asDouble();
Double d2 = ((NumericNode) o2).asDouble();
if (d1.compareTo(d2) == 0) {
return 0;
}
}
return 1;
}
private IsJsonNumber(final Matcher<?> numberMatcher,
final Function<NumericNode, Object> projection) {
super(JsonNodeType.NUMBER);
this.numberMatcher = Objects.requireNonNull(numberMatcher);
this.projection = Objects.requireNonNull(projection);
}
public static Matcher<JsonNode> jsonInt(int number) {
return new IsJsonNumber(is(number), NumericNode::asInt);
}
public static Matcher<JsonNode> jsonInt(Matcher<? super Integer> numberMatcher) {
return new IsJsonNumber(numberMatcher, NumericNode::asInt);
}
public static Matcher<JsonNode> jsonLong(long number) {
return new IsJsonNumber(is(number), NumericNode::asLong);
}
public static Matcher<JsonNode> jsonLong(Matcher<? super Long> numberMatcher) {
return new IsJsonNumber(numberMatcher, NumericNode::asLong);
}
public static Matcher<JsonNode> jsonBigInteger(BigInteger number) {
return new IsJsonNumber(is(number), NumericNode::bigIntegerValue);
}
public static Matcher<JsonNode> jsonBigInteger(Matcher<? super BigInteger> numberMatcher) {
return new IsJsonNumber(numberMatcher, NumericNode::bigIntegerValue);
}
public static Matcher<JsonNode> jsonFloat(float number) {
return new IsJsonNumber(is(number), NumericNode::floatValue);
}
public static Matcher<JsonNode> jsonFloat(Matcher<? super Float> numberMatcher) {
return new IsJsonNumber(numberMatcher, NumericNode::floatValue);
}