Parsers¶
To communicate with each other, web services and mobile applications need to use a common format to exchange data. Most broadly used formats are XML or JSON, the latter being more suited for mobile networks due to the small size of the data.
Frequently, data coming from the server needs to be decoded to a high-level model. On the other hand, data coming from the application needs to be encoded to be sent to the web service, using a data format the web service can understand. The Content API provides two integrated interfaces that need to be implemented to realize those two operations: Serializer and Deserializer.
Imagine the application receives JSON data about a person from the web service. This data may look like:
{
"name" : "John Doe",
"city" : "Los Angeles"
"country" : "United States"
}
This data will be represented by the following class in the application code (simplified):
public class Person {
private String name;
public void getName() {
return this.name;
}
public void getName(String name) {
this.name = name;
}
}
Let’s see how we can intergrate with the Content API to make it transform the JSON text to an instance of the Person class.
Getting a model object¶
public class JSONToPerson implements Deserializer {
public static final String PARSER_TYPE = "JSONToPerson";
public Object deserialize(byte[] data) {
Person person = new Person();
String jsonString = new String(data, "UTF-8");
JSONObject json = new JSONObject(jsonString);
person.setName(json.optString("name"));
return person;
}
}
In Application class:
// Registers deserializer inside the single manager.
ContentManager manager....
manager.putDeserializer(JSONToPerson.PARSER_TYPE, new JSONToPerson());
In Screen class:
public static final String GET_PERSON = "GET_PERSON";
/**
* @see #onGetPerson(Person) to handle model objet
*/
private void queryGetPerson() {
// prepares query with parser
ContentQuery query = new ContentQuery(Method.GET, "http://..../person");
query.setResponseParserType(JSONToPerson.PARSER_TYPE);
query.setContentResponseType(GET_PERSON);
// requests, parses responses
postQuery(query);
}
protected void onContentResponse(ContentResponse response) {
if (response.hasType(GET_PERSON)) {
onGetPerson((Person)response.getValue());
}
}
private void onGetPerson(Person person) {
// do some stuff
}
Posting a model object¶
PersonToJSON class:
public class PersonToJSON implements Parser {
public static final String PARSER_TYPE = "PersonToJSON";
public byte[] serialize(Object personObject) {
Person person = (Person)personObject;
JSONObject json = new JSONObject();
json.put("name", person.getName());
return json.toString().getBytes("UTF-8");
}
}
In Application class:
ContentManager manager....
manager.putParser(PersonToJSON.PARSER_TYPE, new PersonToJSON());
In Screen class:
private void queryPostPerson() {
// prepares query with parser
ContentQuery query = new ContentQuery(Method.POST, "http://..../person");
Person person = new Person();
person.setName("Doe");
query.setParameterParserType(PersonToJSON.PARSER_TYPE);
query.setParameter(person);
query.setContentType("application/json");
// requests, parses responses
postQuery(query);
}