@Override
  public void marshall(
      Object value, Class<?> type, DataOutput dataOutput, SerializationContext serializationContext)
      throws IOException {
    writePossibleNull(value, dataOutput);

    Map<?, ?> map = (Map<?, ?>) value;
    dataOutput.writeInt(map.size());
    for (Entry<?, ?> entry : map.entrySet()) {
      Marshaller keyMarshaller;
      Marshaller valueMarshaller;
      if (mapKeyType != null) {
        ensureMarshallersInitialized(serializationContext);
        keyMarshaller = mapKeyTypeMarshaller;
        valueMarshaller = mapValueTypeMarshaller;
      } else {
        keyMarshaller =
            entry.getKey() != null
                ? serializationContext.findMarshaller(entry.getKey().getClass())
                : null;
        valueMarshaller =
            entry.getValue() != null
                ? serializationContext.findMarshaller(entry.getValue().getClass())
                : null;
      }

      if (writePossibleNull(entry.getKey(), dataOutput)) {
        ClassDefinition keyClassDefinition =
            serializationContext
                .getClassDefinitionContainer()
                .getClassDefinitionByType(entry.getKey().getClass());
        dataOutput.writeLong(keyClassDefinition.getId());
        keyMarshaller.marshall(
            entry.getKey(), entry.getKey().getClass(), dataOutput, serializationContext);
      }

      if (writePossibleNull(entry.getValue(), dataOutput)) {
        ClassDefinition valueClassDefinition =
            serializationContext
                .getClassDefinitionContainer()
                .getClassDefinitionByType(entry.getValue().getClass());
        dataOutput.writeLong(valueClassDefinition.getId());
        valueMarshaller.marshall(
            entry.getValue(), entry.getValue().getClass(), dataOutput, serializationContext);
      }
    }
  }