我进行了很多搜索,到目前为止,找到最好的方法是在这篇文章上:
Class to serialize
package net.sghill.example;import net.sghill.example.UserDeserializerimport net.sghill.example.UserSerializerimport org.prehaus.jackson.map.annotate.JsonDeserialize;import org.prehaus.jackson.map.annotate.JsonSerialize;@JsonDeserialize(using = UserDeserializer.class)public class User { private ObjectId id; private String username; private String password; public User(ObjectId id, String username, String password) { this.id = id; this.username = username; this.password = password; } public ObjectId getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; }}
Deserializer class
package net.sghill.example;import net.sghill.example.User;import org.prehaus.jackson.JsonNode;import org.prehaus.jackson.JsonParser;import org.prehaus.jackson.ObjectCodec;import org.prehaus.jackson.map.DeserializationContext;import org.prehaus.jackson.map.JsonDeserializer;import java.io.IOException;public class UserDeserializer extends JsonDeserializer<User> { @Override public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); return new User(null, node.get("username").getTextValue(), node.get("password").getTextValue()); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)