/** * Transform the JSON node into a node response POJO. * * @param jsonNode * @param key * @param expand * @return * @throws IOException */ private NodeField transformNodeFieldJsonNode(JsonNode jsonNode, String key, boolean expand) throws IOException { // Unwrap stored pojos if (jsonNode.isPojo()) { Object pojo = ((POJONode) jsonNode).getPojo(); if (pojo != null) { if (pojo instanceof NodeFieldImpl) { return pojoNodeToValue(jsonNode, NodeFieldImpl.class, key); } else { return pojoNodeToValue(jsonNode, NodeResponse.class, key); } } else { return null; } } ObjectMapper mapper = JsonUtil.getMapper(); if (expand) { return JsonUtil.readValue(jsonNode.toString(), NodeResponse.class); } else { if (jsonNode.isNull()) { return null; } return mapper.treeToValue(jsonNode, NodeFieldImpl.class); } }
/** * {@inheritDoc} */ @Override public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode tree = jp.readValueAsTree(); if (tree.isPojo()) { POJONode pojoNode = (POJONode) tree; Object pojo = pojoNode.getPojo(); if (pojo instanceof Date) { return (Date) pojoNode.getPojo(); } else { throw new RuntimeException("unsupported date type, expected: " + Date.class.getName()); } } String stringDate = tree.asText(); StdDateFormat stdDateFormat = new StdDateFormat(); try { return stdDateFormat.parse(stringDate); } catch (ParseException e) { throw Throwables.propagate(e); } }
public static PrimitiveObject get( final JsonNode jsonNode ) throws IOException{ if( jsonNode instanceof TextNode ){ return new StringObj( ( (TextNode)jsonNode ).textValue() ); } else if( jsonNode instanceof BooleanNode ){ return new BooleanObj( ( (BooleanNode)jsonNode ).booleanValue() ); } else if( jsonNode instanceof IntNode ){ return new IntegerObj( ( (IntNode)jsonNode ).intValue() ); } else if( jsonNode instanceof LongNode ){ return new LongObj( ( (LongNode)jsonNode ).longValue() ); } else if( jsonNode instanceof DoubleNode ){ return new DoubleObj( ( (DoubleNode)jsonNode ).doubleValue() ); } else if( jsonNode instanceof BigIntegerNode ){ return new StringObj( ( (BigIntegerNode)jsonNode ).bigIntegerValue().toString() ); } else if( jsonNode instanceof DecimalNode ){ return new StringObj( ( (DecimalNode)jsonNode ).decimalValue().toString() ); } else if( jsonNode instanceof BinaryNode ){ return new BytesObj( ( (BinaryNode)jsonNode ).binaryValue() ); } else if( jsonNode instanceof POJONode ){ return new BytesObj( ( (POJONode)jsonNode ).binaryValue() ); } else if( jsonNode instanceof NullNode ){ return NullObj.getInstance(); } else if( jsonNode instanceof MissingNode ){ return NullObj.getInstance(); } else{ return new StringObj( jsonNode.toString() ); } }
/** * Unwrapped the POJO within the JSON node. * * @param jsonNode * @param clazz * @param key * @return */ private <T extends Field> T pojoNodeToValue(JsonNode jsonNode, Class<T> clazz, String key) { Object pojo = ((POJONode) jsonNode).getPojo(); if (pojo == null) { return null; } if (clazz.isAssignableFrom(pojo.getClass())) { return clazz.cast(pojo); } else { throw error(BAD_REQUEST, "The field value for {" + key + "} is not of correct type. Stored POJO was of class {" + pojo.getClass() .getName() + "}"); } }
@Override public boolean isExpandedNodeField(String fieldKey) { JsonNode field = node.get(fieldKey); if (field == null) { return false; } if (field.isObject() && field.has("fields")) { return true; } if (field.isPojo()) { return ((POJONode) field).getPojo() instanceof NodeResponse; } return false; }
protected boolean isFieldNull(DynamicBeanField pCheckedField) { if (!hasField(pCheckedField)) return true; JsonNode tCheckedJsonNode = getFieldJsonNode(pCheckedField); if (tCheckedJsonNode.isPojo()) { return ((POJONode) tCheckedJsonNode).getPojo() == null; } else { return getFieldJsonNode(pCheckedField).isNull(); } }