Skip to content

Commit 9664504

Browse files
hhellyerrobbinspg
authored andcommitted
Escape environment strings so they are valid in JSON. (#20)
1 parent 458fd3d commit 9664504

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

java/src/com/ibm/javametrics/dataproviders/DataProviderManager.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class DataProviderManager {
3333

3434
private ScheduledExecutorService exec;
3535

36+
private static String escapeStringForJSON(String str) {
37+
return str.replace("\\", "\\\\").replace("\"", "\\\"");
38+
}
39+
3640
/**
3741
* Create a JavametricsMBeanConnector
3842
*/
@@ -55,13 +59,16 @@ public void emitPersistentData() {
5559
private void emitEnvironmentData() {
5660
String paramFormat = "{\"Parameter\":\"%s\",\"Value\":\"%s\"}";
5761
StringBuilder message = new StringBuilder("[");
58-
message.append(String.format(paramFormat, "Hostname", EnvironmentDataProvider.getHostname()));
62+
message.append(String.format(paramFormat, "Hostname",
63+
escapeStringForJSON(EnvironmentDataProvider.getHostname())));
5964
message.append(',');
60-
message.append(String.format(paramFormat, "OS Architecture", EnvironmentDataProvider.getArchitecture()));
65+
message.append(String.format(paramFormat, "OS Architecture",
66+
escapeStringForJSON(EnvironmentDataProvider.getArchitecture())));
6167
message.append(',');
6268
message.append(String.format(paramFormat, "Number of Processors", EnvironmentDataProvider.getCPUCount()));
6369
message.append(',');
64-
message.append(String.format(paramFormat, "Command Line", EnvironmentDataProvider.getCommandLine()));
70+
message.append(String.format(paramFormat, "Command Line",
71+
escapeStringForJSON(EnvironmentDataProvider.getCommandLine())));
6572
message.append("]");
6673
Javametrics.getInstance().sendJSON(ENV_TOPIC, message.toString());
6774
}

0 commit comments

Comments
 (0)