Reputation: 424
When I have JSON object like:
{
"asset": {
"properties": [{
"name": "xxx",
"id": "yy"
}]
}
}
Then I have JsInterop wrappers like so:
@JsType
public class Asset {
@JsProperty
public native jsinterop.base.JsArrayLike<Property> getProperties();
}
and:
@JsType
public class Property {
@JsProperty
public native String getName();
@JsProperty
public native String getId();
}
When somewhere in code I try to access the data, it fails when I try to access the attributes of the Property object.
For example:
Asset asset = dataProvider.getAssets();
console.log(assets) //works ok: => {"properties":Array(1)}
console.log(asset.getProperties()) //works ok: => Array(1)
console.log(asset.getProperties().getAt(0)) //works ok: => {"name":"xxx","id":"yy"}
console.log(asset.getProperties().getAt(0).getName() //does not work: => Uncaught exception: undefined
OR if I make the Asset and Property class (isNative=true):
Uncaught exception: Cannot read property "company" of undefined.
I checked and if I compile the app when I try to get the name or the id, the code compiles to:
castTo(asset.properties[0], 125)
OR if (isNative=true):
castToNative(asset.properties[0], $wnd.com.company.project.Property)
First I thought its because of my implementation of the wrapper for array, but the I discovered the "jsinterop.base.JsArrayLike", so I thought that will solve my problem, but the error remained the same. Am I missing someting? I would really like to use JsInterop instead of JSNI methods and Overlay types, but I cannot get past these arrays.
Upvotes: 0
Views: 571
Reputation: 64561
When you're handling data from JSON, you get plain old Object
and Array
objects. So you need to use
@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")
or you could use interfaces instead, which won't do casts and type checks.
Upvotes: 3