Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass Custom Java object to LiquidCore #66

Closed
kashifgrocerkey opened this issue Oct 8, 2018 · 4 comments
Closed

How to pass Custom Java object to LiquidCore #66

kashifgrocerkey opened this issue Oct 8, 2018 · 4 comments

Comments

@kashifgrocerkey
Copy link

kashifgrocerkey commented Oct 8, 2018

Hi I have a class Person as:

class Person {
@JsonProperty("Name")
String name;

@JsonProperty("Age")
int age;
}

I want to pass this into LiquidCore but i got exception unknown property 'Name' from underlying js function. But if i covert my 'Person' object into Map/HashMap using 'ObjectMapper' and then pass it to LiquidCore then it works fine but it has performance issues.

Please suggest any good way to pass Custom Java objects as well as List of to LiquidCore
@ericwlange
Copy link
Member

The annotations are not well documented, but you can do it like this:

public class Person extends JSObject {
    @jsexport(type = String.class)
    Property<String> name;

    @jsexport(type = Integer.class)
    Property<Integer> age;

    Person(JSContext ctx, String name, Integer age) {
        super(ctx);
        this.name.set(name);
        this.age.set(age);
    }
};

@kashifgrocerkey
Copy link
Author

kashifgrocerkey commented Oct 8, 2018

Thanks for your reply.

How can i add it with @JsonProperty ? Actually I get response from Web Api then serial it with @JsonProperty

Can i do this?

public class Person extends JSObject {
    @jsexport(type = String.class)
    @JsonProperty("Name")    
    Property<String> name;

    @jsexport(type = Integer.class)
    Property<Integer> age;

    Person(JSContext ctx, String name, Integer age) {
        super(ctx);
        this.name.set(name);
        this.age.set(age);
    }
}; 

@ericwlange
Copy link
Member

I don't know much about @JsonProperty, but if it is a JSON object you want to pass to LiquidCore, you can use JSON.parse() like so:

String myJSONString = "..."; // I assume you can get this from ObjectMapper
JSValue person = JSON.parse(jsContext, myJSONString);

But I am not sure if this is any different than what you originally did, so it may have similar performance issues.

@kashifgrocerkey
Copy link
Author

Wow, thanks JSON.parse(jsContext, myJSONString) really helps alot for me and boost performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants