|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS Industry News
JSON Serialization with Appcelerator Java Services
Serializing/transforming model objects is really easy to do with Appcelerator IModelObjects
By: Andrew Zuercher
Feb. 19, 2008 07:00 AM
Digg This!
The issue of serializing/transforming model objects is not new, heck I’ve been doing this for quite some time:
When you define your Model classes, there are some very simple things to keep in mind:
Here is a simple example: <public class User implements IModelObject, Serializable { Unit test itNow 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 { Dealing with recursionWhats always a bit of a tangle is understanding how to deal with the recursive/circular relationship. public static JSONObject createBean(IModelObject object,MessageAttr 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 In the snippet below, I’ve omitted the getter/setter methods for the aggregates for simplicity. public class User implements IModelObject, Serializable { Rolling your own serializationAssuming 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 Using Coarse Grained objectsThis 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 = and now with a collection: IMessageDataList<IMessageDataObject> people= SummaryAs 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:
SOA WORLD LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||