Wednesday, May 22, 2024

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



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 JsDate 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<ReferenzaDTOGWT> getNodeIdAllegatti() { return List.of(this.nodeIdAllegatti); }
@JsOverlay public void setNodeIdAllegatti(List<ReferenzaDTOGWT> nodeIdAllegatti) { this.nodeIdAllegatti = nodeIdAllegatti.toArray(new ReferenzaDTOGWT[nodeAllegatti.size()]); }

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

Of course for the Map<String, List<MetadatoDTOGWT>> 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.<String>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-utilsit'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+unsubscribe@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.

No comments:

Post a Comment