@Override
    public EncodedClusterStateBundle encode(ClusterStateBundle stateBundle) {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        if (stateBundle.deferredActivation()) {
            root.setBool("deferred-activation", stateBundle.deferredActivation());
        }
        Cursor states = root.setObject("states");
        // TODO add another function that is not toString for this..!
        states.setString("baseline", stateBundle.getBaselineClusterState().toString());
        Cursor spaces = states.setObject("spaces");
        stateBundle.getDerivedBucketSpaceStates().forEach((key, value) -> spaces.setString(key, value.toString()));

        // Only bother to encode feed block state if cluster is actually blocked
        if (stateBundle.getFeedBlock().map(ClusterStateBundle.FeedBlock::blockFeedInCluster).orElse(false)) {
            Cursor feedBlock = root.setObject("feed-block");
            feedBlock.setBool("block-feed-in-cluster", true);
            feedBlock.setString("description", stateBundle.getFeedBlock().get().getDescription());
        }

        stateBundle.distributionConfig().ifPresent(cfg -> {
            SlimeUtils.copyObject(cfg.precomputedSlimeRepr().get(), root.setObject("distribution-config"));
        });

        Compressor.Compression compression = BinaryFormat.encode_and_compress(slime, compressor);
        return EncodedClusterStateBundle.fromCompressionBuffer(compression);
    }
Implementation of ClusterStateBundleCodec which uses structured Slime binary encoding
to implement (de-)serialization of ClusterStateBundle instances. Encoding format is
intentionally extensible so that we may add other information to it later.

LZ4 compression is transparently applied during encoding and decompression is
subsequently applied during decoding.

Implements optional Slime-based enveloping for *WithEnvelope methods, which removes
need to explicitly track compression metadata by the caller.
clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/rpc/SlimeClusterStateBundleCodec.java