Compilation error: No source code is available for type com.google.gson.Gson

梦想的初衷 提交于 2019-12-24 08:47:32

问题


I'm trying to parse a json string on client side using Gson but while installing project getting an error:

 No source code is available for type com.google.gson.Gson; did you forget to inherit a required module?

I have added <inherits name='com.google.gson.Gson' /> on my gwt.xml. I'm using GWT 2.8.1 can we use Gson on client side ?
[this][1]

using Gson library in GWT client code used older version of GWT and my question is also related to error of Gson module .


回答1:


Gson is not GWT compatible. You should use a GWT compatible library like gwt-jackson.

Alternatively, if your models are simple you can use a technique called JsInterop DTOs. And use the browser native JSON.parse function directly. This technique is based in JsInterop and some limitation explained here. Example:

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object") 
class SearchResult {
    public String display_name; //ex: "Málaga, Provincia de Málaga, Andalusia, Spain"
    public String lat, lon; //ex: lat: "36.7210805", lon: "-4.4210409"
    public double importance; //ex: 0.73359836669253
}

And then call native parse (need to import elemental2-core library):

Object jsonObj = elemental2.core.Global.JSON.parse(jsonStr);
SearchResult result = jsinterop.base.Js.cast(jsonObj);


来源:https://stackoverflow.com/questions/50563002/compilation-error-no-source-code-is-available-for-type-com-google-gson-gson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!