@Override
    public void visit(LwM2mObject object) {
      LOG.trace("Encoding object instances {} into TLV", object);

      Tlv[] tlvs = null;

      ObjectModel objectModel = model.getObjectModel(object.getId());
      if (objectModel != null && !objectModel.multiple) {
        // single instance object, the instance is level is not needed
        tlvs = encodeResources(object.getInstances().get(0).getResources().values());
      } else {
        tlvs = new Tlv[object.getInstances().size()];
        int i = 0;
        for (Entry<Integer, LwM2mObjectInstance> instance : object.getInstances().entrySet()) {
          Tlv[] resources = encodeResources(instance.getValue().getResources().values());
          tlvs[i] = new Tlv(TlvType.OBJECT_INSTANCE, resources, null, instance.getKey());
          i++;
        }
      }

      try {
        out.write(TlvEncoder.encode(tlvs).array());
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
 private byte[] encodeTlvValue(Value<?> value) {
   LOG.trace("Encoding value {} in TLV", value);
   switch (value.type) {
     case STRING:
       return TlvEncoder.encodeString((String) value.value);
     case INTEGER:
     case LONG:
       return TlvEncoder.encodeInteger((Number) value.value);
     case FLOAT:
     case DOUBLE:
       return TlvEncoder.encodeFloat((Number) value.value);
     case BOOLEAN:
       return TlvEncoder.encodeBoolean((Boolean) value.value);
     case TIME:
       return TlvEncoder.encodeDate((Date) value.value);
     case OPAQUE:
       return (byte[]) value.value;
     default:
       throw new IllegalArgumentException("Invalid value type: " + value.type);
   }
 }
    @Override
    public void visit(LwM2mResource resource) {
      LOG.trace("Encoding resource {} into TLV", resource);

      Tlv rTlv = encodeResource(resource);

      try {
        out.write(TlvEncoder.encode(new Tlv[] {rTlv}).array());
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    @Override
    public void visit(LwM2mObjectInstance instance) {
      LOG.trace("Encoding object instance {} into TLV", instance);

      // The instance is encoded as an array of resource TLVs.
      Tlv[] rTlvs = encodeResources(instance.getResources().values());

      try {
        out.write(TlvEncoder.encode(rTlvs).array());
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }