简体   繁体   English

Jruby on Rails Java对象序列化

[英]Jruby on Rails java object serialization

hello dear developers I have a ruby on rails project that contain some specific logic with java library. 你好,亲爱的开发人员,我有一个ruby on rails项目,其中包含一些Java库的特定逻辑。 I want to add posibility to render json and xml for this objects in controllers , just like active record serialization. 我想添加位置以在控制器中为此对象呈现json和xml,就像活动记录序列化一样。

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 newUser是具有私有属性和getter / setter的Java值对象,没有任何其他方法

next I should serialize this object to json 接下来我应该将此对象序列化为json

for jruby google-gson will be fine http://code.google.com/p/google-gson/ for jruby google-gson会很好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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM