adutra commented on code in PR #19240:
URL: https://github.com/apache/iceberg/pull/19240#discussion_r4758009074


##########
core/src/main/java/org/apache/iceberg/util/JsonUtil.java:
##########
@@ -51,26 +54,155 @@ private JsonUtil() {}
           .build();
   private static final ObjectMapper MAPPER = new ObjectMapper(FACTORY);
 
+  /**
+   * Writes JSON with the given function and returns it as a string.
+   *
+   * @param toJson a function that writes JSON to a {@link JsonWriter}
+   * @param pretty whether to pretty-print JSON for readability
+   * @return a JSON string
+   */
+  public static String generateJson(IOConsumer<JsonWriter> toJson, boolean 
pretty) {
+    StringWriter out = new StringWriter();
+    try (JsonWriter writer = newJsonWriter(out, pretty)) {
+      toJson.accept(writer);
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+
+    return out.toString();
+  }
+
+  /**
+   * Creates a {@link JsonWriter} that writes JSON to a {@link Writer}.
+   *
+   * <p>Closing the returned writer flushes it, but does not close the {@link 
Writer}.
+   *
+   * @param writer a writer that receives the JSON
+   * @param pretty whether to pretty-print JSON for readability
+   * @return a writer that must be closed by the caller
+   */
+  public static JsonWriter newJsonWriter(Writer writer, boolean pretty) throws 
IOException {

Review Comment:
   This replaces the legacy `org.apache.iceberg.util.JsonUtil#parse` method. It 
only returns the writer to use though, it doesn't invoke the writer; that 
became the caller's responsibility.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to