Skip to content

Commit d46d385

Browse files
authored
fix: Output file JSON without a leading space before : (#21005)
This is compatible with the old elemental json version and with how browsers output indented json
1 parent 4128447 commit d46d385

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

flow-server/src/main/java/com/vaadin/flow/internal/JacksonUtils.java

+21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
import com.fasterxml.jackson.core.JsonProcessingException;
3636
import com.fasterxml.jackson.core.type.TypeReference;
37+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
38+
import com.fasterxml.jackson.core.util.Separators;
39+
import com.fasterxml.jackson.core.util.Separators.Spacing;
3740
import com.fasterxml.jackson.databind.JsonNode;
3841
import com.fasterxml.jackson.databind.ObjectMapper;
3942
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -489,4 +492,22 @@ public static <T> T readValue(ObjectNode jsonValue,
489492
public static ObjectNode writeValue(Object object) {
490493
return objectMapper.valueToTree(object);
491494
}
495+
496+
/**
497+
* Converts the given node into JSON suitable for writing into a file such
498+
* as {@literal package.json}.
499+
*
500+
* @param node
501+
* the node to convert
502+
* @return the JSON string
503+
* @throws JsonProcessingException
504+
* if the node cannot be converted
505+
*/
506+
public static String toFileJson(JsonNode node)
507+
throws JsonProcessingException {
508+
DefaultPrettyPrinter filePrinter = new DefaultPrettyPrinter(
509+
Separators.createDefaultInstance()
510+
.withObjectFieldValueSpacing(Spacing.AFTER));
511+
return objectMapper.writer().with(filePrinter).writeValueAsString(node);
512+
}
492513
}

flow-server/src/main/java/com/vaadin/flow/server/frontend/NodeUpdater.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ String writePackageFile(JsonNode packageJson) throws IOException {
521521

522522
String writePackageFile(JsonNode json, File packageFile)
523523
throws IOException {
524-
String content = json.toPrettyString() + "\n";
524+
String content = JacksonUtils.toFileJson(json);
525525
if (packageFile.exists() || options.isFrontendHotdeploy()
526526
|| options.isBundleBuild()) {
527527
log().debug("writing file {}.", packageFile.getAbsolutePath());

flow-server/src/test/java/com/vaadin/flow/internal/JacksonUtilsTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.stream.DoubleStream;
3131
import java.util.stream.Stream;
3232

33+
import com.fasterxml.jackson.core.JsonProcessingException;
3334
import com.fasterxml.jackson.databind.JsonNode;
3435
import com.fasterxml.jackson.databind.ObjectMapper;
3536
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -39,7 +40,7 @@
3940
import org.junit.Test;
4041

4142
public class JacksonUtilsTest {
42-
ObjectMapper mapper = new ObjectMapper();
43+
ObjectMapper mapper = JacksonUtils.getMapper();
4344

4445
@Test
4546
public void testEquals() {
@@ -472,4 +473,17 @@ public void testReadObject() {
472473
Assert.assertTrue(person.canSwim);
473474
}
474475

476+
@Test
477+
public void toFileJson() throws JsonProcessingException {
478+
ObjectNode json = JacksonUtils.beanToJson(new ParentBean());
479+
Assert.assertEquals("""
480+
{
481+
"parentValue": "parent",
482+
"child": {
483+
"childValue": "child"
484+
}
485+
}""", JacksonUtils.toFileJson(json));
486+
487+
}
488+
475489
}

0 commit comments

Comments
 (0)