1、簡述
[FieldNamingStrategy]
屬性命名政策 --
用于自定義 json key 的形式
new GsonBuilder()
// 采用自定義的政策
.setFieldNamingStrategy(new FieldStrategy.AllUpperCaseStrategy())
// 采用 預設類的政策
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DOTS)
.create();
-- 注意: 隻能有一個政策起作用。
2、實體類
實作以下功能 ① 讓屬性的名稱 全部改為大寫,
如 username ----> USERNAME
userId ----> USERID
或者 實作 在屬性名的前後面 加上字首 或字尾
注解類如下
package sun.rain.amazing.gson.strategy.field;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 采用注解的用于改變 對應的key值
* @author Reese
*/
@Target( ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface GsonKeyValue {
String value() default "";
String prefix() default "";
String suffix() default "";
}
給屬性添加字首 或字尾
@GsonKeyValue(prefix = "CN_", suffix = "_AN")
private String mobile;
變成 mobile ---> CN_mobile_AN: "xxx"
package sun.rain.amazing.gson.strategy.field;
import com.google.gson.FieldNamingStrategy;
import java.lang.reflect.Field;
/**
* 命名政策
* @author sunRainAmazing
*/
public class FieldStrategy {
/**
* 采用全部大寫的形式
*/
public static class AllUpperCaseStrategy implements FieldNamingStrategy{
@Override
public String translateName(Field f) {
return f.getName().toUpperCase();
}
}
/**
* 添加前置 和 後置 注解
*/
public static class GsonKeyValueCaseStrategy implements FieldNamingStrategy{
@Override
public String translateName(Field f) {
GsonKeyValue gkv = f.getAnnotation(GsonKeyValue.class);
if(gkv != null){
if(gkv.value().length()>0){
return gkv.prefix() + gkv.value() + gkv.suffix();
}else{
return gkv.prefix() + f.getName() + gkv.suffix();
}
}else{
return f.getName();
}
}
}
}
實體類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GsonField {
private Integer id;
@SerializedName("user_name")
private String name;
private String email;
}
package sun.rain.amazing.gson.strategy.field;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author sunRainAmazing
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GsonFieldKey {
private Integer id;
@SerializedName("user_name")
private String name;
@GsonKeyValue(prefix = "QQ_")
private String email;
@GsonKeyValue(prefix = "CN_", suffix = "_AN")
private String mobile;
@GsonKeyValue(value="desc_info", prefix = "CN_", suffix = "_AN")
private String extra;
}
3、測試類
package sun.rain.amazing.gson.strategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Test;
import sun.rain.amazing.gson.strategy.field.FieldStrategy;
import sun.rain.amazing.gson.strategy.field.GsonField;
import sun.rain.amazing.gson.strategy.field.GsonFieldKey;
/**
* @author sunRainAmazing
*/
public class GsonFieldTest {
Gson gson = new Gson();
/**
* 預設注解 優先級 高于自定義的 屬性名政策
* {"ID":10, "user_name":"tom", "EMAIL":"[email protected]"}
* GsonField(id=10, name=tom, [email protected])
*/
@Test
public void test(){
GsonField g = new GsonField(10,"tom","[email protected]");
gson = new GsonBuilder()
.setFieldNamingStrategy(new FieldStrategy.AllUpperCaseStrategy())
.create();
String json = gson.toJson(g);
System.out.println(json);
g = gson.fromJson(json,GsonField.class);
System.out.println(g);
}
/**
* 隻能有一個命名政策起作用
* {"id":10,"user_name":"tom","QQ_email":"[email protected]",
* "CN_mobile_AN":"12025",
* "CN_desc_info_AN":"this is some message"}
*
* GsonFieldKey(id=10, name=tom, [email protected], mobile=12025,
* extra=this is some message)
*/
@Test
public void test1(){
GsonFieldKey g = new GsonFieldKey(10,"tom","[email protected]"
,"12025","this is some message");
gson = new GsonBuilder()
.setFieldNamingStrategy(new FieldStrategy.AllUpperCaseStrategy())
.setFieldNamingStrategy(new FieldStrategy.GsonKeyValueCaseStrategy())
.create();
String json = gson.toJson(g);
System.out.println(json);
g = gson.fromJson(json,GsonFieldKey.class);
System.out.println(g);
}
}