@Override
    public ObjectNode encode(Region region, CodecContext context) {
        checkNotNull(region, NULL_REGION_MSG);

        ObjectNode result = context.mapper().createObjectNode()
                .put(REGION_ID, region.id().toString())
                .put(NAME, region.name())
                .put(TYPE, region.type().toString());

        ArrayNode masters = context.mapper().createArrayNode();

        region.masters().forEach(sets -> {
            ArrayNode setsJson = context.mapper().createArrayNode();
            sets.forEach(nodeId -> setsJson.add(nodeId.toString()));
            masters.add(setsJson);
        });
        result.set(MASTERS, masters);
        return annotate(result, region, context);
    }
@Test
    public void testRegionEncode() {
        NodeId nodeId1 = NodeId.nodeId("1");
        NodeId nodeId2 = NodeId.nodeId("2");
        NodeId nodeId3 = NodeId.nodeId("3");
        NodeId nodeId4 = NodeId.nodeId("4");

        Set<NodeId> set1 = ImmutableSet.of(nodeId1);
        Set<NodeId> set2 = ImmutableSet.of(nodeId1, nodeId2);
        Set<NodeId> set3 = ImmutableSet.of(nodeId1, nodeId2, nodeId3);
        Set<NodeId> set4 = ImmutableSet.of(nodeId1, nodeId2, nodeId3, nodeId4);
        List<Set<NodeId>> masters = ImmutableList.of(set1, set2, set3, set4);

        RegionId regionId = RegionId.regionId("1");
        String name = "foo";
        Region.Type type = Region.Type.ROOM;
        Annotations noAnnots = DefaultAnnotations.EMPTY;

        Region region = new DefaultRegion(regionId, name, type, noAnnots, masters);

        ObjectNode regionJson = regionCodec.encode(region, context);
        assertThat(regionJson, matchesRegion(region));
    }