code: public Optional<String> toJsonWithLengthLimit(T instance, int lengthLimit)
      {
          try (StringWriter stringWriter = new StringWriter();
                  LengthLimitedWriter lengthLimitedWriter = new LengthLimitedWriter(stringWriter, lengthLimit)) {
              mapper.writeValue(lengthLimitedWriter, instance);
              return Optional.of(stringWriter.getBuffer().toString());
          }
          catch (LengthLimitExceededException e) {
              return Optional.empty();
          }
          catch (IOException e) {
              throw new IllegalArgumentException(format("%s could not be converted to JSON", instance.getClass().getName()), e);
          }
      }
docs: Converts the specified instance to optional json string with a length limit. Returns Optional.empty() if length limit is exceeded.
  
  @param instance the instance to convert to json
  @param lengthLimit the maximum length of the serialized string in characters
  @return json string
  @throws IllegalArgumentException if the specified instance can not be converted to json