This snippet shows how to define classes and mapping specifications for persisting JSON objects in a database using JDX ORM. It also shows JDX ORM programming code of how JSON objects can easily be inserted, deleted, updated, and retrieved in an intuitive object oriented way.
Although this snippet shows how JDX can help you with persistence of simple JSON objects, JDX can also help you easily handle complex JSON objects having other nested JSON objects and arrays of JSON objects.
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.softwaretree.jdxandroid.JDX_JSONObject;
- public class JSON_Employee extends JDX_JSONObject {
- public JSON_Employee() {
- super();
- }
- public JSON_Employee(JSONObject jsonObject) throws JSONException {
- super(jsonObject);
- }
- }
- CLASS com.softwaretree.jdxandroidjsonexample.model.JSON_Employee TABLE Employee
- VIRTUAL_ATTRIB id ATTRIB_TYPE int
- VIRTUAL_ATTRIB name ATTRIB_TYPE java.lang.String
- VIRTUAL_ATTRIB exempt ATTRIB_TYPE boolean
- VIRTUAL_ATTRIB compensation ATTRIB_TYPE double
- VIRTUAL_ATTRIB DOB ATTRIB_TYPE long
- PRIMARY_KEY id
- SQLMAP FOR compensation COLUMN_NAME salary
- ;
- // Assuming jdxSetup of type JDXSetup has already been initialized.
- // Obtain ORM handles.
- JXResource jxResource = jdxSetup.checkoutJXResource();v
- JXSession jxSessionHandle = jxResource.getJXSessionHandle();
- JDXS jdxHandle = jxResource.getJDXHandle();
- String employeeClassName = JSON_Employee.class.getName();
- DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
- dfm.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
- try {
- // First delete all existing employees from the database.
- jdxHandle.delete2(employeeClassName, null, 0);
- // Create and save a new JSON employee object, Mark.
- JSON_Employee emp = new JSON_Employee();
- emp.put("id", 1);
- emp.put("name", "Mark");
- emp.put("exempt", true);
- emp.put("compensation", 51001.0);
- emp.put("DOB", dfm.parse("1981-01-01").getTime());
- jdxHandle.insert(emp, 0, null);
- // Create and save a new JSON employee object, David, using an explicit JSONObject instance.
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("id", 2);
- jsonObject.put("name", "David");
- jsonObject.put("exempt", true);
- jsonObject.put("compensation", 52002.0);
- jsonObject.put("DOB", dfm.parse("1982-02-02").getTime());
- emp = new JSON_Employee(jsonObject);
- jdxHandle.insert(emp, 0, null);
- // Create and save a new JSON employee, Steve, using an alternate way by employing a JSON string.
- // The JSON object string may come from another source, for example a web service.
- String dob = new Long(dfm.parse("1983-03-03").getTime()) .toString();
- String jsonString = "{\"id\": 3, \"name\": \"Steve\", \"DOB\":" + dob + "," + "\"exempt\": false,\"compensation\": 53003.0}";
- jsonObject = new JSONObject(jsonString);
- emp = new JSON_Employee(jsonObject);
- jdxHandle.insert(emp, 0, null);
- // Retrieve all the employees
- List queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
- // Retrieve employee, David (id=2)
- ObjectId oid = ObjectId.createObjectId(employeeClassName +"; id=2");
- emp = (JSON_Employee) jdxHandle.getObjectById(oid, false, JDXS.FLAG_SHALLOW, null);
- // Get the underlying JSONObject instance from the Employee object
- retrieved from the database
- jsonObject = emp.getJSONObject();
- } catch (Exception ex) {
- Log.e("JDX", "Error" ex);
- throw ex;
- } finally {
- jdxSetup.checkinJXResource(jxResource);
- }