String into a quoted, correctly-escaped JSON string.
This exists because escaping is needed in two shapes: JsonWriter
builds structured documents, but several callers emit a fragment of JSON by
hand (a MongoDB pipeline stage, a JSON-Lines log record, a --format
json result row) and need only the string-literal primitive. Before this was
extracted, five such callers each carried their own copy of the escape switch;
they agreed by luck rather than construction. JsonWriter delegates
here too, so there is exactly one implementation.
The escaping follows RFC 8259: the two mandatory escapes (" and
\), the short forms for the five named control characters, and
\ u00XX for every other character below 0x20. Characters at or
above 0x20 — including non-ASCII — are emitted as-is, which is valid
in a UTF-8 document.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendQuoted(StringBuilder out, String value) Appendsvaluetooutas a quoted, escaped JSON string literal.static StringReturnsvalueas a quoted, escaped JSON string literal, including the surrounding double quotes.
-
Method Details
-
quote
Returnsvalueas a quoted, escaped JSON string literal, including the surrounding double quotes.- Parameters:
value- the raw string; must not be null- Returns:
- the JSON string literal, e.g.
a"b→"a\"b"
-
appendQuoted
Appendsvaluetooutas a quoted, escaped JSON string literal. Equivalent toquote(String)without the intermediate string, for callers already building into a buffer.- Parameters:
out- the buffer to append to; must not be nullvalue- the raw string; must not be null
-