Re: JSInterop and "JSON.stringify" method return "Converting circular structure to JSON"

2024-05-22 Thread Craig Mitchell
Would it not be easier to just create your JSON object manually with the 
classes in com.google.gwt.json.client?  Ie: JSONObject, JSONArray, ...

On Thursday 23 May 2024 at 1:42:29 am UTC+10 Thomas Broyer wrote:

> On Wednesday, May 22, 2024 at 12:43:56 PM UTC+2 tenti...@gmail.com wrote:
>
> I misunderstood the documentation... ty for the clarification Thomas can 
> you give me some confirmations.
>
> The Date issue, When you say to use the JsDate do you mean the one in the 
> elemental2 package (elemental2.core.JsDate) or in the gwt core (
> com.google.gwt.core.client.JsDate) ?
>
> Any one of them, anything that directly maps to a native JS Date object.
>  
>
> So for the Date issue i just enough to replace this code :
>
> @JsProperty *public* *native* *Date* getDataRepertorioDocumento();
>
>  
>
> @JsProperty *public* *native* *void* setDataRepertorioDocumento(*Date* 
> dataRepertorioDocumento);
>
>  
>
> With:
>
> @JsProperty *public* *native* Js*Date* getDataRepertorioDocumento();
>
>  
>
> @JsProperty *public* *native* *void* setDataRepertorioDocumento(*JsDate* 
> dataRepertorioDocumento);
>
>  
>
> Right ?
>
> Yes.
> (note that it works for serializing because a JS Date object has a 
> toJSON() method that returns its toISOString(), but it won't work for 
> parsing JSON, for that you will have to pass a *reviver* function to 
> JSON.parse() that will have to be aware of your object structure to know 
> that the dataRepertorioDocumento property value needs to be parsed to a 
> Date object, or use a @JsOverlay getter/setter pair that will 
> serialize/parse the java.util.Date or JsDate value to/from the JSON 
> representation you want, same as List and Map)
>
> I also missed an instance of Integer in your objects, this will have to be 
> changed to Double.
>  
>
> For the "List" and "Map" problem, i will probably try to use some 
> @JsOverlay instead to use a second argument  on the JSON.stringify by the 
> way can you point me out some example (i'm not very skilled with this 
> library) ?
>
>
> Could be as simple as (note that a copy is made each time the getter or 
> setter is called):
> ReferenzaDTOGWT[] nodeIdAllegatti;
> // This could also use Elemental's JsArrayLike.asList()
> @JsOverlay public List getNodeIdAllegatti() { return 
> List.of(this.nodeIdAllegatti); }
> @JsOverlay public void setNodeIdAllegatti(List 
> nodeIdAllegatti) { this.nodeIdAllegatti = nodeIdAllegatti.toArray(new 
> ReferenzaDTOGWT[nodeAllegatti.size()]); }
>
> JsPropertyMap errors;
> @JsOverlay public Map getErrors() {
>   var ret = new HashMap();
>   errors.forEach(key -> ret.put(ret, errors.get(key)));
>   return ret;
> }
> @JsOverlay public setErrors(Map errors) {
>   var obj = JsPropertyMap.of();
>   errors.forEach((key, value) -> obj.set(key, value));
>   this.errors = obj;
> }
>
> Of course for the Map> mappaAltriMetadati 
> you'd have to also transform each List.
>
> The JSON.stringify replacer could look like:
> JSONType.StringifyReplacerFn replacer = (key, value) -> {
>   if (value instanceof List) {
> return ((List) value).toArray();
>   }
>   if (value instanceof Map) {
> var obj = JsPropertyMap.of();
> ((Map) value).forEach((k, v) -> obj.set(k, v));
> return obj;
>   }
>   if (value instanceof Date) {
> return ((Date) value).getTime(); // pass that to JsDate.create() if 
> you prefer an ISO-formatted String rather than the timestamp
>   }
>   if (value instanceof Integer) {
> return ((Integer) value).doubleValue();
>   }
>   return value;
> };
>  
> This is all totally untested (also note that I haven't actually written 
> GWT code for years, this is all based on memory and the javadocs)
>
> Also I found this project updated for GWT 2.9.0 and java 11  
> https://github.com/jp-solutions/gwt-interop-utils . it’s seem goof enough 
> for my use case,  i’ll try out and let you know it.
>
> Not sure what it actually brings on top of plain old JsInterop Base or 
> Elemental Core…
>

-- 
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/c88b2e0c-0883-4ff1-b9a0-1d52d9040158n%40googlegroups.com.


Re: JSInterop and "JSON.stringify" method return "Converting circular structure to JSON"

2024-05-22 Thread Thomas Broyer


On Wednesday, May 22, 2024 at 12:43:56 PM UTC+2 tenti...@gmail.com wrote:

I misunderstood the documentation... ty for the clarification Thomas can 
you give me some confirmations.

The Date issue, When you say to use the JsDate do you mean the one in the 
elemental2 package (elemental2.core.JsDate) or in the gwt core (
com.google.gwt.core.client.JsDate) ?

Any one of them, anything that directly maps to a native JS Date object.
 

So for the Date issue i just enough to replace this code :

@JsProperty *public* *native* *Date* getDataRepertorioDocumento();

 

@JsProperty *public* *native* *void* setDataRepertorioDocumento(*Date* 
dataRepertorioDocumento);

 

