Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To add to the other answer, <code>\u</code> escapes have an interesting property in Java where they are actually <em>preprocessed</em> by the compiler (<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.3">JLS §3.3</a>).</p> <p>By this, I mean that <code>\u</code> characters are replaced in the source file <em>before</em> going to the compiler. Therefore, your code:</p> <pre><code>public String expectedJSONWithHTMLValues(){ return "{" + "\"someHtml\":{" + "\"html\":\"\u003Chtml\u003EHTML\u0026CSS\u003C/html\u003E\"}}"; } </code></pre> <p>Is being preprocessed to:</p> <pre><code>public String expectedJSONWithHTMLValues(){ return "{" + "\"someHtml\":{" + "\"html\":\"&lt;html&gt;HTML&amp;CSS&lt;/html&gt;\"}}"; } </code></pre> <p>And <em>that</em> is what's being compiled.</p> <p>The Java compiler will turn any <code>\u</code> escapes into the character prior to actually compiling the file. This means that <code>\u</code> escapes can be used in variable names, class names, method names, and so on. The compiler will turn it into the character itself and use it in the compiling process. This is why you're supposed to use <code>\n</code> in your strings instead of <code>\u000a</code>. If you used the latter, the source code would go from something like this:</p> <pre><code>String s = "My\u000aNewline"; </code></pre> <p>To this:</p> <pre><code>String s = "My Newline"; </code></pre> <p>Which would cause a compiler error since the <code>String</code> literal is split across multiple lines.</p> <p>This can allow you to do some extremely bad things. For example, this is 100% legal Java code and will compile on any operating system:</p> <pre><code>\u0070\u0075\u0062\u006c\u0069\u0063 \u0063\u006c\u0061\u0073\u0073 \u004d\u0061\u0069\u006e \u007b \u0070\u0072\u0069\u0076\u0061\u0074\u0065 \u0073\u0074\u0061\u0074\u0069\u0063 \u0053\u0074\u0072\u0069\u006e\u0067 \u0073\u006f\u006d\u0065\u0053\u0074\u0072\u0069\u006e\u0067 \u003d \u0022\u004d\u0079 \u0053\u0074\u0072\u0069\u006e\u0067\u0022\u003b \u0070\u0075\u0062\u006c\u0069\u0063 \u0073\u0074\u0061\u0074\u0069\u0063 \u0076\u006f\u0069\u0064 \u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d \u0061\u0072\u0067\u0073\u0029 \u007b \u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0073\u006f\u006d\u0065\u0053\u0074\u0072\u0069\u006e\u0067\u0029\u003b \u007d \u007d </code></pre> <p>It prints:</p> <pre><code>My String </code></pre> <p>Don't believe me? <a href="http://ideone.com/yVGwiz">See for yourself.</a> The compiler first converted all <code>\u</code> escapes into characters, which takes everything like <code>\u0070\u0072\u0069\u0076\u0061\u0074\u0065</code> and transforms it into <code>private</code>, or <code>\u0073\u0074\u0061\u0074\u0069\u0063\u0053\u0074\u0072\u0069\u006e\u0067</code> into <code>String</code>.</p> <p>Anyway, to fix your particular issue, all you need to do is double-escape the <code>\</code> like so:</p> <pre><code>public String expectedJSONWithHTMLValues(){ return "{" + "\"someHtml\":{" + "\"html\":\"\\u003Chtml\\u003EHTML\\u0026CSS\\u003C/html\\u003E\"}}"; } </code></pre> <p>It's also worth noting that this String:</p> <pre><code>{"someHtml":{"html":"\u003Chtml\u003EHTML\u0026CSS\u003C/html\u003E"}} </code></pre> <p>Is not equal to this String with the whitespace added in:</p> <pre><code>{ "someHtml": { "html": "\u003Chtml\u003EHTML\u0026CSS\u003C/html\u003E" } } </code></pre> <p><code>String.equals</code> will include whitespace in the equality, so make sure that your two strings are identical character-by-character, including whitespace.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload