Can I use the same class for ORMLite and Jackson JSON?

I want to use ORMLite to query data from SQLite and store it in a Java class, and then convert this class to JSON using the Jackson JSON library and send it via HTTP. I also want to do the opposite - get the data from the server in JSON and convert it to a Java class and store this class in SQLite using ORMLite.

Can I do this using one class for a table for both ORMLite and Jackson?

+5
source share
1 answer

Yes you can, why not? you can convert to json any java object you want

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);;

ORMLite create an object that is a java object.

@DatabaseTable(tableName = "accounts")
public class Account {
    @DatabaseField(id = true)
    private String name;

    @DatabaseField(canBeNull = false)
    private String password;
    ...
    Account() {
        // all persisted classes must define a no-arg constructor with at least package visibility
    }
    ...    
}

Yes, you can.

+2
source

All Articles