Java tutorial
/* * This file is part of JuniperBotJ. * * JuniperBotJ is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * JuniperBotJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with JuniperBotJ. If not, see <http://www.gnu.org/licenses/>. */ package ru.caramel.juniperbot.core.utils; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.lang.reflect.Field; public final class GsonUtils { private GsonUtils() { // private } public static Gson create() { GsonBuilder builder = new GsonBuilder(); builder.addDeserializationExclusionStrategy(new SuperclassExclusionStrategy()); builder.addSerializationExclusionStrategy(new SuperclassExclusionStrategy()); return builder.create(); } public static class SuperclassExclusionStrategy implements ExclusionStrategy { public boolean shouldSkipClass(Class<?> arg0) { return false; } public boolean shouldSkipField(FieldAttributes fieldAttributes) { String fieldName = fieldAttributes.getName(); Class<?> theClass = fieldAttributes.getDeclaringClass(); return isFieldInSuperclass(theClass, fieldName); } private boolean isFieldInSuperclass(Class<?> subclass, String fieldName) { Class<?> superclass = subclass.getSuperclass(); Field field; while (superclass != null) { field = getField(superclass, fieldName); if (field != null) return true; superclass = superclass.getSuperclass(); } return false; } private Field getField(Class<?> theClass, String fieldName) { try { return theClass.getDeclaredField(fieldName); } catch (Exception e) { return null; } } } }