gson使用教程-翻译8
来源:互联网 发布:Linux修改jvm内存大小 编辑:程序博客网 时间:2024/05/16 13:54
原文地址:http://www.studytrails.com/java/json/java-google-json-exclusion-strategy.jsp
在这一节中,我们将会看到如何选择Java对象中的某些属性转换成json.Gson默认是会把java对象中所有的属性都转换成Json.然而,有时候我们想控制那些属性转,那些属性不转.这种控制的方法有几种.甚至对于没有源码的类型都是可以的.不同的方式:
- 使用自定义的注解来忽略注解的属性
- 通过实现ExclusionStrategy接口和实现shouldSkipFiled和shouldSkipClass方法
- 通过使用@Expose注解和在GsonBuilder使用excludeFiledsWithoutExposeAnnotation().这样就会忽略所有没有使用@Expose注解的属性.
以下的demo展示这三种方式
package com.studytrails.json.gson;import java.awt.Color;import com.google.gson.Gson;import com.google.gson.GsonBuilder;public class ExclusionExample { public static void main(String[] args) { // We create an instance of type CAT. Cat cat = new Cat(); cat.setName("Cat"); cat.setAge(1); cat.setColor(Color.BLACK); cat.setCountry("US"); // we allow serializing null. therefore although the fields lazy is // null, it will be serialized. We add a CustomExclusionStrategy that // will exclude the Color class. We also allow only those fields that // have been exposed using the @Expore annotation Gson gson = new GsonBuilder().serializeNulls().setExclusionStrategies(new CustomExclusionStrategy(Color.class)) .excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(cat)); // prints {"name":"Cat","lazy":null} }}
Cat类
package com.studytrails.json.gson;import java.awt.Color;import com.google.gson.annotations.Expose;public class Cat { @Expose private String name; private int age; private Color color; @Expose @Country private String country; @Expose private Boolean lazy = null; public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public void setColor(Color color) { this.color = color; } public int getAge() { return age; } public String getName() { return name; } public Color getColor() { return color; } public void setCountry(String country) { this.country = country; } public String getCountry() { return country; } public void setLazy(Boolean lazy) { this.lazy = lazy; } public Boolean getLazy() { return lazy; }}
Exclusion Strategy
package com.studytrails.json.gson;import com.google.gson.ExclusionStrategy;import com.google.gson.FieldAttributes;/** * This class defines custom exclusion policy. We want to ignore all fields that * have been annotated with the Country annotation. Note that we can also ignore * fields based on name or type. This same policy can be applied to any class. * In this example we apply to the CAT class, but it is not limited to the cat * class. * */public class CustomExclusionStrategy implements ExclusionStrategy { private Class classToExclude; public CustomExclusionStrategy(Class classToExclude) { this.classToExclude = classToExclude; } // This method is called for all fields. if the method returns false the // field is excluded from serialization @Override public boolean shouldSkipField(FieldAttributes f) { if (f.getAnnotation(Country.class) == null) return false; return true; } // This method is called for all classes. If the method returns false the // class is excluded. @Override public boolean shouldSkipClass(Class<?> clazz) { if (clazz.equals(classToExclude)) return true; return false; }}
0 0
- gson使用教程-翻译8
- gson使用教程-翻译1
- gson使用教程-翻译2
- gson使用教程-翻译3
- gson使用教程-翻译4
- gson使用教程-翻译5
- gson使用教程-翻译6
- gson使用教程-翻译7
- Gson使用教程
- GSON 使用教程
- Gson使用简单教程
- FastJson Jackson Gson使用教程
- Gson 教程
- 详解Gson使用(五)实现百度翻译功能
- 【Gson】Gson 使用笔记
- Gson源码学习笔记(1)——Gson使用教程(译)
- Gson教程十一(译):轻松使用仁慈的Gson(容错机制)
- Android Retrofit 2.0(二)使用教程OkHttp3 + Gson + RxJava
- VM虚拟机鼠标体验差
- 支持向量机:Duality
- gson使用教程-翻译7
- 数据结构_队列
- Windows7上使用VS2013编译Caffe源码(不带GPU支持)步骤
- gson使用教程-翻译8
- lightoj 1050 - Marbles (概率DP)
- Python中通过注释修改编码格式的底层来源
- 3种实用常用的清除浮动方法
- Leetcode: Dungeon Game
- Pascal开关灯问题
- Gson补充
- Lengthening Sticks - Codeforces 571 A
- Dom第一天学习总结