2011-03-22 79 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)