Gson版本 2.8.2java
附gson-2.8.2下载连接app
@Expose(serialize = false, deserialize = false)
在类的成员上以告诉Gson 跳过本字段的(反)序列化。 (反)序列化时,须要Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
而不是Gson gson = new Gson()
好比有下User类,我不想把nickName也输出出来,网上查了查,说只要把注解改为@Expose(serialize = false, deserialize = false)google
package go.Gson; import com.google.gson.Gson; import com.google.gson.annotations.Expose; public class User { User(String name_, String nickName_) { name = name_; nickName = nickName_; } public static void main(String[] args) { User u = new User("Jackey", "Jack"); Gson gson = new Gson(); System.out.println(gson.toJson(u));//{"name":"Jackey","nickName":"Jack"} } @Expose String name; @Expose(serialize = false, deserialize = false) String nickName; }
事实是注解改了之后, 也仍是会输出nickName的。code
查看了下Gson-2.8.2的源码,在Expose.java中有注释,说如何使用对象
public class User { @Expose private String firstName; @Expose(serialize = false) private String lastName; @Expose (serialize = false, deserialize = false) private String emailAddress; private String password; }
If you created Gson with {@code new Gson()}, the {@code toJson()} and {@code fromJson()}
methods will use the {@code password} field along-with {@code firstName}, {@code lastName},
and {@code emailAddress} for serialization and deserialization. However, if you created Gson
with {@code Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()}
then the {@code toJson()} and {@code fromJson()} methods of Gson will exclude the
{@code password} field. This is because the {@code password} field is not marked with the
{@code @Expose} annotation. Gson will also exclude {@code lastName} and {@code emailAddress}
from serialization since {@code serialize} is set to {@code false}. Similarly, Gson will
exclude {@code emailAddress} from deserialization since {@code deserialize} is set to false.get
即使用Gson gson = new Gson()注解不会生效,须要用Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()去配置Gson源码
代码改为以下便可it
package go.Gson; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; public class User { User(String name_, String nickName_) { name = name_; nickName = nickName_; } public static void main(String[] args) { User u = new User("Jackey", "Jack"); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(u));// {"name":"Jackey"} } @Expose String name; @Expose(serialize = false, deserialize = false) String nickName; }
设想一个UserWrapper类
package go.Gson; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; public class UserWrapper { UserWrapper(){ user = new User("Jackey", "Jack"); } public static void main(String[] args) { UserWrapper u = new UserWrapper(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(u)); } @Expose private User user; } class User { User(String name_, String nickName_) { name = name_; nickName = nickName_; } @Expose String name; @Expose(serialize = false, deserialize = false) String nickName; }
这个会输出{"user":{"name":"Jackey"}}
,这很讨厌,咱们想让User对象只输出name的值,要这么作:
package go.Gson; import java.lang.reflect.Type; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.annotations.Expose; public class UserWrapper { UserWrapper(){ user = new User("Jackey", "Jack"); } public static void main(String[] args) { UserWrapper u = new UserWrapper(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() .registerTypeAdapter(User.class, new UserAdapter()).create(); System.out.println(gson.toJson(u)); } @Expose private User user; } class User { User(String name_, String nickName_) { name = name_; nickName = nickName_; } @Expose String name; @Expose(serialize = false, deserialize = false) String nickName; } class UserAdapter implements JsonSerializer<User> { @Override public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(src.name); } }
相似的interface 还有JsonDeserializer,TypeAdapter