From 1d4b6bac6e5d5d889bf38ecce44afa93e3cc03e2 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 23 Jan 2025 11:19:00 -0800 Subject: [PATCH] Internal PiperOrigin-RevId: 718943413 --- .../com/google/protobuf/util/JsonFormat.java | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java index 844c91f9d13f6..5adefd6098e1f 100644 --- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java +++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java @@ -91,7 +91,8 @@ public static Printer printer() { /* preservingProtoFieldNames */ false, /* omittingInsignificantWhitespace */ false, /* printingEnumsAsInts */ false, - /* sortingMapKeys */ false); + /* sortingMapKeys */ false, + /* unsafeDisableCodepointsForHtmlSymbols */ false); } private enum ShouldPrintDefaults { @@ -114,6 +115,7 @@ public static class Printer { private final boolean omittingInsignificantWhitespace; private final boolean printingEnumsAsInts; private final boolean sortingMapKeys; + private final boolean unsafeDisableCodepointsForHtmlSymbols; private Printer( com.google.protobuf.TypeRegistry registry, @@ -123,7 +125,8 @@ private Printer( boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, - boolean sortingMapKeys) { + boolean sortingMapKeys, + boolean unsafeDisableCodepointsForHtmlSymbols) { this.registry = registry; this.oldRegistry = oldRegistry; this.shouldPrintDefaults = shouldOutputDefaults; @@ -132,6 +135,7 @@ private Printer( this.omittingInsignificantWhitespace = omittingInsignificantWhitespace; this.printingEnumsAsInts = printingEnumsAsInts; this.sortingMapKeys = sortingMapKeys; + this.unsafeDisableCodepointsForHtmlSymbols = unsafeDisableCodepointsForHtmlSymbols; } /** @@ -153,7 +157,8 @@ public Printer usingTypeRegistry(TypeRegistry oldRegistry) { preservingProtoFieldNames, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -175,7 +180,8 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) { preservingProtoFieldNames, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -204,7 +210,8 @@ public Printer includingDefaultValueFields() { preservingProtoFieldNames, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -232,7 +239,8 @@ public Printer includingDefaultValueFields(Set fieldsToAlwaysOu preservingProtoFieldNames, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -253,7 +261,8 @@ public Printer alwaysPrintFieldsWithNoPresence() { preservingProtoFieldNames, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -270,7 +279,8 @@ public Printer printingEnumsAsInts() { preservingProtoFieldNames, omittingInsignificantWhitespace, true, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } private void checkUnsetPrintingEnumsAsInts() { @@ -294,7 +304,8 @@ public Printer preservingProtoFieldNames() { true, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } @@ -323,7 +334,8 @@ public Printer omittingInsignificantWhitespace() { preservingProtoFieldNames, true, printingEnumsAsInts, - sortingMapKeys); + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -346,7 +358,8 @@ public Printer sortingMapKeys() { preservingProtoFieldNames, omittingInsignificantWhitespace, printingEnumsAsInts, - true); + true, + unsafeDisableCodepointsForHtmlSymbols); } /** @@ -368,7 +381,8 @@ public void appendTo(MessageOrBuilder message, Appendable output) throws IOExcep output, omittingInsignificantWhitespace, printingEnumsAsInts, - sortingMapKeys) + sortingMapKeys, + unsafeDisableCodepointsForHtmlSymbols) .print(message); } @@ -726,6 +740,8 @@ private static final class PrinterImpl { private static class GsonHolder { private static final Gson DEFAULT_GSON = new GsonBuilder().create(); + private static final Gson GSON_WITHOUT_HTML_ESCAPING = + new GsonBuilder().disableHtmlEscaping().create(); } PrinterImpl( @@ -737,7 +753,8 @@ private static class GsonHolder { Appendable jsonOutput, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, - boolean sortingMapKeys) { + boolean sortingMapKeys, + boolean unsafeDisableCodepointsForHtmlSymbols) { this.registry = registry; this.oldRegistry = oldRegistry; this.shouldPrintDefaults = shouldPrintDefaults; @@ -745,7 +762,10 @@ private static class GsonHolder { this.preservingProtoFieldNames = preservingProtoFieldNames; this.printingEnumsAsInts = printingEnumsAsInts; this.sortingMapKeys = sortingMapKeys; - this.gson = GsonHolder.DEFAULT_GSON; + this.gson = + unsafeDisableCodepointsForHtmlSymbols + ? GsonHolder.GSON_WITHOUT_HTML_ESCAPING + : GsonHolder.DEFAULT_GSON; // json format related properties, determined by printerType if (omittingInsignificantWhitespace) { this.generator = new CompactTextGenerator(jsonOutput);