Example usage for com.google.gson FieldAttributes getDeclaringClass

List of usage examples for com.google.gson FieldAttributes getDeclaringClass

Introduction

In this page you can find the example usage for com.google.gson FieldAttributes getDeclaringClass.

Prototype

public Class<?> getDeclaringClass() 

Source Link

Usage

From source file:br.com.caelum.vraptor.serialization.gson.Exclusions.java

License:Open Source License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    SkipSerialization annotation = f.getAnnotation(SkipSerialization.class);
    if (annotation != null)
        return true;

    String fieldName = f.getName();
    Class<?> definedIn = f.getDeclaringClass();

    for (Entry<String, Class<?>> include : serializee.getIncludes().entries()) {
        if (isCompatiblePath(include, definedIn, fieldName)) {
            return false;
        }//  www  .ja  va  2  s.co m
    }
    for (Entry<String, Class<?>> exclude : serializee.getExcludes().entries()) {
        if (isCompatiblePath(exclude, definedIn, fieldName)) {
            return true;
        }
    }

    Field field = reflectionProvider.getField(definedIn, fieldName);
    return !serializee.isRecursive() && !shouldSerializeField(field.getType());
}

From source file:cn.teamlab.wg.framework.struts2.json.JsonSerializerExclusionStrategy.java

License:Apache License

private String className(FieldAttributes f) {
    String declaringClass = f.getDeclaringClass().toString();
    if (declaringClass.indexOf('$') != -1) { // Inner class
        int index = declaringClass.lastIndexOf('$');
        return declaringClass.substring(index + 1, declaringClass.length());
    } else if (declaringClass.indexOf('.') != -1) { // Public class
        int index = declaringClass.lastIndexOf('.');
        return declaringClass.substring(index + 1, declaringClass.length());
    }//  w  ww.j a va  2s . c o  m

    return declaringClass;
}

From source file:com.camino.lib.provider.network.ServiceGenerator.java

License:Apache License

/**
 * A factory method to create a service instance with authorization
 *///from  ww w  .  ja  v a2 s  . co m
public static <S> S createService(String baseUrl, Class<S> serviceClass, final String authToken) {
    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
            //                .connectionPool(new ConnectionPool(MAX_CONNECTIONS, 5, TimeUnit.MINUTES))
            //                .addInterceptor(new StethoInterceptor())
            //                .sslSocketFactory(SSLConfig.getSSLSocketFactory())
            .readTimeout(READ_TIMEOUT_SEC, TimeUnit.SECONDS);

    if (authToken != null) {
        httpClientBuilder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Authorization", "Bearer " + authToken)
                        .method(original.method(), original.body());

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    }

    OkHttpClient client = httpClientBuilder.build();

    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getDeclaringClass().equals(RealmObject.class);
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    }).create();

    Retrofit.Builder builder = new Retrofit.Builder().baseUrl(baseUrl)
            .callbackExecutor(Executors.newCachedThreadPool())
            .addConverterFactory(GsonConverterFactory.create(gson));

    Retrofit retrofit = builder.client(client).build();

    return retrofit.create(serviceClass);
}

From source file:com.division.jsonrpc.api.SuperclassExclusionStrategy.java

License:Apache License

public boolean shouldSkipField(FieldAttributes fieldAttributes) {
    String fieldName = fieldAttributes.getName();
    Class<?> theClass = fieldAttributes.getDeclaringClass();

    return isFieldInSuperclass(theClass, fieldName);
}

From source file:com.gilecode.yagson.strategy.ExcludeFieldsByDeclaringClasses.java

License:Apache License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    return skipDeclaringClasses.contains(f.getDeclaringClass());
}

From source file:com.gilecode.yagson.strategy.ExcludeFieldsInClassesByNames.java

