[deepamehta-devel] JSON deserialization

Jörg Richter jri at deepamehta.de
Fri Sep 16 17:58:55 CEST 2016


Hi Robert,

nice to see you're proceeding with the CROWD Literature app.
Here I want provide you a hint for designing your business service (CrowdService).

In your CrowdService implementation you have this method:

	public void updateEditablePerson(@PathParam("personId") long personId, String editablePersonJSON) {
		...
	}

JSON is meant only as a transport format. Your business service is supposed to abstract the transport and rely on 1st-class business objects instead, e.g. EditablePerson. Keep in mind your service methods are not only callable from remote, but also from within the same OSGi container. Serialization/Deserialization would be an unnecessary overhead in this case.

This would make a better business API:

	public void updateEditablePerson(@PathParam("personId") long personId, EditablePerson person) {
		...
	}

JSON-deserialization (in case of remote invocation) and EditablePerson instantiation is done by JAX-RS then, by the help of DM's standard JAX-RS providers (see module dm4-webservice). All you need to do is to implement a public EditablePerson constructor which expects a JSONObject as a parameter:

	public class EditablePerson ... {

		String firstName;
		String lastName;

		// this constructor is invoked by DM4 Webservice / JAX-RS
		public EditablePerson(JSONObject json) throws JSONException {
			this.firstName = json.getString("firstName");
			this.lastName = json.getString("lastName");
		}

		public EditablePerson(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}

		...
	}

A developer would invoke the 2nd constructor (which takes the values directly) when calling your updateEditablePerson() method locally, possibly from another DM module which consumes your CrowdService.

Cheers,
Jörg

-------------- n�chster Teil --------------
Ein Dateianhang mit Bin�rdaten wurde abgetrennt...
Dateiname   : signature.asc
Dateityp    : application/pgp-signature
Dateigr��e  : 496 bytes
Beschreibung: Message signed with OpenPGP using GPGMail
URL         : <http://lists.deepamehta.de/pipermail/devel-lists.deepamehta.de/attachments/20160916/48afdb7f/attachment.sig>


More information about the devel mailing list