Merge branch '3.4.x'

Closes gh-45211
This commit is contained in:
Andy Wilkinson
2025-04-16 11:37:00 +01:00
2 changed files with 16 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.json;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
@@ -114,6 +115,9 @@ class JsonValueWriter {
throw new UncheckedIOException(ex);
}
}
else if (value instanceof Path p) {
writeString(p.toString());
}
else if (value instanceof Iterable<?> iterable) {
writeArray(iterable::forEach);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.boot.json;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -240,6 +241,16 @@ class JsonValueWriterTests {
.isThrownBy(() -> valueWriter.end(Series.ARRAY)));
}
@Test // gh-44502
void writeJavaNioPathWhenSingleElementShouldBeSerializedAsString() {
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("a")))).isEqualTo(quoted("a"));
}
@Test // gh-44502
void writeJavaNioPathShouldBeSerializedAsString() {
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("a/b/c")))).isEqualTo(quoted("a\\/b\\/c"));
}
private <V> String write(V value) {
return doWrite((valueWriter) -> valueWriter.write(value));
}