| By Andrew Zuercher | Article Rating: |
|
| February 19, 2008 07:00 AM EST | Reads: |
15,530 |
The issue of serializing/transforming model objects is not new, heck I’ve been doing this for quite some time:
- RMI (ejb/corba)
- XML (jms, soap, etc..)
- JSON
When you define your Model classes, there are some very simple things to keep in mind:
- annotate your attributes with @MessageAttr
- have your class implement IModelObject
Here is a simple example:
<public class User implements IModelObject, Serializable {
private static final long serialVersionUID = 1L;
@MessageAttr
public String name;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
...
}
Unit test it
Now to test the rendering of our object to JSON with a simple junit test…. As you can see, we leverage the Appcelerator framework to serialize our objects to JSON
public class ForumTest extends TestCase {
public void testSimple() {
User user = createUser();
Message message = new Message();
JSONMessageDataObject data = new JSONMessageDataObject();
message.setData(data);
message.getData().put("user", user);
message.getData().put("count", 1);
String messagestr = data.toDataString();
assertEquals(messagestr,"\"user\":{\"password\":
\"pwd\",\"threads\":0,\"fullName\"
:\"antewew\",\"username\":\"azuercher\",\"state\":
\"mystate\",\"email\":\"email\",
\"posts\":0,\"id\":0},\"count\":1}");
}
private User createUser() {
User user = new User();
user.setEmail("email");
user.setFullName("ante wew");
user.setId(new Long(0));
user.setPassword("pwd");
user.setPosts(new Long(0));
user.setState("mystate");
user.setThreads(new Long(0));
user.setUsername("azuercher");
return user;
}
}
Dealing with recursion
Whats always a bit of a tangle is understanding how to deal with the recursive/circular relationship.
If you take a look at JSONObject you will see 2 overridden methods for createBean
public static JSONObject createBean(IModelObject object,MessageAttr
parentAtt, String context,String[] parentSuppres,int level, int maxlevels)
public static JSONObject createBean(IModelObject object)
The latter is obviously a bit more simple, but the former is where the power is. In the MessageAttr annotation, you can provide the suppress attribute which is a comma separated list of aggregates (using bean.name notation to not serialize). This is used for attributes in your IModelObject implementation where the association is with another IModelObject. The following model is for a forum object model where the following aggregate hierarchy exists:
* Forum
** ForumThread
*** Post
In the snippet below, I’ve omitted the getter/setter methods for the aggregates for simplicity.
public class User implements IModelObject, Serializable {
@MessageAttr (suppress="user,thread.lastPost")
public Post lastPost;
}
public class Post implements IModelObject, Serializable {
@MessageAttr(suppress="lastPost,forum.lastPost")
public Forumthread thread;
@MessageAttr (suppress="lastPost")
public User user;
}
public class Forumthread implements IModelObject, Serializable {
@MessageAttr (suppress="lastPost")
public Forum forum;
@MessageAttr (suppress="thread,user.lastPost")
public Post lastPost;
}
public class Forum implements IModelObject, Serializable {
@MessageAttr (suppress="thread.forum,thread.lastPost,user.lastPost")
public Post lastPost;
}
Rolling your own serialization
Assuming you know what your JSON string is going to look like, you can use our RawMessageDataList and RawMessageDataObject to serialze your objects. This is pretty useful if you already are rendering JSON in an existing framework and don’t want to have to transform to and back again. The snippet below shows with static strings just so that you get the idea:
IMessageDataList people = new
RawMessageDataList(
"[{'name':'joe','age':22},{'name':'jane','age':33}]");
IMessageDataList dog = new RawMessageDataObject("{'breed':'doberman','weight':78}");
Message message = new Message();
message.setData(new JSONMessageDataObject());
message.getData().put("people", people);
message.getData().put("dog", dog);
Using Coarse Grained objects
This is probably the simplest/prettiest way to implement your services assuming that you aren’t interested in using fine grained classes that are associated with most of today’s persistence frameworks. Here is how you would create a single compound JSON object:
IMessageDataObject dog =
MessageUtils.createMessageDataObject();
obj.put("breed","doberman");
obj.put("wieght",78);Message message = new Message();
message.setData(new JSONMessageDataObject());
message.getData().put("dog", dog);
and now with a collection:
IMessageDataList<IMessageDataObject> people=
MessageUtils.createMessageDataObjectList();
IMessageDataObject joe =
MessageUtils.createMessageDataObject();
joe.put("name","joe");
joe.put("age",22);
IMessageDataObject jane =
MessageUtils.createMessageDataObject();
joe.put("name","jane");
joe.put("age",33);
people.put(joe);
people.put(jane);
Message message = new Message();
message.setData(new JSONMessageDataObject());
message.getData().put("people", people);
Summary
As you can see there are quite a bit of alternatives for you based on what your needs are to accommodate your service implementations. I’ve personally used all of the above as I’ve implemented:
- Model Objects: using Hibernate for persistence
- Custom Serialization: for integrating with pre-rendered objects (commons-monitoring)
- Coarse Grained: in implementing a dashboard/event driven solution
Published February 19, 2008 Reads 15,530
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Andrew Zuercher
Andrew Zuercher is an Enterprise Architect at Appcelerator, advocating the implementation of RIA with agile methodologies.
![]() |
Duty Editor 02/18/08 12:10:16 PM EST | |||
No problem Andrew, we've taken cxare of it. |
||||
![]() |
andrew zuercher 02/18/08 11:04:17 AM EST | |||
to dove tail on the feedback that i left last friday, i was wondering if you could corect the reference to http://zuerchtech.com instead of http://zuerchertech.com which is not associated with me whatsoever. much thanks, -andrew zuercher |
||||
![]() |
Andrew Zuercher 02/15/08 07:21:51 PM EST | |||
Thanks for publishing this article which is also cross posted at: Please note, that although I ran Zuercher Technologies based out of Atlanta, GA I have since dissolved it and now am employed at Appcelerator (http://appcelerator.com) as an Enterprise Architect advocating the implementation of RIA with agile methodologies. |
||||
- Kindle 2 vs Nook
- Is Cloud Computing Like Teenage Sex?
- GovIT Expo Highlights Cloud Computing
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Cloud Computing Can Revitalize Your Career as Software Developer
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Yahoo! SVP Shelton Shugar to Discuss Innovation at Cloud Computing Expo
- Virtualization Journal "Readers' Choice Awards" Voting Is Now Open
- Einstein, Sharks and Clouds: IT Security in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Virtualization Expo Call for Papers Deadline December 15
- Amazon Web Services Database in the Cloud
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Is Cloud Computing Like Teenage Sex?
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Ulitzer News: Search vs New Media
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Confessions of a Ulitzer Addict
- GovIT Expo Highlights Cloud Computing
- Twitter, Linked In, Ning and Ulitzer: Easy Personal Branding Strategy
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- Linus' Top Ten SCO Barbs
- A Closer Look at Damn Small Linux
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Where Are RIA Technologies Headed in 2008?
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- i-Technology Viewpoint: The New Paradigm of IT Buying
- Is Linux Desktop-Ready Yet...or Not?


























