On Jun 7, 11:00 am, Boris Granveaud <
bgran...@gmail.com> wrote:
> Hi,
>
> It seems that JSONParser doesn't like some unicode characters if they
> are not escaped with \uxxxx:
>
> public class Test implements EntryPoint {
> public void onModuleLoad() {
> for (char c = 0x2000; c < 0x2050; c++) {
> String str = "{\"string\":\"" + c + "\"}";
> try {
> JSONValue json = JSONParser.parse(str);
> } catch (Exception e) {
> System.out.println("JSON parse error char=" +
> Integer.toHexString((int) c));
> }
> }
> }
>
> }
>
> In GWT 2.0.3 emulator, I've got the following results:
>
> - Chrome 5.0 and FF 3.6: error with character 0x2028 and 0x2029
> - IE 8.0: no error
>
> It works if I escape the characters with \u2028 and \u2029.
This is because GWT for now uses eval() to "parse" JSON, and U+2028
and U+2029 are line terminators in JavaScript (per spec).
Quoting ECMASCript 5, which defines JSON.parse:
"""JSON uses a more limited set of white space characters than
WhiteSpace and allows Unicode code points U+2028 and U+2029 to
directly appear in JSONString literals without using an escape
sequence."""
> I'm using Jackson on server-side to generate the JSON string which is
> sent to my GWT application and it doesn't escape by default these
> characters. As a workaround, I've implemented a Jackson custom
> serializer.
Jackson is right, as these aren't special characters in JSON, but on
the other hand, it could escape them to cope with web apps that eval()
the result instead of JSON.parse()ing it.