@Override
    public ObjectNode encode(MappingEntry mappingEntry, CodecContext context) {

        checkNotNull(mappingEntry, "Mapping entry cannot be null");

        final ObjectNode result = context.mapper().createObjectNode()
                .put(ID, Long.toString(mappingEntry.id().value()))
                .put(DEVICE_ID, mappingEntry.deviceId().toString())
                .put(STATE, mappingEntry.state().toString());

        if (mappingEntry.key() != null) {
            final JsonCodec<MappingKey> keyCodec =
                    context.codec(MappingKey.class);
            result.set(KEY, keyCodec.encode(mappingEntry.key(), context));
        }

        if (mappingEntry.value() != null) {
            final JsonCodec<MappingValue> valueCodec =
                    context.codec(MappingValue.class);
            result.set(VALUE, valueCodec.encode(mappingEntry.value(), context));
        }

        return result;
    }
@Test
    public void testMappingEntryEncode() {
        MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);

        MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT);
        MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY);
        MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
        MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY);

        MappingKey key = DefaultMappingKey.builder()
                .withAddress(address)
                .build();

        MappingTreatment treatment = DefaultMappingTreatment.builder()
                .add(unicastWeight)
                .add(unicastPriority)
                .add(multicastWeight)
                .add(multicastPriority)
                .withAddress(address)
                .build();

        MappingAction action = MappingActions.noAction();

        MappingValue value = DefaultMappingValue.builder()
                .add(treatment)
                .withAction(action)
                .build();

        Mapping mapping = DefaultMapping.builder()
                                .withId(ID)
                                .forDevice(DEVICE_ID)
                                .withKey(key)
                                .withValue(value)
                                .build();

        MappingEntry entry = new DefaultMappingEntry(mapping, STATE);

        ObjectNode entryJson = entryCodec.encode(entry, context);
        assertThat(entryJson, MappingEntryJsonMatcher.matchesMappingEntry(entry));
    }