This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 1d7c1ff97f GROOVY-11926: Improve consistency of YAML functionality
(clean up resource closing)
1d7c1ff97f is described below
commit 1d7c1ff97f68077747096391fbbe9ad8a3026425
Author: Paul King <[email protected]>
AuthorDate: Sun Apr 12 22:46:42 2026 +1000
GROOVY-11926: Improve consistency of YAML functionality (clean up resource
closing)
---
.../groovy-yaml/src/main/java/groovy/yaml/YamlSlurper.java | 14 ++++++++------
.../java/org/apache/groovy/yaml/util/YamlConverter.java | 8 ++++----
2 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/subprojects/groovy-yaml/src/main/java/groovy/yaml/YamlSlurper.java
b/subprojects/groovy-yaml/src/main/java/groovy/yaml/YamlSlurper.java
index 6beeb4d3d0..d8e0f1c0c1 100644
--- a/subprojects/groovy-yaml/src/main/java/groovy/yaml/YamlSlurper.java
+++ b/subprojects/groovy-yaml/src/main/java/groovy/yaml/YamlSlurper.java
@@ -21,7 +21,6 @@ package groovy.yaml;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import groovy.json.JsonSlurper;
-import groovy.yaml.YamlRuntimeException;
import org.apache.groovy.yaml.util.YamlConverter;
import java.io.File;
@@ -92,8 +91,9 @@ public class YamlSlurper {
* @return the root node of the parsed tree of Nodes
*/
public Object parse(Path path) throws IOException {
- // note: convert to an input stream to allow the support of foreign
file objects
- return parse(Files.newInputStream(path));
+ try (InputStream stream = Files.newInputStream(path)) {
+ return parse(new InputStreamReader(stream));
+ }
}
/**
@@ -121,8 +121,8 @@ public class YamlSlurper {
* @since 6.0.0
*/
public <T> T parseAs(Class<T> type, Reader reader) {
- try (Reader r = reader) {
- return new ObjectMapper(new YAMLFactory()).readValue(r, type);
+ try {
+ return new ObjectMapper(new YAMLFactory()).readValue(reader, type);
} catch (IOException e) {
throw new YamlRuntimeException(e);
}
@@ -164,6 +164,8 @@ public class YamlSlurper {
* @since 6.0.0
*/
public <T> T parseAs(Class<T> type, Path path) throws IOException {
- return parseAs(type, Files.newInputStream(path));
+ try (InputStream stream = Files.newInputStream(path)) {
+ return parseAs(type, new InputStreamReader(stream));
+ }
}
}
diff --git
a/subprojects/groovy-yaml/src/main/java/org/apache/groovy/yaml/util/YamlConverter.java
b/subprojects/groovy-yaml/src/main/java/org/apache/groovy/yaml/util/YamlConverter.java
index f99833cccf..8a6f457619 100644
---
a/subprojects/groovy-yaml/src/main/java/org/apache/groovy/yaml/util/YamlConverter.java
+++
b/subprojects/groovy-yaml/src/main/java/org/apache/groovy/yaml/util/YamlConverter.java
@@ -39,10 +39,10 @@ public final class YamlConverter {
* @return the text of json
*/
public static String convertYamlToJson(Reader yamlReader) {
- try (Reader reader = yamlReader) {
+ try {
List<Object> resultList =
new ObjectMapper()
- .readValues(new
YAMLFactory().createParser(reader), Object.class)
+ .readValues(new
YAMLFactory().createParser(yamlReader), Object.class)
.readAll();
Object yaml = 1 == resultList.size() ? resultList.get(0) :
resultList;
return new ObjectMapper().writeValueAsString(yaml);
@@ -57,8 +57,8 @@ public final class YamlConverter {
* @return the text of yaml
*/
public static String convertJsonToYaml(Reader jsonReader) {
- try (Reader reader = jsonReader) {
- JsonNode json = new ObjectMapper().readTree(reader);
+ try {
+ JsonNode json = new ObjectMapper().readTree(jsonReader);
return new YAMLMapper().writeValueAsString(json);
} catch (IOException e) {