License:Apache License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    if (skipFieldNames.contains(f.getName())) {
        for (Class<?> declaringSuperClass : declaringSuperClasses) {
            if (declaringSuperClass != null && declaringSuperClass.isAssignableFrom(f.getDeclaringClass())) {
                return true;
            }/*  ww  w  .  j a  v a 2 s  .  c o m*/
        }
    }
    return false;
}

From source file:com.google.gerrit.httpd.restapi.RestApiServlet.java

License:Apache License

private static void enablePartialGetFields(GsonBuilder gb, Multimap<String, String> config) {
    final Set<String> want = Sets.newHashSet();
    for (String p : config.get("fields")) {
        Iterables.addAll(want, OptionUtil.splitOptionValue(p));
    }//from ww  w. j a  v  a2 s.  c  om
    if (!want.isEmpty()) {
        gb.addSerializationExclusionStrategy(new ExclusionStrategy() {
            private final Map<String, String> names = Maps.newHashMap();

            @Override
            public boolean shouldSkipField(FieldAttributes field) {
                String name = names.get(field.getName());
                if (name == null) {
                    // Names are supplied by Gson in terms of Java source.
                    // Translate and cache the JSON lower_case_style used.
                    try {
                        name = FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES.translateName(//
                                field.getDeclaringClass().getDeclaredField(field.getName()));
                        names.put(field.getName(), name);
                    } catch (SecurityException e) {
                        return true;
                    } catch (NoSuchFieldException e) {
                        return true;
                    }
                }
                return !want.contains(name);
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        });
    }
}

From source file:com.hmatalonga.greenhub.util.GsonRealmBuilder.java

License:Apache License

private static GsonBuilder getBuilder() {
    return new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
        @Override/* w w w.j a  v  a 2  s  .co m*/
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getDeclaringClass().equals(RealmObject.class);
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    });
}

From source file:com.ilearnrw.reader.types.LogBasicExclusionStrategy.java

License:Open Source License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    if (f.getDeclaringClass() == c) {
        for (int i = 0; i < fieldNames.size(); i++) {
            if (f.getName().equals(fieldNames.get(i)))
                return true;
        }/*from w ww  . j  a v a  2s .c  o  m*/
    }
    return false;
}

From source file:com.jd.survey.service.util.JsonHelperService.java

License:Open Source License

public String serializeSurveyDefinition(SurveyDefinition surveyDefinition) {
    try {//from w  w  w .  j  av  a 2 s . c o  m
        GsonBuilder gsonBuilder = new GsonBuilder();
        //set up the fields to skip in the serialization
        gsonBuilder = gsonBuilder.setExclusionStrategies(new ExclusionStrategy() {
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }

            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                boolean skip = (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("department"))
                        || (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("users"))
                        || (f.getDeclaringClass() == SurveyDefinitionPage.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == SurveyDefinitionPage.class
                                && f.getName().equals("surveyDefinition"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("page"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("optionsList"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("rowLabelsList"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("columnLabelsList"))
                        || (f.getDeclaringClass() == QuestionOption.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == QuestionOption.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == QuestionOption.class && f.getName().equals("question"))
                        || (f.getDeclaringClass() == QuestionRowLabel.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == QuestionRowLabel.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == QuestionRowLabel.class && f.getName().equals("question"))
                        || (f.getDeclaringClass() == QuestionColumnLabel.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == QuestionColumnLabel.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == QuestionColumnLabel.class
                                && f.getName().equals("question"));
                return skip;
            }

        });

        //de-proxy the object
        gsonBuilder.registerTypeHierarchyAdapter(HibernateProxy.class, new HibernateProxySerializer());
        Hibernate.initialize(surveyDefinition);
        if (surveyDefinition instanceof HibernateProxy) {
            surveyDefinition = (SurveyDefinition) ((HibernateProxy) surveyDefinition)
                    .getHibernateLazyInitializer().getImplementation();
        }
        Gson gson = gsonBuilder.serializeNulls().create();
        return gson.toJson(surveyDefinition);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}