code: public static int serialize(OutputStream stream, Object obj) {
          ObjectMapper mapper = createMapperWithJaxbAnnotationInspector();
  
          try (DataOutputStream data = new DataOutputStream(stream);
               OutputStreamWriter writer = new OutputStreamWriter(data, StandardCharsets.UTF_8)) {
              mapper.writerWithDefaultPrettyPrinter().writeValue(writer, obj);
              return data.size();
          } catch (IOException e) {
              throw new ReportGenerationException(e);
          }
      }
docs: Serialize specified object to directory with specified name. Given output stream will be closed.
  
  @param obj object to serialize
  @return number of bytes written to directory