Does a pattern exist for serializing Java objects to JSON? -
this question not concerning exact specifics of how serialize java object json representation, rather scalable , testable pattern serializing java objects json. system in i'm maintaining has notion of varying levels of granularity regards serialization of objects. example, system has concept of project. project has following fields:
- name
- description
- owner
- list of tasks
- change history
- other metadata
when serializing list of projects, it's useful return "summary" information:
- name
- description
- owner
omitting more detailed stuff. however, when request single project, "detailed" view returned includes everything. objects in system have notion of summary , detail view, i'm finding in cases, i'm either returning or little information.
to handle attributes returned view, i've annotated class, , described summary , detail view:
@json(summary = { "name", "description", "owner" }, detail = { "name", "description", "owner", "tasks", "changes", ... } class project { private string name; ... } this works decently, mentioned above, find in cases, i'm either returning or little. interested see kind of patterns exist out there flexible approach getting data need. doing wrong if i'm finding i'm needing return different representations of data? should pick set number of object representations , stick that? help.
you use subclassing automatic serialisation framework. example using jaxb (which supports both json , xml):
class detailsummary { public @xmlelement string name; public @xmlelement string description; public @xmlelement string owner; } class detail extends detailsummary { public @xmlelement list<task> tasks; ... } this approach allows multiple levels of detail forces use classes simple records.
Comments
Post a Comment