@Override
    public ObjectNode encode(TrafficTreatment treatment, CodecContext context) {
        checkNotNull(treatment, "Traffic treatment cannot be null");

        final ObjectNode result = context.mapper().createObjectNode();
        final ArrayNode jsonInstructions = result.putArray(INSTRUCTIONS);

        final JsonCodec<Instruction> instructionCodec =
                context.codec(Instruction.class);

        for (final Instruction instruction : treatment.immediate()) {
            jsonInstructions.add(instructionCodec.encode(instruction, context));
        }

        if (treatment.metered() != null) {
            for (Instructions.MeterInstruction instruction : treatment.meters()) {
                jsonInstructions.add(instructionCodec.encode(instruction, context));
            }
        }
        if (treatment.tableTransition() != null) {
            jsonInstructions.add(instructionCodec.encode(treatment.tableTransition(), context));
        }
        if (treatment.clearedDeferred()) {
            result.put(CLEAR_DEFERRED, true);
        }

        final ArrayNode jsonDeferred = result.putArray(DEFERRED);

        for (final Instruction instruction : treatment.deferred()) {
            jsonDeferred.add(instructionCodec.encode(instruction, context));
        }

        return result;
    }
@Test
    public void testTrafficTreatmentEncode() {

        Instruction output = Instructions.createOutput(PortNumber.portNumber(0));
        Instruction modL2Src = Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
        Instruction modL2Dst = Instructions.modL2Dst(MacAddress.valueOf("44:55:66:77:88:99"));
        MeterId meterId = MeterId.meterId(1);
        Instruction meter = Instructions.meterTraffic(meterId);
        Instruction transition = Instructions.transition(1);
        TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
        TrafficTreatment treatment = tBuilder
                .add(output)
                .add(modL2Src)
                .add(modL2Dst)
                .add(meter)
                .add(transition)
                .build();

        ObjectNode treatmentJson = trafficTreatmentCodec.encode(treatment, context);
        assertThat(treatmentJson, TrafficTreatmentJsonMatcher.matchesTrafficTreatment(treatment));
    }