Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] How to escape Unicode with JSON.toString()?

So while Section 7 indicates a "\u####" notation as optional behavior.
https://tools.ietf.org/html/rfc8259#section-7

That is discouraged in the same spec (Section 8).

It's obvious that section 7 is old, as it limits the "\u" encoding to 3 bytes, even though the UTF-8 / Unicode spec has passed that 3 byte upper limit a while ago and is now at 4 bytes.

The "\u####" behavior would also have no correlation to encoding for XML as indicated by your initial question.

Guess we need more details on what is happening, and what ADM expects, in order to help.


Joakim Erdfelt / joakim@xxxxxxxxxxx

On Wed, Mar 28, 2018 at 10:19 AM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
org.eclipse.jetty.util.ajax.JSON.toString()  produces a JSON formatted string.

The error you are getting back is an XML?
XML encoding is different then JSON encoding.

org.eclipse.jetty.util.ajax.JSON.toString() tries to follow the guidance at https://tools.ietf.org/html/rfc8259#section-8

Perhaps you have some oddball charset declaration getting in your way.
I don't know how ADM works, but if you are submitting the JSON to them in an HttpClient, make sure your `Content-Type` request header says something like "application/json; charset=utf-8"
If ADM is issuing requests to your server, then make sure your `Content-Type` response header has "application/json; charset=utf-8"


Joakim Erdfelt / joakim@xxxxxxxxxxx

On Wed, Mar 28, 2018 at 10:01 AM, Alexander Farber <alexander.farber@xxxxxxxxx> wrote:
Hello fellow Jetty users and developers,

is it please possible to escape UTF-8 characters when using

org.eclipse.jetty.util.ajax.JSON.toString() method?

I understand that it might be an internal library, but until now it
works well for me in a servlet which among other tasks sends push
notifications via FCM (Firebase Cloud Messaging) and ADM (Amazon
Device Messaging).

However my problem with the latter is that ADM does not accept any
UTF-8 chars (in my case Cyrillic) and reproducibly fails with the
cryptic error message:

<SerializationException>
<Message>Could not parse XML</Message>
</SerializationException>

java.lang.IllegalStateException:
unknown char '<'(60) in |||<SerializationException>|  <Message>Could
not parse XML</Message>|</SerializationException>||

So is there maybe some possibility in Jetty 9.4.8.v20171121 to encode the chars?

Here is my Java code:

    // this string is POSTed to ADM server
    public String toAdmBody() {
        Map<String, Object> root  = new HashMap<>();
        Map<String, String> data  = new HashMap<>();
        root.put(KEY_DATA, data);
        data.put(KEY_BODY, mBody);
        // ADM does not accept integers
        data.put(KEY_GID, String.valueOf(mGid));
        // TODO encode utf8 chars
        return JSON.toString(root);
    }

Thank you
Alex
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users



Back to the top