Skip to content

Commit 1d4b6ba

Browse files
Internal
PiperOrigin-RevId: 718943413
1 parent 0894265 commit 1d4b6ba

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

java/util/src/main/java/com/google/protobuf/util/JsonFormat.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public static Printer printer() {
9191
/* preservingProtoFieldNames */ false,
9292
/* omittingInsignificantWhitespace */ false,
9393
/* printingEnumsAsInts */ false,
94-
/* sortingMapKeys */ false);
94+
/* sortingMapKeys */ false,
95+
/* unsafeDisableCodepointsForHtmlSymbols */ false);
9596
}
9697

9798
private enum ShouldPrintDefaults {
@@ -114,6 +115,7 @@ public static class Printer {
114115
private final boolean omittingInsignificantWhitespace;
115116
private final boolean printingEnumsAsInts;
116117
private final boolean sortingMapKeys;
118+
private final boolean unsafeDisableCodepointsForHtmlSymbols;
117119

118120
private Printer(
119121
com.google.protobuf.TypeRegistry registry,
@@ -123,7 +125,8 @@ private Printer(
123125
boolean preservingProtoFieldNames,
124126
boolean omittingInsignificantWhitespace,
125127
boolean printingEnumsAsInts,
126-
boolean sortingMapKeys) {
128+
boolean sortingMapKeys,
129+
boolean unsafeDisableCodepointsForHtmlSymbols) {
127130
this.registry = registry;
128131
this.oldRegistry = oldRegistry;
129132
this.shouldPrintDefaults = shouldOutputDefaults;
@@ -132,6 +135,7 @@ private Printer(
132135
this.omittingInsignificantWhitespace = omittingInsignificantWhitespace;
133136
this.printingEnumsAsInts = printingEnumsAsInts;
134137
this.sortingMapKeys = sortingMapKeys;
138+
this.unsafeDisableCodepointsForHtmlSymbols = unsafeDisableCodepointsForHtmlSymbols;
135139
}
136140

137141
/**
@@ -153,7 +157,8 @@ public Printer usingTypeRegistry(TypeRegistry oldRegistry) {
153157
preservingProtoFieldNames,
154158
omittingInsignificantWhitespace,
155159
printingEnumsAsInts,
156-
sortingMapKeys);
160+
sortingMapKeys,
161+
unsafeDisableCodepointsForHtmlSymbols);
157162
}
158163

159164
/**
@@ -175,7 +180,8 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) {
175180
preservingProtoFieldNames,
176181
omittingInsignificantWhitespace,
177182
printingEnumsAsInts,
178-
sortingMapKeys);
183+
sortingMapKeys,
184+
unsafeDisableCodepointsForHtmlSymbols);
179185
}
180186

181187
/**
@@ -204,7 +210,8 @@ public Printer includingDefaultValueFields() {
204210
preservingProtoFieldNames,
205211
omittingInsignificantWhitespace,
206212
printingEnumsAsInts,
207-
sortingMapKeys);
213+
sortingMapKeys,
214+
unsafeDisableCodepointsForHtmlSymbols);
208215
}
209216

210217
/**
@@ -232,7 +239,8 @@ public Printer includingDefaultValueFields(Set<FieldDescriptor> fieldsToAlwaysOu
232239
preservingProtoFieldNames,
233240
omittingInsignificantWhitespace,
234241
printingEnumsAsInts,
235-
sortingMapKeys);
242+
sortingMapKeys,
243+
unsafeDisableCodepointsForHtmlSymbols);
236244
}
237245

238246
/**
@@ -253,7 +261,8 @@ public Printer alwaysPrintFieldsWithNoPresence() {
253261
preservingProtoFieldNames,
254262
omittingInsignificantWhitespace,
255263
printingEnumsAsInts,
256-
sortingMapKeys);
264+
sortingMapKeys,
265+
unsafeDisableCodepointsForHtmlSymbols);
257266
}
258267

259268
/**
@@ -270,7 +279,8 @@ public Printer printingEnumsAsInts() {
270279
preservingProtoFieldNames,
271280
omittingInsignificantWhitespace,
272281
true,
273-
sortingMapKeys);
282+
sortingMapKeys,
283+
unsafeDisableCodepointsForHtmlSymbols);
274284
}
275285

276286
private void checkUnsetPrintingEnumsAsInts() {
@@ -294,7 +304,8 @@ public Printer preservingProtoFieldNames() {
294304
true,
295305
omittingInsignificantWhitespace,
296306
printingEnumsAsInts,
297-
sortingMapKeys);
307+
sortingMapKeys,
308+
unsafeDisableCodepointsForHtmlSymbols);
298309
}
299310

300311

@@ -323,7 +334,8 @@ public Printer omittingInsignificantWhitespace() {
323334
preservingProtoFieldNames,
324335
true,
325336
printingEnumsAsInts,
326-
sortingMapKeys);
337+
sortingMapKeys,
338+
unsafeDisableCodepointsForHtmlSymbols);
327339
}
328340

329341
/**
@@ -346,7 +358,8 @@ public Printer sortingMapKeys() {
346358
preservingProtoFieldNames,
347359
omittingInsignificantWhitespace,
348360
printingEnumsAsInts,
349-
true);
361+
true,
362+
unsafeDisableCodepointsForHtmlSymbols);
350363
}
351364

352365
/**
@@ -368,7 +381,8 @@ public void appendTo(MessageOrBuilder message, Appendable output) throws IOExcep
368381
output,
369382
omittingInsignificantWhitespace,
370383
printingEnumsAsInts,
371-
sortingMapKeys)
384+
sortingMapKeys,
385+
unsafeDisableCodepointsForHtmlSymbols)
372386
.print(message);
373387
}
374388

@@ -726,6 +740,8 @@ private static final class PrinterImpl {
726740

727741
private static class GsonHolder {
728742
private static final Gson DEFAULT_GSON = new GsonBuilder().create();
743+
private static final Gson GSON_WITHOUT_HTML_ESCAPING =
744+
new GsonBuilder().disableHtmlEscaping().create();
729745
}
730746

731747
PrinterImpl(
@@ -737,15 +753,19 @@ private static class GsonHolder {
737753
Appendable jsonOutput,
738754
boolean omittingInsignificantWhitespace,
739755
boolean printingEnumsAsInts,
740-
boolean sortingMapKeys) {
756+
boolean sortingMapKeys,
757+
boolean unsafeDisableCodepointsForHtmlSymbols) {
741758
this.registry = registry;
742759
this.oldRegistry = oldRegistry;
743760
this.shouldPrintDefaults = shouldPrintDefaults;
744761
this.includingDefaultValueFields = includingDefaultValueFields;
745762
this.preservingProtoFieldNames = preservingProtoFieldNames;
746763
this.printingEnumsAsInts = printingEnumsAsInts;
747764
this.sortingMapKeys = sortingMapKeys;
748-
this.gson = GsonHolder.DEFAULT_GSON;
765+
this.gson =
766+
unsafeDisableCodepointsForHtmlSymbols
767+
? GsonHolder.GSON_WITHOUT_HTML_ESCAPING
768+
: GsonHolder.DEFAULT_GSON;
749769
// json format related properties, determined by printerType
750770
if (omittingInsignificantWhitespace) {
751771
this.generator = new CompactTextGenerator(jsonOutput);

0 commit comments

Comments
 (0)