Skip to content

Commit

Permalink
Deprecate JsonUtils and remove implementation
Browse files Browse the repository at this point in the history
The code for the prettyPrint method comes from Stackoverflow, which has a license incompatible with our Apache licensed code. The Stackoverflow code has been removed and the method/class deprecated for removal. On the off chance this API is used, I did not delete the whole class in a patch release. Though it was always intended to be an internal class, it was not clearly marked as such.

The effect of this is that the `HttpSender.Request` class' `toString` will no longer be pretty-printed for JSON requests. This is an acceptable compromise for now rather than coming up with an original implementation for non-critical functionality.

See gh-2762
  • Loading branch information
shakuzen committed Aug 26, 2021
1 parent 1650aa1 commit 19e6bc6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,22 @@
*/
package io.micrometer.core.instrument.util;

/**
* @deprecated this will be removed and should not be used
*/
@Deprecated
public final class JsonUtils {

private JsonUtils() {
}

/**
* Based on https://stackoverflow.com/a/49564514/510017
*
* @param unformattedJsonString JSON string that has not been formatted at all.
* @return A best-effort at pretty printed JSON, even for malformed JSON.
* @return the input string unchanged
* @deprecated this will be removed and no longer does anything meaningful
*/
@Deprecated
public static String prettyPrint(String unformattedJsonString) {
StringBuilder sb = new StringBuilder();
int indentLevel = 0;
boolean inQuote = false;
for (char charFromUnformattedJson : unformattedJsonString.toCharArray()) {
switch (charFromUnformattedJson) {
case '"':
// switch the quoting status
inQuote = !inQuote;
sb.append(charFromUnformattedJson);
break;
case ' ':
// For space: ignore the space if it is not being quoted.
if (inQuote) {
sb.append(charFromUnformattedJson);
}
break;
case '{':
case '[':
// Starting a new block: increase the indent level
sb.append(charFromUnformattedJson);
indentLevel++;
appendIndentedNewLine(indentLevel, sb);
break;
case '}':
case ']':
// Ending a new block; decrese the indent level
indentLevel--;
appendIndentedNewLine(indentLevel, sb);
sb.append(charFromUnformattedJson);
break;
case ',':
// Ending a json item; create a new line after
sb.append(charFromUnformattedJson);
if (!inQuote) {
appendIndentedNewLine(indentLevel, sb);
}
break;
default:
sb.append(charFromUnformattedJson);
}
}
return sb.toString();
}

/**
* Print a new line with indention at the beginning of the new line.
*
* @param indentLevel
* @param stringBuilder
*/
private static void appendIndentedNewLine(int indentLevel, StringBuilder stringBuilder) {
stringBuilder.append("\n");
for (int i = 0; i < indentLevel; i++) {
// Assuming indention using 2 spaces
stringBuilder.append(" ");
}
return unformattedJsonString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.micrometer.core.ipc.http;

import io.micrometer.core.instrument.util.JsonUtils;
import io.micrometer.core.instrument.util.StringUtils;
import io.micrometer.core.lang.Nullable;

Expand Down Expand Up @@ -111,8 +110,6 @@ public String toString() {
.append(url.toString()).append("\n");
if (entity.length == 0) {
printed.append("<no request body>");
} else if ("application/json".equals(requestHeaders.get("Content-Type"))) {
printed.append(JsonUtils.prettyPrint(new String(entity)));
} else {
printed.append(new String(entity));
}
Expand Down

0 comments on commit 19e6bc6

Please sign in to comment.