开发者

Jruby on Rails java object serialization

hello dear developers I have a ruby on rails project that contain some specific logic with j开发者_如何转开发ava library. I want to add posibility to render json and xml for this objects in controllers , just like active record serialization.

How can I do it ?

Example of code in controller

// Create the ContactWS structure
ContactWS contactInfo = new ContactWS();
// Put some data into it
contactInfo.setPostalCode("12345");
contactInfo.setFaxNumber("555-123456");
contactInfo.setEmail("foo@bar.com");
// Pass the contact info to the user creation call
// This assumes userData is an already filled UserWS structure.
userData.setContact(contactInfo);
// Now create the user
UserWS newUser = api.createUser(userData);

newUser is Java value object with private properties and getters/setters without any other methods

next I should serialize this object to json


for jruby google-gson will be fine http://code.google.com/p/google-gson/

example

include_package "com.google.gson"
gson = GsonBuilder.new.setFieldNamingPolicy(FieldNamingPolicy::LOWER_CASE_WITH_UNDERSCORES).create()
json = gson.to_json(javaObject)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