class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {

    @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) {
            jsonInstructions.add(instructionCodec.encode(treatment.metered(), context));
        }
        if (treatment.tableTransition() != null) {
            jsonInstructions.add(instructionCodec.encode(treatment.tableTransition(), context));
        }

        final ArrayNode jsonDeferred = result.putArray("deferred");

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

        return result;
    }

}