java.lang.Object
com.darkcollective.relix.json.JsonStrings

public final class JsonStrings extends Object
JSON string-literal escaping — the single source of truth for turning a Java 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 Details

    • quote

      public static String quote(String value)
      Returns value as 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

      public static void appendQuoted(StringBuilder out, String value)
      Appends value to out as a quoted, escaped JSON string literal. Equivalent to quote(String) without the intermediate string, for callers already building into a buffer.
      Parameters:
      out - the buffer to append to; must not be null
      value - the raw string; must not be null