Friday, April 5, 2024

Re: How to unescape a JSONString?

I ran into this.  GWT is too smart sometimes.  :)

For my example, I was querying an API for WebRTC ICE servers, which returned a string which was a JSON array.

When I had:

private static native JavaScriptObject createPeerConnection(String iceServersJson) /*-{
  var peerConnectionConfig = {
    iceServers: iceServersJson
  };
  return new RTCPeerConnection(peerConnectionConfig);
}-*/;

GWT excaped the quotes, so iceServers switched from an array, to just a string, exactly like I asked it to.

So, really, I needed what Thomas suggested:

JavaScriptObject iceServersObj = ((JSONArray)JSONParser.parseStrict(iceServersJson)).getJavaScriptObject();

private static native JavaScriptObject createPeerConnection(JavaScriptObject iceServersJson) /*-{
  var peerConnectionConfig = {
    iceServers: iceServersJson
  };
  return new RTCPeerConnection(peerConnectionConfig);
}-*/;

Now I'm telling GWT it's a JSO, and GWT knows not to escape it.

On Monday 9 November 2009 at 10:25:24 pm UTC+11 Thomas Broyer wrote:


On Nov 9, 11:47 am, peterk <peter.ke...@gmail.com> wrote:
> I'm having some trouble dealing with escaping and unescaping of Java
> strings for encoding in JSON.
>
> I use JSONString to encode a Java string and that seems to work ok.
> For example, newlines turn into \n, tabs turn into \t and so on.
>
> However, given this escaped sequence back, how to I turn this back
> into an unescaped javastring wheren \n is turned into a newline and so
> on?
>
> If I use stringvalue() on the JSONString it just gives back the same
> json encoded string with the \n and \t encoding etc.
>
> Anyone have any ideas? :)

JSONParser.parse? ;-)

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/a57c3d85-8385-4021-bdd9-0082f8deffa8n%40googlegroups.com.

No comments:

Post a Comment