下面列出了com.fasterxml.jackson.databind.node.BooleanNode#valueOf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Judge Java objects and create JsonNode.
*/
public static JsonNode get( final Object obj ) throws IOException {
if ( obj instanceof PrimitiveObject ) {
return PrimitiveObjectToJsonNode.get( (PrimitiveObject)obj );
} else if ( obj instanceof String ) {
return new TextNode( (String)obj );
} else if ( obj instanceof Boolean ) {
return BooleanNode.valueOf( (Boolean)obj );
} else if ( obj instanceof Short ) {
return IntNode.valueOf( ( (Short)obj ).intValue() );
} else if ( obj instanceof Integer ) {
return IntNode.valueOf( (Integer)obj );
} else if ( obj instanceof Long ) {
return new LongNode( (Long)obj );
} else if ( obj instanceof Float ) {
return new DoubleNode( ( (Float)obj ).doubleValue() );
} else if ( obj instanceof Double ) {
return new DoubleNode( (Double)obj );
} else if ( obj instanceof byte[] ) {
return new BinaryNode( (byte[])obj );
} else if ( obj == null ) {
return NullNode.getInstance();
} else {
return new TextNode( obj.toString() );
}
}
/**
* Convert PrimitiveObject to JsonNode.
*/
public static JsonNode get( final PrimitiveObject obj ) throws IOException {
if ( obj == null ) {
return NullNode.getInstance();
}
switch ( obj.getPrimitiveType() ) {
case BOOLEAN:
return BooleanNode.valueOf( obj.getBoolean() );
case BYTE:
return IntNode.valueOf( obj.getInt() );
case SHORT:
return IntNode.valueOf( obj.getInt() );
case INTEGER:
return IntNode.valueOf( obj.getInt() );
case LONG:
return new LongNode( obj.getLong() );
case FLOAT:
return new DoubleNode( obj.getDouble() );
case DOUBLE:
return new DoubleNode( obj.getDouble() );
case STRING:
return new TextNode( obj.getString() );
case BYTES:
return new BinaryNode( obj.getBytes() );
default:
return NullNode.getInstance();
}
}
@Override
public JsonNode serialize(Boolean object) {
return BooleanNode.valueOf(object);
}
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());
}
/**
* Sets boolean on the specific json pointer.
*
* @param ptr json pointer
* @param isTrue boolean to set
* @throws WorkflowException workflow exception
*/
public void setAt(JsonPointer ptr, Boolean isTrue) throws WorkflowException {
BooleanNode booleanNode = BooleanNode.valueOf(isTrue);
attach(ptr, booleanNode);
}