@Override
    public ObjectNode encode(RoleInfo roleInfo, CodecContext context) {
        checkNotNull(roleInfo, "RoleInfo cannot be null");

        ObjectNode result = context.mapper().createObjectNode();

        if (roleInfo.master() != null) {
            result.put(MASTER, roleInfo.master().id());
        }

        ArrayNode backups = context.mapper().createArrayNode();
        roleInfo.backups().forEach(backup -> backups.add(backup.id()));

        if (!roleInfo.backups().isEmpty()) {
            result.set(BACKUPS, backups);
        }

        return result;
    }
@Test
    public void testRoleInfoEncode() {
        NodeId masterNodeId = NodeId.nodeId("1");
        NodeId backupNodeId1 = NodeId.nodeId("1");
        NodeId backupNodeId2 = NodeId.nodeId("2");
        NodeId backupNodeId3 = NodeId.nodeId("3");
        List<NodeId> backupNodeIds =
                ImmutableList.of(backupNodeId1, backupNodeId2, backupNodeId3);

        RoleInfo roleInfo = new RoleInfo(masterNodeId, backupNodeIds);
        ObjectNode roleInfoJson = roleInfoCodec.encode(roleInfo, context);
        assertThat(roleInfoJson, RoleInfoJsonMatcher.matchesRoleInfo(roleInfo));
    }