In context of a web application, especially when dealing with Ajax, it is handy to have your classes convert to to JSON to send to a browser. If you have a complex model with custom classes to be sent over, you will most likely write your JSON generation code, if however all you need is to convert ActiveJDBC models to JSON, this functionality is already available from your models without extra effort.

In more complicated situation, they probably would write some JSON generation code. However under simple condition, classes Model and LazyList already provide the basics.

Generate simple JSON from a model

Here is code that will provide stock JSON from a model:

The JSON produced will look something like this:

The boolean parameter to the method toJson() is whether to generate human readable format or a single string.

Specify output attributes for generated JSON

A variation on the example above is to provide a list of attributes that you are interested in, so that only these attributes are included:

The resulting JSON will only include the attributes specified:

Inclusion of dependencies

When a model is has relationships, the generated JSON will loop through them to include their JSON into the parent JSON as well:

result:

Generate JSON from a resultset

Generating JSON from a LazyList is equally easy:

An example of generated JSON:

[
  {
    "type":"org.javalite.activejdbc.test_models.User",
    "id":"1",
    "first_name":"Marilyn",
    "email":"mmonroe@yahoo.com",
    "last_name":"Monroe",
    "children" : {
      addresses : [
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"1",
        "zip":"60606",
        "state":"IL",
        "address1":"123 Pine St.",
        "address2":"apt 31",
        "user_id":"1",
        "city":"Springfield"
      },
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"2",
        "zip":"60606",
        "state":"IL",
        "address1":"456 Brook St.",
        "address2":"apt 21",
        "user_id":"1",
        "city":"Springfield"
      },
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"3",
        "zip":"60606",
        "state":"IL",
        "address1":"23 Grove St.",
        "address2":"apt 32",
        "user_id":"1",
        "city":"Springfield"
      }
    ]
    }
  },
  {
    "type":"org.javalite.activejdbc.test_models.User",
    "id":"2",
    "first_name":"John",
    "email":"jdoe@gmail.com",
    "last_name":"Doe",
    "children" : {
      addresses : [
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"4",
        "zip":"60606",
        "state":"IL",
        "address1":"143 Madison St.",
        "address2":"apt 34",
        "user_id":"2",
        "city":"Springfield"
      },
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"5",
        "zip":"60606",
        "state":"IL",
        "address1":"153 Creek St.",
        "address2":"apt 35",
        "user_id":"2",
        "city":"Springfield"
      },
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"6",
        "zip":"60606",
        "state":"IL",
        "address1":"163 Gorge St.",
        "address2":"apt 36",
        "user_id":"2",
        "city":"Springfield"
      },
        {
        "type":"org.javalite.activejdbc.test_models.Address",
        "id":"7",
        "zip":"60606",
        "state":"IL",
        "address1":"173 Far Side.",
        "address2":"apt 37",
        "user_id":"2",
        "city":"Springfield"
      }
    ]
    }
  }
]

Since the include() was used, the corresponding children from the ADDRESSES table were queried too for their JSON.

Hydrate ActiveJDBC models from JSON

Sometimes you need to do the opposite of generating JSON. For instance, when I build webservices using ActiveWeb, often a JSON document is posted in a web request, and it needs to be serialized to a model. This functionality is not built into ActiveJDBC because this would require adding a dependency to parse JSON, but it is not needed by most projects. The solution is super simple: add a class with this code to your project:

then add Jackson dependency:

After this, it is trivial to write code like this:

In general, if there is another format, say XML or YML, you can convert that to a Java Map, then initialize a model from that Map


How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page