title: Java or Scala. How to convert characters like \x22 into String
body: <p>I have a string that looks like this:</p>
  
  <pre><code>{\x22documentReferer\x22:\x22http:\x5C/\x5C/pikabu.ru\x5C/freshitems.php\x22}
  </code></pre>
  
  <p>How could I convert this into a readable JSON?</p>
  
  <p>I've found different slow solutions like <a href="https://stackoverflow.com/a/45969033/5686654" title="here with regEx">here with regEx</a></p>
  
  <p>Have already tried:</p>
  
  <pre><code>URL.decode
  StringEscapeUtils
  JSON.parse // from different libraries 
  </code></pre>
  
  <p>For example python has simple solution like decode from <code>'string_escape'</code></p>
  
  <p>Linked possible duplicate applies to Python, and my question is about Java or Scala</p>
  
  <p>Working but also very slow solution I'm using now is from <a href="https://stackoverflow.com/a/4298836/5686654">here</a>:</p>
  
  <pre><code> def unescape(oldstr: String): String = {
  val newstr = new StringBuilder(oldstr.length)
  var saw_backslash = false
  var i = 0
  while (i &lt; oldstr.length) {
    {
      val cp = oldstr.codePointAt(i)
      if (!saw_backslash) {
        if (cp == '\\') saw_backslash = true
        else newstr.append(cp.toChar)
      } else {
        if (cp == '\\') {
          saw_backslash = false
          newstr.append('\\')
          newstr.append('\\')
        } else {
          if (cp == 'x') {
            if (i + 2 &gt; oldstr.length) die("string too short for \\x escape")
            i += 1
            var value = 0
            try
              value = Integer.parseInt(oldstr.substring(i, i + 2), 16)
            catch {
              case nfe: NumberFormatException =&gt;
                die("invalid hex value for \\x escape")
            }
            newstr.append(value.toChar)
            i += 1
          }
          else {
            newstr.append('\\')
            newstr.append(cp.toChar)
          }
          saw_backslash = false
        }
      }
    }
    i += 1
  }
      if (saw_backslash) newstr.append('\\')
      newstr.toString
    }
  
  private def die(msg: String) {
    throw new IllegalArgumentException(msg)
  }
  </code></pre>