Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use one class for both ORMLite and Jackson JSON?

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

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

like image 760
SuitUp Avatar asked Nov 03 '22 21:11

SuitUp


1 Answers

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 entity, which is java object so.

@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.

like image 141
Grigoriev Nick Avatar answered Nov 09 '22 11:11

Grigoriev Nick