下面列出了怎么用com.fasterxml.jackson.databind.node.FloatNode的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);
}
@Test
public void testIsDecimalFunction() {
// check that is-decimal still works even if input
// is a FloatNode and not a DoubleNode
Expression expr = Parser.compileString("is-decimal(.)");
JsonNode context = new FloatNode(1.0f);
JsonNode actual = expr.apply(context);
assertTrue(actual.isBoolean());
assertTrue(actual.booleanValue());
}
@Override
public Number serialize(Object dataFetcherResult) {
if (dataFetcherResult instanceof FloatNode) {
return ((FloatNode) dataFetcherResult).numberValue();
} else {
throw serializationException(dataFetcherResult, FloatNode.class);
}
}
@Override
public FloatNode parseValue(Object input) {
if (input instanceof Number || input instanceof String) {
return FloatNode.valueOf(new BigDecimal(input.toString()).floatValue());
}
if (input instanceof FloatNode) {
return (FloatNode) input;
}
throw valueParsingException(input, Number.class, String.class, FloatNode.class);
}
@Override
public FloatNode parseLiteral(Object input) {
if (input instanceof IntValue) {
return FloatNode.valueOf(((IntValue) input).getValue().floatValue());
} if (input instanceof FloatValue) {
return FloatNode.valueOf(((FloatValue) input).getValue().floatValue());
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class, FloatValue.class));
}
}
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);
}
private JsonNode createNode(Object value) {
if (value instanceof Float) {
return new FloatNode((Float) value);
} else if (value instanceof Double) {
return new DoubleNode((Double) value);
}
return null;
}
private Example createExample(JsonNode node) {
if (node instanceof ObjectNode) {
ObjectExample obj = new ObjectExample();
ObjectNode on = (ObjectNode) node;
for (Iterator<Entry<String, JsonNode>> x = on.fields(); x.hasNext(); ) {
Entry<String, JsonNode> i = x.next();
String key = i.getKey();
JsonNode value = i.getValue();
obj.put(key, createExample(value));
}
return obj;
} else if (node instanceof ArrayNode) {
ArrayExample arr = new ArrayExample();
ArrayNode an = (ArrayNode) node;
for (JsonNode childNode : an) {
arr.add(createExample(childNode));
}
return arr;
} else if (node instanceof DoubleNode) {
return new DoubleExample(node.doubleValue());
} else if (node instanceof IntNode || node instanceof ShortNode) {
return new IntegerExample(node.intValue());
} else if (node instanceof FloatNode) {
return new FloatExample(node.floatValue());
} else if (node instanceof BigIntegerNode) {
return new BigIntegerExample(node.bigIntegerValue());
} else if (node instanceof DecimalNode) {
return new DecimalExample(node.decimalValue());
} else if (node instanceof LongNode) {
return new LongExample(node.longValue());
} else if (node instanceof BooleanNode) {
return new BooleanExample(node.booleanValue());
} else {
return new StringExample(node.asText());
}
}
@Override
public Optional<Float> deserialize(JsonNode json) {
if (json instanceof FloatNode) {
return Optional.of(json.floatValue());
} else if (json instanceof DoubleNode) {
return Optional.of(json.floatValue());
} else {
return Optional.empty();
}
}
@Override
public Optional<Double> deserialize(JsonNode json) {
if (json instanceof DoubleNode || json instanceof FloatNode) {
return Optional.of(json.asDouble());
} else {
return Optional.empty();
}
}
private Object jsonNodeToValue(JsonNode node) {
Object value = node;
if (node instanceof NullNode) {
value = null;
} else if (node instanceof TextNode) {
value = node.asText();
} else if (node instanceof BooleanNode) {
value = node.asBoolean();
} else if (node instanceof IntNode) {
value = node.asInt();
} else if (node instanceof FloatNode) {
value = (float)((FloatNode)node).asDouble();
} else if (node instanceof DoubleNode) {
value = (float)((DoubleNode)node).asDouble();
} else if (node instanceof ArrayNode) {
int numItems = node.size();
String[] values = new String[numItems];
for (int i = 0; i < numItems; i++) {
values[i] = node.path(i).asText();
}
value = values;
}
return (value);
}
public JsonNode convert(Object node) {
if (node == null)
return NullNode.instance;
else
return new FloatNode((Float) node);
}
@Override
public JsonNode serialize(Float object) {
return FloatNode.valueOf(object);
}
private Float convert(Float value) {
return (Float) FloatConverter.converter().value(new FloatNode(value));
}
@Activate
public void activate() {
appId = coreService.registerApplication("org.onosproject.workplacestore");
log.info("appId=" + appId);
KryoNamespace workplaceNamespace = KryoNamespace.newBuilder()
.register(KryoNamespaces.API)
.register(WorkflowData.class)
.register(Workplace.class)
.register(DefaultWorkplace.class)
.register(WorkflowContext.class)
.register(DefaultWorkflowContext.class)
.register(SystemWorkflowContext.class)
.register(WorkflowState.class)
.register(ProgramCounter.class)
.register(DataModelTree.class)
.register(JsonDataModelTree.class)
.register(List.class)
.register(ArrayList.class)
.register(JsonNode.class)
.register(ObjectNode.class)
.register(TextNode.class)
.register(LinkedHashMap.class)
.register(ArrayNode.class)
.register(BaseJsonNode.class)
.register(BigIntegerNode.class)
.register(BinaryNode.class)
.register(BooleanNode.class)
.register(ContainerNode.class)
.register(DecimalNode.class)
.register(DoubleNode.class)
.register(FloatNode.class)
.register(IntNode.class)
.register(JsonNodeType.class)
.register(LongNode.class)
.register(MissingNode.class)
.register(NullNode.class)
.register(NumericNode.class)
.register(POJONode.class)
.register(ShortNode.class)
.register(ValueNode.class)
.register(JsonNodeCreator.class)
.register(JsonNodeFactory.class)
.build();
localWorkplaceMap.clear();
workplaceMap = storageService.<String, WorkflowData>consistentMapBuilder()
.withSerializer(Serializer.using(workplaceNamespace))
.withName("workplace-map")
.withApplicationId(appId)
.build();
workplaceMap.addListener(workplaceMapEventListener);
localContextMap.clear();
contextMap = storageService.<String, WorkflowData>consistentMapBuilder()
.withSerializer(Serializer.using(workplaceNamespace))
.withName("workflow-context-map")
.withApplicationId(appId)
.build();
contextMap.addListener(contextMapEventListener);
workplaceMapEventListener.syncLocal();
contextMapEventListener.syncLocal();
log.info("Started");
}
public JsonNode toJson(Object object) throws IOException {
if (object instanceof SBase) {
SBase base = (SBase) object;
ObjectNode jsonObject = OBJECT_MAPPER.createObjectNode();
jsonObject.put("__type", base.getSClass().getSimpleName());
for (SField field : base.getSClass().getAllFields()) {
jsonObject.set(field.getName(), toJson(base.sGet(field)));
}
return jsonObject;
} else if (object instanceof Collection) {
Collection<?> collection = (Collection<?>) object;
ArrayNode jsonArray = OBJECT_MAPPER.createArrayNode();
for (Object value : collection) {
jsonArray.add(toJson(value));
}
return jsonArray;
} else if (object instanceof Date) {
return new LongNode(((Date) object).getTime());
} else if (object instanceof DataHandler) {
DataHandler dataHandler = (DataHandler) object;
InputStream inputStream = dataHandler.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(inputStream, out);
return new TextNode(new String(Base64.encodeBase64(out.toByteArray()), Charsets.UTF_8));
} else if (object instanceof Boolean) {
return BooleanNode.valueOf((Boolean) object);
} else if (object instanceof String) {
return new TextNode((String) object);
} else if (object instanceof Long) {
return new LongNode((Long) object);
} else if (object instanceof UUID) {
return new TextNode(((UUID) object).toString());
} else if (object instanceof Integer) {
return new IntNode((Integer) object);
} else if (object instanceof Double) {
return new DoubleNode((Double) object);
} else if (object instanceof Float) {
return new FloatNode((Float) object);
} else if (object instanceof Enum) {
return new TextNode(object.toString());
} else if (object == null) {
return NullNode.getInstance();
} else if (object instanceof byte[]) {
byte[] data = (byte[]) object;
return new TextNode(new String(Base64.encodeBase64(data), Charsets.UTF_8));
}
throw new UnsupportedOperationException(object.getClass().getName());
}
private static <T> void testTypeDecoding(Decoder.Type type, Map<T, byte[]> tests)
throws IOException {
NodeCache cache = new CHMCache();
for (Map.Entry<T, byte[]> entry : tests.entrySet()) {
T expect = entry.getKey();
byte[] input = entry.getValue();
String desc = "decoded " + type.name() + " - " + expect;
try (FileChannel fc = DecoderTest.getFileChannel(input)) {
MappedByteBuffer mmap = fc.map(MapMode.READ_ONLY, 0, fc.size());
Decoder decoder = new Decoder(cache, mmap, 0);
decoder.POINTER_TEST_HACK = true;
// XXX - this could be streamlined
if (type.equals(Decoder.Type.BYTES)) {
assertArrayEquals(desc, (byte[]) expect, decoder.decode(0).binaryValue());
} else if (type.equals(Decoder.Type.ARRAY)) {
assertEquals(desc, expect, decoder.decode(0));
} else if (type.equals(Decoder.Type.UINT16)
|| type.equals(Decoder.Type.INT32)) {
assertEquals(desc, expect, decoder.decode(0).asInt());
} else if (type.equals(Decoder.Type.UINT32)
|| type.equals(Decoder.Type.POINTER)) {
assertEquals(desc, expect, decoder.decode(0).asLong());
} else if (type.equals(Decoder.Type.UINT64)
|| type.equals(Decoder.Type.UINT128)) {
assertEquals(desc, expect, decoder.decode(0).bigIntegerValue());
} else if (type.equals(Decoder.Type.DOUBLE)) {
assertEquals(desc, expect, decoder.decode(0).asDouble());
} else if (type.equals(Decoder.Type.FLOAT)) {
assertEquals(desc, new FloatNode((Float) expect), decoder.decode(0));
} else if (type.equals(Decoder.Type.UTF8_STRING)) {
assertEquals(desc, expect, decoder.decode(0).asText());
} else if (type.equals(Decoder.Type.BOOLEAN)) {
assertEquals(desc, expect, decoder.decode(0).asBoolean());
} else {
assertEquals(desc, expect, decoder.decode(0));
}
}
}
}