google的@SerializedName和@Expose注解

google的@SerializedName和@Expose注解

2015年02月03日 15:09:45 阅读数:387

注解了@SerializedName的字段会被序列化到JSON中,输出的JSON格式中的名字即为注解时给定的名字。

Java代码 复制代码

  1. public class SomeClassWithFields {   
  2.    @SerializedName("name") private final String someField;   
  3.    private final String someOtherField;   
  4.   
  5.    public SomeClassWithFields(String a, String b) {   
  6.      this.someField = a;   
  7.      this.someOtherField = b;   
  8.    }   
  9.  }  

[java] view plain copy

  1. public class SomeClassWithFields {  
  2.    @SerializedName("name") private final String someField;  
  3.    private final String someOtherField;  
  4.   
  5.    public SomeClassWithFields(String a, String b) {  
  6.      this.someField = a;  
  7.      this.someOtherField = b;  
  8.    }  
  9.  }  

Java代码 复制代码

  1. SomeClassWithFields objectToSerialize = new SomeClassWithFields("a", "b");   
  2.  Gson gson = new Gson();   
  3.  String jsonRepresentation = gson.toJson(objectToSerialize);   
  4.  System.out.println(jsonRepresentation);   
  5.   
  6.  ===== OUTPUT =====   
  7.  {"name":"a","someOtherField":"b"}  

[java] view plain copy

  1. SomeClassWithFields objectToSerialize = new SomeClassWithFields("a", "b");  
  2.  Gson gson = new Gson();  
  3.  String jsonRepresentation = gson.toJson(objectToSerialize);  
  4.  System.out.println(jsonRepresentation);  
  5.   
  6.  ===== OUTPUT =====  
  7.  {"name":"a","someOtherField":"b"}  

上面是google API的例子。这个注解可以用在需要以JSON格式输出pojo类信息的情况。

同样@Expose注解的意思是:An annotation that indicates this member should be exposed for JSON serialization or deserialization。这时google API上的一些话:

 

This annotation has no effect unless you build com.google.gson.Gson with a com.google.gson.GsonBuilder and invoke com.google.gson.GsonBuilder.excludeFieldsWithoutExposeAnnotation() method.

Here is an example of how this annotation is meant to be used:

 

 public class User {
   @Expose private String firstName;
   @Expose private String lastName;
   @Expose private String emailAddress;
   private String password;
 }
 

If you created Gson with new Gson(), the toJson() and fromJson() methods will use the password field along-with firstName, lastName, and emailAddress for serialization and deserialization. However, if you created Gson with Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() then the toJson() and fromJson() methods of Gson will exclude the password field. This is because the password field is not marked with the @Expose annotation.

Note that another way to achieve the same effect would have been to just mark the password field as transient, and Gson would have excluded it even with default settings. The @Expose annotation is useful in a style of programming where you want to explicitly specify all fields that should get considered for serialization or deserialization.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值