DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

JSONModel Container

Open OptimistNot opened this issue 11 years ago • 4 comments

Hey! ok, i'm receiving json from server , setData() with it and after trying to convert it toModel(). (of course i'm using jsonmodel) how am i supposed to convert jsonmodel toModel() if my foreign key on .getInstance() from jsonmodel passing String? im getting cast error (JSONObject) inValue, can't cast string to json. What if i try convert to model with null foreign keys?? getting NPE even if in my model foreign keys can be null. Ideas? Any help would be appreciated.. Thanks!

OptimistNot avatar Jun 04 '15 11:06 OptimistNot

ok, i had to override it... this is bad and i should feel bad but still...better then nothing!

public BaseModelContainer getInstance(Object inValue, Class columnClass) {

    if(inValue != null && inValue.toString().equals("{}")){
        return new JsonModel((JSONObject)inValue, columnClass);
    }

    if(columnClass.getSimpleName().equals("YourClassName")){
        YourClassWithJsonModel.getJsonModel().load(inValue);
        return new JsonModel((JSONObject)YourClassWithJsonModel.getJsonModel().getData(), columnClass);
    }

}

OptimistNot avatar Jun 05 '15 15:06 OptimistNot

This piece is meant not to be a Json parser and thus only handles get() from the JSONObject currently, which doesnt attempt to coerce the value to the proper column value. It's possible (and on my roadmap) to create a smarter db connector for JSON and generate code to interact in a much better way than the abstract/limited nature currently introduced by ModelContainer.

agrosner avatar Jun 11 '15 19:06 agrosner

Hi,

Can you show how to use dbflow with json that has a 1-n relationship?

ihajat avatar Nov 21 '15 23:11 ihajat

3.0.0+ has smarter value conversion for ModelContainer classes.

agrosner avatar Feb 29 '16 00:02 agrosner