我正在将gson用于我的序列化目的,我没有找到一种基于gson基于字段的价值提供的基于excrusionstrategy类将某些字段排除在序列化之外的方法,因为它仅支持它顶级类或基于字段属性的排除。字段属性不包括该字段的值。所以我该怎么做?
实现此目的的方法是为所讨论的类创建自定义序列化器。在允许Gson以默认方式创建JSON对象之后,请根据其值删除要排除的属性。
public class SerializerForMyClass implements JsonSerializer<MyClass> {
@Override
public JsonElement serialize(MyClass obj, Type type, JsonSerializationContext jsc) {
Gson gson = new Gson();
JsonObject jObj = (JsonObject)gson.toJsonTree(obj);
if(obj.getMyProperty()==0){
jObj.remove("myProperty");
}
return jObj;
}
}
并在此类应用程序中用于序列化的GSON对象中注册新序列化器。
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MyClass.class, new SerializerForMyClass());
Gson gson=gsonBuilder.create();
gson.toJson(myObjectOfTypeMyClass);
这就是我使用类型适配器避免序列化 false
的布尔值的方式。它避免创建其他Gson
实例,并且不依赖特定字段名称。
class MyClassTypeAdapter: JsonSerializer<MyClass>{
override fun serialize(myObject: MyClass, type: Type, context: JsonSerializationContext): JsonElement {
val jsonElement = context.serialize(myObject)
jsonElement.asJsonObject.entrySet().removeAll { it.value is JsonPrimitive && (it.value as JsonPrimitive).isBoolean && !it.value.asBoolean }
return jsonElement
}
}
这是我写的班级的一个更深入的示例,以删除所有错误的布尔人以及所有" false"字符串。它很快就被扔了在一起,但似乎还可以。让我知道是否有任何错误。
public class RemoveFalseJsonSerializer implements JsonSerializer<Object> {
//~ Methods --------------------------------------------------------------------------------------------------------
/**
* serialize
*
* @param object in value
* @param type in value
* @param jsonSerializationContext in value
*
* @return out value
*/
@Override
public JsonElement serialize(Object object, Type type, JsonSerializationContext jsonSerializationContext) {
Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(object);
trimJson(jsonElement);
return jsonElement;
}
/**
* We've finally made it to a primitive of some sort. Should we trim it?
*
* @param jsonElement in value
*
* @return out value
*/
private boolean shouldTrimElement(JsonElement jsonElement) {
return jsonElement == null || jsonElement.isJsonNull()
|| (jsonElement.isJsonPrimitive()
&& ((jsonElement.getAsJsonPrimitive().isBoolean() && !jsonElement.getAsBoolean()) // trim false
|| (jsonElement.getAsJsonPrimitive().isString() // also trim the string "false"
&& "false".equalsIgnoreCase(jsonElement.getAsString()))));
}
/**
* trimJson
*
* @param jsonElement in value
*/
private void trimJson(JsonElement jsonElement) {
if (jsonElement == null || jsonElement.isJsonNull() || jsonElement.isJsonPrimitive()) {
return;
}
if (jsonElement.isJsonObject()) {
List<String> toRemove = new ArrayList<>();
JsonObject asJsonObject = jsonElement.getAsJsonObject();
for (Map.Entry<String, JsonElement> jsonElementEntry : asJsonObject.entrySet()) {
if (jsonElementEntry.getValue().isJsonObject() || jsonElementEntry.getValue().isJsonArray()) {
trimJson(jsonElementEntry.getValue());
} else if (shouldTrimElement(jsonElementEntry.getValue())) {
toRemove.add(jsonElementEntry.getKey());
}
}
if (CollectionUtils.isNotEmpty(toRemove)) {
for (String remove : toRemove) {
asJsonObject.remove(remove);
}
}
} else if (jsonElement.isJsonArray()) {
JsonArray asJsonArray = jsonElement.getAsJsonArray();
for (JsonElement element : asJsonArray) {
trimJson(element);
}
}
}
}