JDX Programming Code Snippets

Mapping JSON Objects


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.

Class Definitions

                
                
  1. import org.json.JSONException;
  2. import org.json.JSONObject;
  3.  
  4. import com.softwaretree.jdxandroid.JDX_JSONObject;
  5.  
  6. public class JSON_Employee extends JDX_JSONObject {
  7.  
  8. public JSON_Employee() {
  9. super();
  10. }
  11.  
  12. public JSON_Employee(JSONObject jsonObject) throws JSONException {
  13. super(jsonObject);
  14. }
  15. }
Notes:
  • JSON_Employee is a simple shell class defining a domain model object class for Employee objects based on the JSONObject class.
    (line 6)
  • The JDX_JSONObject class serves as a base class for persisting JSONObject instances of a domain specific class (JSON_Employee), which is defined as a subclass of this class.
    (line 6)
  • This class needs to define just two constructors. Most of the processing is handled by the superclass JDX_JSONObject.
    (lines 8 and 12)

Mapping Specification

           
           
  1. CLASS com.softwaretree.jdxandroidjsonexample.model.JSON_Employee TABLE Employee
  2. VIRTUAL_ATTRIB id ATTRIB_TYPE int
  3. VIRTUAL_ATTRIB name ATTRIB_TYPE java.lang.String
  4. VIRTUAL_ATTRIB exempt ATTRIB_TYPE boolean
  5. VIRTUAL_ATTRIB compensation ATTRIB_TYPE double
  6. VIRTUAL_ATTRIB DOB ATTRIB_TYPE long
  7.  
  8. PRIMARY_KEY id
  9. SQLMAP FOR compensation COLUMN_NAME salary
  10. ;
Notes:
  • A VIRTUAL_ATTRIB specification is used to identify each persistent attribute of the corresponding JSON object (i.e., id, name, exempt, compensation, and DOB of an employee).
    (lines 2 to 6)
  • A date is represented as long (number of milliseconds) for a JSON object since there is no standard JSON string format for date representation.
    (line 6)
  • Rest of the mapping specification is provided after all the VIRTUAL_ATTRIB specifications.
    (lines 8 to 9)

Programming Example

                
                
  1. // Assuming jdxSetup of type JDXSetup has already been initialized.
  2. // Obtain ORM handles.
  3. JXResource jxResource = jdxSetup.checkoutJXResource();v
  4. JXSession jxSessionHandle = jxResource.getJXSessionHandle();
  5. JDXS jdxHandle = jxResource.getJDXHandle();
  6. String employeeClassName = JSON_Employee.class.getName();
  7. DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
  8. dfm.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
  9. try {
  10. // First delete all existing employees from the database.
  11. jdxHandle.delete2(employeeClassName, null, 0);
  12. // Create and save a new JSON employee object, Mark.
  13. JSON_Employee emp = new JSON_Employee();
  14. emp.put("id", 1);
  15. emp.put("name", "Mark");
  16. emp.put("exempt", true);
  17. emp.put("compensation", 51001.0);
  18. emp.put("DOB", dfm.parse("1981-01-01").getTime());
  19. jdxHandle.insert(emp, 0, null);
  20. // Create and save a new JSON employee object, David, using an explicit JSONObject instance.
  21. JSONObject jsonObject = new JSONObject();
  22. jsonObject.put("id", 2);
  23. jsonObject.put("name", "David");
  24. jsonObject.put("exempt", true);
  25. jsonObject.put("compensation", 52002.0);
  26. jsonObject.put("DOB", dfm.parse("1982-02-02").getTime());
  27. emp = new JSON_Employee(jsonObject);
  28. jdxHandle.insert(emp, 0, null);
  29. // Create and save a new JSON employee, Steve, using an alternate way by employing a JSON string.
  30. // The JSON object string may come from another source, for example a web service.
  31. String dob = new Long(dfm.parse("1983-03-03").getTime()) .toString();
  32. String jsonString = "{\"id\": 3, \"name\": \"Steve\", \"DOB\":" + dob + "," + "\"exempt\": false,\"compensation\": 53003.0}";
  33. jsonObject = new JSONObject(jsonString);
  34. emp = new JSON_Employee(jsonObject);
  35. jdxHandle.insert(emp, 0, null);
  36. // Retrieve all the employees
  37. List queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
  38. // Retrieve employee, David (id=2)
  39. ObjectId oid = ObjectId.createObjectId(employeeClassName +"; id=2");
  40. emp = (JSON_Employee) jdxHandle.getObjectById(oid, false, JDXS.FLAG_SHALLOW, null);
  41. // Get the underlying JSONObject instance from the Employee object
  42. retrieved from the database
  43. jsonObject = emp.getJSONObject();
  44. } catch (Exception ex) {
  45. Log.e("JDX", "Error" ex);
  46. throw ex;
  47. } finally {
  48. jdxSetup.checkinJXResource(jxResource);
  49. }
Notes:
  • All the notes of the Simple Mapping 1 Example apply.
  • Whereas the Simple Mapping Example 1 uses a higher-level façade (JDXHelper) to interact with the JDX ORM system, this code snippet uses JXResource, JXSession, and JDXS objects to interact more directly with the ORM system.
    (lines 4, 5, 6)
  • As various comments in the above programming code suggest, JSON objects may be created and obtained in many different ways.
    (lines 17, 27, 41, 54)