栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Gson可选和必填字段

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gson可选和必填字段

如您所述,Gson无法定义“必填字段”,并且

null
如果JSON中缺少某些内容,您只会进入反序列化的对象。

这是一个可重复使用的反序列化器和注释,它将完成此任务。局限性在于,如果POJO仍然需要自定义反序列化器,则您必须走得更远,或者

Gson
在构造函数中传入一个对象以反序列化为对象本身,或者将注解签出到单独的方法中并使用它在您的解串器中。您还可以通过创建自己的异常并将其传递给来改进异常处理,
JsonParseException
以便可以
getCause()
在调用方中对其进行检测。

综上所述,在绝大多数情况下,这将起作用:

public class App{    public static void main(String[] args)    {        Gson gson = new GsonBuilder() .registerTypeAdapter(TestAnnotationBean.class, new AnnotatedDeserializer<TestAnnotationBean>()) .create();        String json = "{"foo":"This is foo","bar":"this is bar"}";        TestAnnotationBean tab = gson.fromJson(json, TestAnnotationBean.class);        System.out.println(tab.foo);        System.out.println(tab.bar);        json = "{"foo":"This is foo"}";        tab = gson.fromJson(json, TestAnnotationBean.class);        System.out.println(tab.foo);        System.out.println(tab.bar);        json = "{"bar":"This is bar"}";        tab = gson.fromJson(json, TestAnnotationBean.class);        System.out.println(tab.foo);        System.out.println(tab.bar);    }}@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)@interface JsonRequired{}class TestAnnotationBean{    @JsonRequired public String foo;    public String bar;}class AnnotatedDeserializer<T> implements JsonDeserializer<T>{    public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException    {        T pojo = new Gson().fromJson(je, type);        Field[] fields = pojo.getClass().getDeclaredFields();        for (Field f : fields)        { if (f.getAnnotation(JsonRequired.class) != null) {     try     {         f.setAccessible(true);         if (f.get(pojo) == null)         {  throw new JsonParseException("Missing field in JSON: " + f.getName());         }     }     catch (IllegalArgumentException ex)     {         Logger.getLogger(AnnotatedDeserializer.class.getName()).log(Level.SEVERE, null, ex);     }     catch (IllegalAccessException ex)     {         Logger.getLogger(AnnotatedDeserializer.class.getName()).log(Level.SEVERE, null, ex);     } }        }        return pojo;    }}

输出:

这是foo这是酒吧这是foo空值线程“ main” com.google.gson.JsonParseException中的异常:JSON中的字段缺失:foo


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号