2011-03-22 78 views
1

hello亲爱的开发者 我有一个ruby on rails项目,其中包含一些特定的与java库的逻辑。 我想添加posibility为控制器中的这些对象呈现json和xml,就像活动记录序列化一样。Jruby on Rails java对象序列化

我该怎么办?

在控制器的代码示例

// Create the ContactWS structure 
ContactWS contactInfo = new ContactWS(); 
// Put some data into it 
contactInfo.setPostalCode("12345"); 
contactInfo.setFaxNumber("555-123456"); 
contactInfo.setEmail("[email protected]"); 
// 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与私有属性和 getter/setter方法Java值对象,而无需任何其它方法

下一I应序列这个目的是JSON

+0

我试图从Java对象创建红宝石值对象,并使用非标准红宝石系列化但源代码变得难看.. – Fivell 2011-03-22 15:48:25

+0

问题是模糊。你能提供一个代码片段,或伪代码,它说明你想要做什么? – Davidann 2011-03-22 17:46:00

+0

完成后,对不起我的英文如果我表达自己不清楚 – Fivell 2011-03-22 20:25:40

回答

0

为jruby google-gson会很好 http://code.google.com/p/google-gson/

例如

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