如何使用GSon公开方法?

如何使用GSon公开方法?,第1张

如何使用GSon公开方法

我遇到的最好的解决方案是制作一个专用的序列化器:

public class AccountSerializer implements JsonSerializer<Account> {    @Override    public JsonElement serialize(Account account, Type type, JsonSerializationContext context) {        JsonObject root = new JsonObject();        root.addProperty("id", account.id);        root.addProperty("email", account.email);        root.addProperty("enpredPassword", account.getEnpredPassword());        return root;    }}

并在我看来像这样使用它:

GsonBuilder gson = new GsonBuilder();gson.registerTypeAdapter(Account.class, new AccountSerializer());Gson parser = gson.create();renderJSON(parser.toJson(json));

但是

@Expose
为某个方法工作会很棒:它将避免使序列化器仅用于显示方法!



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存