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


##########
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 {
+    JsonGenerator generator = FACTORY.createGenerator(writer);
+    generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
+    if (pretty) {
+      generator.useDefaultPrettyPrinter();
+    }
+
+    return new Jackson2JsonWriter(generator);
+  }
+
+  /**
+   * Parses JSON from a stream. The stream is not closed.
+   *
+   * <p>Malformed or empty input fails with an {@link IOException}.
+   *
+   * @param stream a stream of UTF-8 encoded JSON
+   * @return the parsed {@link JsonTree}
+   */
+  public static JsonTree parseJson(InputStream stream) throws IOException {
+    JsonNode node = 
MAPPER.reader().without(JsonParser.Feature.AUTO_CLOSE_SOURCE).readTree(stream);
+    if (node == null || node.isMissingNode()) {
+      throw new IOException("No content to map due to end-of-input");
+    }
+
+    return Jackson2JsonTree.of(node);
+  }
+
+  /**
+   * Parses a JSON string.
+   *
+   * <p>Malformed or empty input fails with an {@link UncheckedIOException}.
+   *
+   * @param json a JSON string
+   * @return the parsed {@link JsonTree}
+   */
+  public static JsonTree parseJson(String json) {
+    JsonNode node;
+    try {
+      node = MAPPER.readTree(json);
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+
+    if (node == null || node.isMissingNode()) {
+      throw new UncheckedIOException(
+          new IOException("No content to map due to end-of-input: " + json));
+    }
+
+    return Jackson2JsonTree.of(node);
+  }
+
+  // 
---------------------------------------------------------------------------
+  // Jackson 2 support, deprecated and to be removed
+  // 
---------------------------------------------------------------------------
+
+  /**
+   * Returns a view of a Jackson 2 node that can be passed to methods that 
take a {@link JsonTree}.
+   *
+   * @deprecated since 1.13.0, will be removed in 1.14.0; only used by the 
deprecated Jackson 2
+   *     overloads, which take their place.
+   */
+  @Deprecated
+  public static JsonTree wrap(JsonNode node) {

Review Comment:
   This method and the 2 following ones were introduced in this PR and 
deprecated at the same time. This is required unfortunately; these methods 
should be removed when all the other deprecated Jackson 2 methods will be 
removed.



##########
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 {
+    JsonGenerator generator = FACTORY.createGenerator(writer);
+    generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
+    if (pretty) {
+      generator.useDefaultPrettyPrinter();
+    }
+
+    return new Jackson2JsonWriter(generator);
+  }
+
+  /**
+   * Parses JSON from a stream. The stream is not closed.
+   *
+   * <p>Malformed or empty input fails with an {@link IOException}.
+   *
+   * @param stream a stream of UTF-8 encoded JSON
+   * @return the parsed {@link JsonTree}
+   */
+  public static JsonTree parseJson(InputStream stream) throws IOException {

Review Comment:
   This method and the following one are the impl-agnostic versions of the 
legacy `org.apache.iceberg.util.JsonUtil#parse` method.



-- 
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