SpanParser { public static String escapeJson(String orig) { if (orig == null) { return null; } StringBuilder sb = null; for (int i = 0; i < orig.length(); i++) { char nextChar = orig.charAt(i); if (needsJsonEscaping(nextChar)) { if (sb == null) { sb = new StringBuilder(orig.length() + 16); sb.append(orig, 0, i); } String escapedValue = getJsonEscapedValueForChar(nextChar); if (escapedValue == null) { logger.warn( "Somehow found a character that needs escaping, but getJsonEscapedValueForChar(char) " + "came back null. This shouldn't happen! char_int_value={}", (int)nextChar ); sb.append(nextChar); } else { sb.append(escapedValue); } } else if (sb != null) { sb.append(nextChar); } } if (sb == null) { return orig; } return sb.toString(); } protected  SpanParser(); static String convertSpanToJSON(Span span); static Span fromJSON(String json); static String convertSpanToKeyValueFormat(Span span); static Span fromKeyValueString(String keyValueStr); static String escapeJson(String orig); static String unescapeJson(String escaped); static final String TRACE_ID_FIELD; static final String PARENT_SPAN_ID_FIELD; static final String SPAN_ID_FIELD; static final String SPAN_NAME_FIELD; static final String SAMPLEABLE_FIELD; static final String USER_ID_FIELD; static final String SPAN_PURPOSE_FIELD; static final String START_TIME_EPOCH_MICROS_FIELD; static final String DURATION_NANOS_FIELD; static final String TAGS_FIELD; static final String ANNOTATIONS_LIST_FIELD; static final String ANNOTATION_SUBOBJECT_TIMESTAMP_FIELD; static final String ANNOTATION_SUBOBJECT_VALUE_FIELD; static final String KEY_VALUE_TAG_PREFIX; static final String KEY_VALUE_TIMESTAMPED_ANNOTATION_PREFIX; }
@UseDataProvider("escapeJsonScenarioDataProvider") @Test public void escapeJson_escapes_characters_as_expected(EscapeJsonScenario scenario) throws JsonProcessingException { String jacksonSerialized = objectMapper.writeValueAsString( Collections.singletonMap("foo", scenario.strToEscape) ); String escaped = SpanParser.escapeJson(scenario.strToEscape); String escapedWithJacksonObjBoilerplate = "{\"foo\":\"" + escaped + "\"}"; assertThat(escapedWithJacksonObjBoilerplate).isEqualTo(jacksonSerialized); assertThat(escaped).isEqualTo(scenario.expectedEscaped); }
@Test public void escapeJson_returns_null_if_passed_null() { assertThat(SpanParser.escapeJson(null)).isNull(); }
@Test public void escapeJson_returns_same_instance_if_no_chars_need_escaping() { String orig = UUID.randomUUID().toString(); assertThat(SpanParser.escapeJson(orig)).isSameAs(orig); }