[英]Jruby on Rails java object serialization
你好,亲爱的开发人员,我有一个ruby on rails项目,其中包含一些Java库的特定逻辑。 我想添加位置以在控制器中为此对象呈现json和xml,就像活动记录序列化一样。
我该怎么做 ?
控制器中的代码示例
// 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是具有私有属性和getter / setter的Java值对象,没有任何其他方法
接下来我应该将此对象序列化为json
for 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.