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() ); } }
private static void emitJsonNode(StringBuilder buf, JsonNode node) { if (node.isNumber()) { // Formatting of numbers depending on type switch (node.numberType()) { case BIG_INTEGER: buf.append(((BigIntegerNode)node).bigIntegerValue().toString()); break; case BIG_DECIMAL: buf.append(((DecimalNode)node).decimalValue().toPlainString()); break; case INT: case LONG: buf.append(node.asLong()); break; case FLOAT: case DOUBLE: double val = node.asDouble(); buf.append(Double.toString(val)); break; default: break; } } else if (node.isArray()) { // JavaScript Array.toString() will comma-delimit the elements. for (int i = 0, size = node.size(); i < size; i++) { if (i >= 1) { buf.append(","); } buf.append(node.path(i).asText()); } } else if (!node.isNull() && !node.isMissingNode()) { buf.append(node.asText()); } }
public static Object unwrap(Object val) { // Can Jackson do this via // ObjectMapper.treeToValue()? The // spec is unclear Object result = val; ObjectMapper mapper = new ObjectMapper(); if (val instanceof ObjectNode) { result = mapper.convertValue((ObjectNode) val, Map.class); } else if (val instanceof ArrayNode) { result = mapper.convertValue((ObjectNode) val, List.class); } else if (val instanceof NullNode) { result = null; } else if (val instanceof BooleanNode) { result = ((BooleanNode) val).booleanValue(); } else if (val instanceof ShortNode) { result = ((ShortNode) val).shortValue(); } else if (val instanceof IntNode) { result = ((IntNode) val).intValue(); } else if (val instanceof LongNode) { result = ((LongNode) val).longValue(); } else if (val instanceof DoubleNode) { result = ((DoubleNode) val).doubleValue(); } else if (val instanceof FloatNode) { result = ((FloatNode) val).floatValue(); } else if (val instanceof BigIntegerNode) { result = ((BigIntegerNode) val).bigIntegerValue(); } else if (val instanceof DecimalNode) { result = ((DecimalNode) val).decimalValue(); } return result; }
public void IntegerValidator() { intTypes.add(IntNode.class); intTypes.add(ShortNode.class); intTypes.add(BigIntegerNode.class); }
private Result decodeByType(Type type, int offset, int size) throws IOException { // MAP, ARRAY, and BOOLEAN do not use newOffset as we don't read the // next <code>size</code> bytes. For all other types, we do. int newOffset = offset + size; switch (type) { case MAP: return this.decodeMap(size, offset); case ARRAY: return this.decodeArray(size, offset); case BOOLEAN: return new Result(Decoder.decodeBoolean(size), offset); case UTF8_STRING: TextNode s = new TextNode(this.decodeString(size)); return new Result(s, newOffset); case DOUBLE: return new Result(this.decodeDouble(), newOffset); case FLOAT: return new Result(this.decodeFloat(), newOffset); case BYTES: BinaryNode b = new BinaryNode(this.getByteArray(size)); return new Result(b, newOffset); case UINT16: IntNode i = this.decodeUint16(size); return new Result(i, newOffset); case UINT32: LongNode l = this.decodeUint32(size); return new Result(l, newOffset); case INT32: IntNode int32 = this.decodeInt32(size); return new Result(int32, newOffset); case UINT64: BigIntegerNode bi = this.decodeBigInteger(size); return new Result(bi, newOffset); case UINT128: BigIntegerNode uint128 = this.decodeBigInteger(size); return new Result(uint128, newOffset); default: throw new InvalidDatabaseException( "Unknown or unexpected type: " + type.name()); } }
private BigIntegerNode decodeBigInteger(int size) { byte[] bytes = this.getByteArray(size); return new BigIntegerNode(new BigInteger(1, bytes)); }