public static JsonNode get( final PrimitiveObject obj ) throws IOException{ 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 new TextNode( null ); } }
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() ); } }
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() ); } }
public Datapoint addBytesValue(long time, byte[] value) { initialValues(); if (values != null && !values.isEmpty() && (type == null || !type.equals(TsdbConstants.TYPE_BYTES))) { throw new IllegalStateException("There is already another type in datapoint, " + "could not add byte array type again"); } type = TsdbConstants.TYPE_BYTES; values.add(Lists.<JsonNode> newArrayList(new LongNode(time), new BinaryNode(value))); return this; }
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()); } }
@Override public Schema binary(BinaryNode ignored) { return Schema.create(Schema.Type.BYTES); }
private static <T> T visit(JsonNode node, JsonTreeVisitor<T> visitor) { switch (node.getNodeType()) { case OBJECT: Preconditions.checkArgument(node instanceof ObjectNode, "Expected instance of ObjectNode: " + node); // use LinkedHashMap to preserve field order Map<String, T> fields = Maps.newLinkedHashMap(); Iterator<Map.Entry<String, JsonNode>> iter = node.fields(); while (iter.hasNext()) { Map.Entry<String, JsonNode> entry = iter.next(); visitor.recordLevels.push(entry.getKey()); fields.put(entry.getKey(), visit(entry.getValue(), visitor)); visitor.recordLevels.pop(); } return visitor.object((ObjectNode) node, fields); case ARRAY: Preconditions.checkArgument(node instanceof ArrayNode, "Expected instance of ArrayNode: " + node); List<T> elements = Lists.newArrayListWithExpectedSize(node.size()); for (JsonNode element : node) { elements.add(visit(element, visitor)); } return visitor.array((ArrayNode) node, elements); case BINARY: Preconditions.checkArgument(node instanceof BinaryNode, "Expected instance of BinaryNode: " + node); return visitor.binary((BinaryNode) node); case STRING: Preconditions.checkArgument(node instanceof TextNode, "Expected instance of TextNode: " + node); return visitor.text((TextNode) node); case NUMBER: Preconditions.checkArgument(node instanceof NumericNode, "Expected instance of NumericNode: " + node); return visitor.number((NumericNode) node); case BOOLEAN: Preconditions.checkArgument(node instanceof BooleanNode, "Expected instance of BooleanNode: " + node); return visitor.bool((BooleanNode) node); case MISSING: Preconditions.checkArgument(node instanceof MissingNode, "Expected instance of MissingNode: " + node); return visitor.missing((MissingNode) node); case NULL: Preconditions.checkArgument(node instanceof NullNode, "Expected instance of NullNode: " + node); return visitor.nullNode((NullNode) node); default: throw new IllegalArgumentException( "Unknown node type: " + node.getNodeType() + ": " + node); } }