标签:serialize efi class ota def build serial transient tran
new Gson().toJson(obj)
Gson gson = new GsonBuilder().serializeNulls().create();
gson.toJson(obj)
字段加上transient修饰符
public transient int x;
String json = new Gson().toJson(obj);
Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.PROTECTED).create();
String json = gson.toJson(obj);
没有被@Expose标注的字段会被排除
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String json = gson.toJson(obj);
class MyObj {
public int _x; // <---
public int y;
public MyObj(int x, int y) {
this._x = x;
this.y = y;
}
}
@Test
public void gson() {
ExclusionStrategy myExclusionStrategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes fa) {
return fa.getName().startsWith("_"); // <---
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
};
Gson gson = new GsonBuilder()
.setExclusionStrategies(myExclusionStrategy) // <---
.create();
MyObj obj = new MyObj(1, 2);
String json = gson.toJson(obj);
Assert.assertEquals("{\"y\":2}", json);
}
标签:serialize efi class ota def build serial transient tran
原文地址:https://www.cnblogs.com/format-ch/p/14914029.html