Skip to content

Commit

Permalink
Internal
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 718943413
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 23, 2025
1 parent 0894265 commit 1d4b6ba
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public static Printer printer() {
/* preservingProtoFieldNames */ false,
/* omittingInsignificantWhitespace */ false,
/* printingEnumsAsInts */ false,
/* sortingMapKeys */ false);
/* sortingMapKeys */ false,
/* unsafeDisableCodepointsForHtmlSymbols */ false);
}

private enum ShouldPrintDefaults {
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -132,6 +135,7 @@ private Printer(
this.omittingInsignificantWhitespace = omittingInsignificantWhitespace;
this.printingEnumsAsInts = printingEnumsAsInts;
this.sortingMapKeys = sortingMapKeys;
this.unsafeDisableCodepointsForHtmlSymbols = unsafeDisableCodepointsForHtmlSymbols;
}

/**
Expand All @@ -153,7 +157,8 @@ public Printer usingTypeRegistry(TypeRegistry oldRegistry) {
preservingProtoFieldNames,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand All @@ -175,7 +180,8 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) {
preservingProtoFieldNames,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand Down Expand Up @@ -204,7 +210,8 @@ public Printer includingDefaultValueFields() {
preservingProtoFieldNames,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand Down Expand Up @@ -232,7 +239,8 @@ public Printer includingDefaultValueFields(Set<FieldDescriptor> fieldsToAlwaysOu
preservingProtoFieldNames,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand All @@ -253,7 +261,8 @@ public Printer alwaysPrintFieldsWithNoPresence() {
preservingProtoFieldNames,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand All @@ -270,7 +279,8 @@ public Printer printingEnumsAsInts() {
preservingProtoFieldNames,
omittingInsignificantWhitespace,
true,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

private void checkUnsetPrintingEnumsAsInts() {
Expand All @@ -294,7 +304,8 @@ public Printer preservingProtoFieldNames() {
true,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}


Expand Down Expand Up @@ -323,7 +334,8 @@ public Printer omittingInsignificantWhitespace() {
preservingProtoFieldNames,
true,
printingEnumsAsInts,
sortingMapKeys);
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand All @@ -346,7 +358,8 @@ public Printer sortingMapKeys() {
preservingProtoFieldNames,
omittingInsignificantWhitespace,
printingEnumsAsInts,
true);
true,
unsafeDisableCodepointsForHtmlSymbols);
}

/**
Expand All @@ -368,7 +381,8 @@ public void appendTo(MessageOrBuilder message, Appendable output) throws IOExcep
output,
omittingInsignificantWhitespace,
printingEnumsAsInts,
sortingMapKeys)
sortingMapKeys,
unsafeDisableCodepointsForHtmlSymbols)
.print(message);
}

Expand Down Expand Up @@ -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(
Expand All @@ -737,15 +753,19 @@ 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;
this.includingDefaultValueFields = includingDefaultValueFields;
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);
Expand Down

0 comments on commit 1d4b6ba

Please sign in to comment.