File tree Expand file tree Collapse file tree 3 files changed +61
-2
lines changed
core/src/main/java/com/microsoft/applicationinsights Expand file tree Collapse file tree 3 files changed +61
-2
lines changed Original file line number Diff line number Diff line change 11# CHANGELOG
22
33## Version 1.0.10
4- - Fixed Request Telemetry Sending bug with new schema
4+ - Fix issue of sending custom event.
5+ - Method ` sanitizeStringForJSON ` takes string input and converts it to JSON friendly string.
6+ - Class ` com.microsoft.applicationinsights.common.SanitizationUtils ` has methods to sanitize string for JSON.
7+ - Fixed Request Telemetry Sending bug with new schema.
58- Schema updated to the latest version. Changes in internal namespace ` core/src/main/java/com/microsoft/applicationinsights/internal/schemav2 ` .
69- Class ` SendableData ` in internal namespace deleted.
710- Class ` com.microsoft.applicationinsights.telemetry.BaseSampleSourceTelemetry ` takes generic class qualifier ` Domain ` instead of ` SendableData ` .
Original file line number Diff line number Diff line change 1+ package com .microsoft .applicationinsights .common ;
2+
3+ import java .text .StringCharacterIterator ;
4+
5+ /**
6+ * Created by dhdoshi on 09/10/2017
7+ *
8+ * This class provides utility functions to sanitize strings
9+ * for various formats. Currently it supports JSON sanitization
10+ */
11+ public final class SanitizationUtils {
12+
13+ /**
14+ * This method appends escape characters to input String to prevent
15+ * JSON Sanitization failure
16+ * @param text
17+ * @return Sanitized String suitable for JSON
18+ */
19+ public static String sanitizeStringForJSON (String text ) {
20+
21+ final StringBuilder result = new StringBuilder ();
22+ StringCharacterIterator iterator = new StringCharacterIterator (text );
23+ for (char curr = iterator .current (); curr != iterator .DONE ; curr = iterator .next ()) {
24+ if ( curr == '\"' ){
25+ result .append ("\\ \" " );
26+ }
27+ else if (curr == '\\' ){
28+ result .append ("\\ \\ " );
29+ }
30+ else if (curr == '/' ){
31+ result .append ("\\ /" );
32+ }
33+ else if (curr == '\b' ){
34+ result .append ("\\ b" );
35+ }
36+ else if (curr == '\f' ){
37+ result .append ("\\ f" );
38+ }
39+ else if (curr == '\n' ){
40+ result .append ("\\ n" );
41+ }
42+ else if (curr == '\r' ){
43+ result .append ("\\ r" );
44+ }
45+ else if (curr == '\t' ){
46+ result .append ("\\ t" );
47+ }
48+ else {
49+ result .append (curr );
50+ }
51+ }
52+ return result .toString ();
53+ }
54+ }
Original file line number Diff line number Diff line change 2929
3030import com .google .common .base .Strings ;
3131
32+ import com .microsoft .applicationinsights .common .SanitizationUtils ;
3233import com .microsoft .applicationinsights .internal .schemav2 .*;
3334import com .microsoft .applicationinsights .internal .util .LocalStringsUtils ;
3435
@@ -273,8 +274,9 @@ private <T> void write(T item) throws IOException {
273274 {
274275 out .write (String .valueOf (item ));
275276 } else {
277+ String sanitizedItem = SanitizationUtils .sanitizeStringForJSON (String .valueOf (item ));
276278 out .write (JSON_COMMA );
277- out .write (String . valueOf ( item ) );
279+ out .write (sanitizedItem );
278280 out .write (JSON_COMMA );
279281 }
280282 }
You can’t perform that action at this time.
0 commit comments