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