DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

save() cant cast string to long

Open OptimistNot opened this issue 11 years ago • 5 comments

Hey! im trying to save data in JSONModel. Ex: JSONObject object = new JSONObject(json); json_model.setData(object); save(false); and im getting exception Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

on saving my id field! in json i have it like {"final_product_audit_id":200}

i tried: Timber.e("id: " + object.getLong("final_product_audit_id")); and its passed! but this one: Timber.e("id: " + (long)object.get("final_product_audit_id")); failed.

on line: ((long)modelContainer.getValue("final_product_audit_id")) > 0 i got the exception

Thank you!

OptimistNot avatar Apr 13 '15 17:04 OptimistNot

It appears the JSONObject.get(object) is the culprit. So it appears its attempting to retrieve a field which the JSON thinks is an Integer then trying to unbox into a long.

agrosner avatar Apr 14 '15 18:04 agrosner

Either: 1. subclass JsonModel use that instead and return the proper value from the JSONObject so it can cast properly (not ideal, but works) 2. Don't use ModelContainer and just parse into a Model first (even less ideal).

agrosner avatar Apr 14 '15 18:04 agrosner

ok, so i extended JSONModel and Overrided getValue() and for id column of table it works perfectly, but for foreign keys...when bindToContentValues called they (fk) using own container so getValue() is called not from JsonModel but from BaseModelContainer. Any help? Thanks!

OptimistNot avatar Apr 16 '15 14:04 OptimistNot

ok i Overrided getInstance also...now i only work with JsonModel class and getValue() is called properly. Thanks anyway :)

OptimistNot avatar Apr 16 '15 15:04 OptimistNot

Your'e welcome. Its meant for simple cases and may benefit people. Right now its not meant to be a full parsing library inside of an ORM database library. Its possible I can add a separate hook to smooth over the conversions.

agrosner avatar Apr 21 '15 14:04 agrosner