With:

@JsProperty *public* *native* Js*Date* getDataRepertorioDocumento();

 

@JsProperty *public* *native* *void* setDataRepertorioDocumento(*JsDate* 
dataRepertorioDocumento);

 

Right ?

Yes.
(note that it works for serializing because a JS Date object has a toJSON() 
method that returns its toISOString(), but it won't work for parsing JSON, 
for that you will have to pass a *reviver* function to JSON.parse() that 
will have to be aware of your object structure to know that the 
dataRepertorioDocumento property value needs to be parsed to a Date object, 
or use a @JsOverlay getter/setter pair that will serialize/parse the 
java.util.Date or JsDate value to/from the JSON representation you want, 
same as List and Map)

I also missed an instance of Integer in your objects, this will have to be 
changed to Double.
 

For the "List" and "Map" problem, i will probably try to use some 
@JsOverlay instead to use a second argument  on the JSON.stringify by the 
way can you point me out some example (i'm not very skilled with this 
library) ?


Could be as simple as (note that a copy is made each time the getter or 
setter is called):
ReferenzaDTOGWT[] nodeIdAllegatti;
// This could also use Elemental's JsArrayLike.asList()
@JsOverlay public List getNodeIdAllegatti() { return 
List.of(this.nodeIdAllegatti); }
@JsOverlay public void setNodeIdAllegatti(List 
nodeIdAllegatti) { this.nodeIdAllegatti = nodeIdAllegatti.toArray(new 
ReferenzaDTOGWT[nodeAllegatti.size()]); }

JsPropertyMap errors;
@JsOverlay public Map getErrors() {
  var ret = new HashMap();
  errors.forEach(key -> ret.put(ret, errors.get(key)));
  return ret;
}
@JsOverlay public setErrors(Map errors) {
  var obj = JsPropertyMap.of();
  errors.forEach((key, value) -> obj.set(key, value));
  this.errors = obj;
}

Of course for the Map> mappaAltriMetadati 
you'd have to also transform each List.

The JSON.stringify replacer could look like:
JSONType.StringifyReplacerFn replacer = (key, value) -> {
  if (value instanceof List) {
return ((List) value).toArray();
  }
  if (value instanceof Map) {
var obj = JsPropertyMap.of();
((Map) value).forEach((k, v) -> obj.set(k, v));
return obj;
  }
  if (value instanceof Date) {
return ((Date) value).getTime(); // pass that to JsDate.create() if you 
prefer an ISO-formatted String rather than the timestamp
  }
  if (value instanceof Integer) {
return ((Integer) value).doubleValue();
  }
  return value;
};
 
This is all totally untested (also note that I haven't actually written GWT 
code for years, this is all based on memory and the javadocs)

Also I found this project updated for GWT 2.9.0 and java 11  
https://github.com/jp-solutions/gwt-interop-utils . it’s seem goof enough 
for my use case,  i’ll try out and let you know it.

Not sure what it actually brings on top of plain old JsInterop Base or 
Elemental Core…

-- 
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/fb1a696c-7f35-4f8a-8208-d88ff8f15fc9n%40googlegroups.com.


Re: JSInterop and "JSON.stringify" method return "Converting circular structure to JSON"

2024-05-22 Thread Marco Tenti (IoProgrammo88)


I misunderstood the documentation... ty for the clarification Thomas can 
you give me some confirmations.

The Date issue, When you say to use the JsDate do you mean the one in the 
elemental2 package (elemental2.core.JsDate) or in the gwt core (
com.google.gwt.core.client.JsDate) ?

So for the Date issue i just enough to replace this code :

@JsProperty *public* *native* *Date* getDataRepertorioDocumento();

 

@JsProperty *public* *native* *void* setDataRepertorioDocumento(*Date* 
dataRepertorioDocumento);

 

With:

@JsProperty *public* *native* Js*Date* getDataRepertorioDocumento();

 

@JsProperty *public* *native* *void* setDataRepertorioDocumento(*JsDate* 
dataRepertorioDocumento);

 

Right ?

 

For the "List" and "Map" problem, i will probably try to use some 
@JsOverlay instead to use a second argument  on the JSON.stringify by the 
way can you point me out some example (i'm not very skilled with this 
library) ?  
Also I found this project updated for GWT 2.9.0 and java 11  
https://github.com/jp-solutions/gwt-interop-utils 
. it’s seem goof enough for my use case,  i’ll try out and let you know it. 

Il giorno mercoledì 22 maggio 2024 alle 11:50:10 UTC+2 Vassilis Virvilis ha 
scritto:

> or passing a @JsFunction to JSON.stringify() as its second argument 
>
>
> I wish I knew that some time before...
>  
> -- 
> Vassilis Virvilis
>

-- 
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/c3bf1105-8329-431a-a42d-97ce7b4871e8n%40googlegroups.com.


Re: JSInterop and "JSON.stringify" method return "Converting circular structure to JSON"

2024-05-22 Thread Vassilis Virvilis
>
> or passing a @JsFunction to JSON.stringify() as its second argument


I wish I knew that some time before...

-- 
Vassilis Virvilis

-- 
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/CAKbOjExeTQcBh9nHaUpqVYFzjjw%3DVQqvth7g3ZEjE2NoAdYVjg%40mail.gmail.com.