i m trying to save an object to a file as json string ! but when i try to do it without ExclusionStrategy for GsonBuilder it gives me an "duplicated name in Json Exception field hashCode_ in my class" ! i search in google and found a solution that i should use ExclusionStrategy and skip some feilds ! so i did that and i got Stack overflow exception this time ! i dont know what to do and there seems to be no answers to this question on Internet !
this is my convertToJsonString Method :
public static <E> String convertToJsonString(E e) {
ExclusionStrategy strategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
if(f.getName().equals("hashCode_"))
return true;
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
};
gson=new GsonBuilder().setExclusionStrategies(strategy).create();
return gson.toJson(e);
}
this is where i make JsonElement
public static <E> JsonElement createJsonElement(E e) {
return gson.toJsonTree(e);
}
and this is where i save my jsonString to a file :
public void writeToFileHotel(Hotel hotel) {
JsonElement element= JsonUtils.createJsonElement(hotel);
JsonUtils.writeJsonToFile(element,allGiataHotels_File);
}
this is my exception without ExclusionStrategy
Exception in thread "main" java.lang.IllegalArgumentException: class
ir.viratech.tickbed.model.user.AgencyUser declares multiple JSON fields
named hashCode_
this is my exception with ExclusionStrategy
Exception in thread "main" java.lang.StackOverflowError
note : IllegalArgumentException exception accured on JsonElement element= JsonUtils.createJsonElement(hotel) line