MCPcopy Index your code
hub / github.com/OpenTSDB/opentsdb / escapeJson

Method escapeJson

src/tsd/HttpQuery.java:478–523  ·  view source on GitHub ↗

Escapes a string appropriately to be a valid in JSON. Valid JSON strings are defined in RFC 4627, Section 2.5. @param s The string to escape, which is assumed to be in . @param buf The buffer into which to write the escaped string.

(final String s, final StringBuilder buf)

Source from the content-addressed store, hash-verified

476 * @param buf The buffer into which to write the escaped string.
477 */
478 static void escapeJson(final String s, final StringBuilder buf) {
479 final int length = s.length();
480 int extra = 0;
481 // First count how many extra chars we'll need, if any.
482 for (int i = 0; i < length; i++) {
483 final char c = s.charAt(i);
484 switch (c) {
485 case '"':
486 case '\\':
487 case '\b':
488 case '\f':
489 case '\n':
490 case '\r':
491 case '\t':
492 extra++;
493 continue;
494 }
495 if (c < 0x001F) {
496 extra += 4;
497 }
498 }
499 if (extra == 0) {
500 buf.append(s); // Nothing to escape.
501 return;
502 }
503 buf.ensureCapacity(buf.length() + length + extra);
504 for (int i = 0; i < length; i++) {
505 final char c = s.charAt(i);
506 switch (c) {
507 case '"': buf.append('\\').append('"'); continue;
508 case '\\': buf.append('\\').append('\\'); continue;
509 case '\b': buf.append('\\').append('b'); continue;
510 case '\f': buf.append('\\').append('f'); continue;
511 case '\n': buf.append('\\').append('n'); continue;
512 case '\r': buf.append('\\').append('r'); continue;
513 case '\t': buf.append('\\').append('t'); continue;
514 }
515 if (c < 0x001F) {
516 buf.append('\\').append('u').append('0').append('0')
517 .append((char) Const.HEX[(c >>> 4) & 0x0F])
518 .append((char) Const.HEX[c & 0x0F]);
519 } else {
520 buf.append(c);
521 }
522 }
523 }
524
525 /**
526 * Sends data in an HTTP "200 OK" reply to the client.

Callers 5

escapeJsonMethod · 0.95
formatErrorV1Method · 0.95
internalErrorMethod · 0.95
badRequestMethod · 0.95
stringifyMethod · 0.95

Calls 2

ensureCapacityMethod · 0.80
appendMethod · 0.45

Tested by 1

escapeJsonMethod · 0.76