我有下面这3种方法。我只是不知道将它们作为一个整体,因为它们是数组、列表或只是一个对象的类型。这些方法只是将给定对象转换为 json 字符串,然后将其转换为给定的指定类。
public static List<Object> objectListSerializer(List<Document> documents, Class entity) {
List<Object> entityList;
String json = com.mongodb.util.JSON.serialize(documents);
entityList = (List<Object>) GSON_INSTANCE.fromJson(json, entity);
return entityList;
}
public static Object objectArraySerializer(Object objectArray, Class clazz) {
String jsonString = GSON_INSTANCE.toJson(objectArray);
Object convert[] = (Object[]) GSON_INSTANCE.fromJson(jsonString, clazz);
return convert;
}
public static Object objectSerializer(Object object, Class clazz) {
String jsonString = GSON_INSTANCE.toJson(object);
Object convert = GSON_INSTANCE.fromJson(jsonString, clazz);
return convert;
}