I'm in agreement on simplest approach is generally the best and for me JSON manipulation win out hands down in this situation.
The important bit of the question is '(removing some fields that are not relevant ) to the mobile client'
So from what I understand there is more than one mobile client variant and for each different json fields are selected from the source customer data json.
Using the DTO approach, I would first have to marshall the source customer data JSON to a DTO, then if I had 4 mobile clients then I would need 4 DTO's and multiple method for setting the attributes on each Type of DTO.
Using the JSONObject approach, I would have something like:
JSONObject sourceCustomerData = JSONObject.fromObject(httpResponseInputStream)
and would pass the sourceCustomerData
to a method to return the JSON suitable to individual mobile client something like:
JSONObject customerDataClientX = getRequiredJson(requiredJsonFeildsForMobileClientA, sourceCustomerData)
where requiredJsonFeildsForMobileClientA
is list of required JSON fields. Therefore for each mobile client a different list of required JSON fields can be passed in.
DTO PROS
- Straight forward and verbose code POJO's.
- Suitable for more complex transformations.
- Easily testable.
DTO CONS
- DTO's are models held in code, they cannot changed without manipulating code.
- More code as DTO and there population code are directly related to the number of mobile client variants.
JSONObject PROS
- Straight forward code.
- Easily testable, it's a function on external factors.
- Less code as one shoe fits all, not directly related to the number of mobile client variants
- There is no model held in code, the list of JSON fields for each mobile client can be held in configuration externally.
JSONObject CONS
- Not suitable for more complex transformations.
- JSONObject requires navigation JSON model, not so nice to ready or write.
In future, as more complex transformations are required we can add then in and even consider moving to a DTO’s based model if it makes the task easier.