There's no escaping. You have a string value, it stays a string value. If 
its content is JSON representing an array and you want that array, then 
indeed you have to parse the JSON.

It looks like there's a major misunderstanding about what GWT does with 
your code, and/or possibly where/when the code runs or something.
GWT "only" translates the Java syntax to JS (and also optimizes 
everything), and therefore comes with a library of classes that emulates 
the Java runtime core classes so they can also be translated the same way 
as your code. JSNI is an escape hatch to be able to "put JS syntax inside 
your Java syntax", but that's all.
In other words, if you have a string "[ 42, true, null ]" in a variable 
(that you retrieved from your server), if you call that method, it's 
exactly equivalent to this JS:
var iceServersJson = "[ 42, true, null ]";
var peerConnectionConfig = {
  iceServers: iceServersJson
};
i.e. the peerConnectionConfig object has an iceServers property whose value 
is just the iceServersJson string value.
GWT won't "magically" generate JS code at runtime replacing the value as-is 
to form some new JS each time, i.e. it *won't* become:
var peerConnectionConfig = {
  iceServers: [ 42, true, null ]
};
No, really, that Java/JSNI function is transformed to this JS function:
function createPeerConnection(iceServersJson) {
  var peerConnectionConfig = {
    iceServers: iceServersJson
  };
  return new RTCPeerConnection(peerConnectionConfig);
}
and then at one point you call it. It's your job to give it either a string 
value or parse the string value as JSON and passe the result.

Kudos to resurrecting a 15 years old post though! 🤣

BTW, you may want to prefer JsonUtils.safeEval(iceServersJson) 
here: 
https://www.gwtproject.org/javadoc/latest/com/google/gwt/core/client/JsonUtils.html
 
(and actually you may want to move to using JsInterop rather than JSNI, and 
maybe Elemental 2)

On Saturday, April 6, 2024 at 8:01:13 AM UTC+2 ma...@craig-mitchell.com 
wrote:

> 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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-web-toolkit/23fa1804-789c-4d25-98de-6b8770519e77n%40googlegroups.com.

Reply via email to