问题
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