private static BinaryFormat getFormatEncoder(GrowableByteBuffer buffer, Tensor tensor) {
        boolean hasMappedDimensions = tensor.type().hasMappedDimensions();
        boolean hasIndexedDimensions = tensor.type().hasIndexedDimensions();
        boolean isMixed = hasMappedDimensions && hasIndexedDimensions;

        // TODO: Encoding as indexed if the implementation is mixed is not yet supported so use mixed format instead
        if (tensor instanceof MixedTensor && ! isMixed && hasIndexedDimensions)
            isMixed = true;

        if (isMixed && tensor.type().valueType() == TensorType.Value.DOUBLE) {
            encodeFormatType(buffer, MIXED_BINARY_FORMAT_TYPE);
            return new MixedBinaryFormat();
        }
        else if (isMixed) {
            encodeFormatType(buffer, MIXED_BINARY_FORMAT_WITH_CELLTYPE);
            encodeValueType(buffer, tensor.type().valueType());
            return new MixedBinaryFormat(tensor.type().valueType());
        }
        else if (hasIndexedDimensions && tensor.type().valueType() == TensorType.Value.DOUBLE) {
            encodeFormatType(buffer, DENSE_BINARY_FORMAT_TYPE);
            return new DenseBinaryFormat();
        }
        else if (hasIndexedDimensions) {
            encodeFormatType(buffer, DENSE_BINARY_FORMAT_WITH_CELLTYPE);
            encodeValueType(buffer, tensor.type().valueType());
            return new DenseBinaryFormat(tensor.type().valueType());
        }
        else if (tensor.type().valueType() == TensorType.Value.DOUBLE) {
            encodeFormatType(buffer, SPARSE_BINARY_FORMAT_TYPE);
            return new SparseBinaryFormat();
        }
        else {
            encodeFormatType(buffer, SPARSE_BINARY_FORMAT_WITH_CELLTYPE);
            encodeValueType(buffer, tensor.type().valueType());
            return new SparseBinaryFormat(tensor.type().valueType());
        }
    }
Decode some data to a tensor

@param type the type to decode and validate to, or empty to use the type given in the data
@param buffer the buffer containing the data, use GrowableByteByffer.wrap(byte[]) if you have a byte array
@return the resulting tensor
@throws IllegalArgumentException if the tensor data was invalid
vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java