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:org.eclipse.mylyn.internal.gerrit.core.client.JSonSupport.java

License:Open Source License

public JSonSupport() {
    TypeToken<Map<Id, PatchSetApproval>> approvalMapType = new TypeToken<Map<ApprovalCategory.Id, PatchSetApproval>>() {
    };//from  w w w  . ja va 2  s . c o m
    ExclusionStrategy exclustionStrategy = new ExclusionStrategy() {

        public boolean shouldSkipField(FieldAttributes f) {
            // commentLinks requires instantiation of com.google.gwtexpui.safehtml.client.RegexFindReplace which is not on classpath
            if (f.getDeclaredClass() == List.class && f.getName().equals("commentLinks") //$NON-NLS-1$
                    && f.getDeclaringClass() == GerritConfig.class) {
                return true;
            }
            if (f.getDeclaredClass() == Map.class && f.getName().equals("given")) { //$NON-NLS-1$
                //return true;
            }
            // GSon 2.1 fails to deserialize the SubmitType enum
            if (f.getDeclaringClass() == Project.class && f.getName().equals("submitType")) { //$NON-NLS-1$
                return true;
            }
            return false;
        }

        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };
    gson = JsonServlet.defaultGsonBuilder()
            .registerTypeAdapter(JSonResponse.class, new JSonResponseDeserializer())
            .registerTypeAdapter(Edit.class, new JsonDeserializer<Edit>() {
                public Edit deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    if (json.isJsonArray()) {
                        JsonArray array = json.getAsJsonArray();
                        if (array.size() == 4) {
                            return new Edit(array.get(0).getAsInt(), array.get(1).getAsInt(),
                                    array.get(2).getAsInt(), array.get(3).getAsInt());
                        }
                    }
                    return new Edit(0, 0);
                }
            })
            // ignore GerritForge specific AuthType "TEAMFORGE" which is unknown to Gerrit
            .registerTypeAdapter(AuthType.class, new JsonDeserializer<AuthType>() {

                @Override
                public AuthType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    String jsonString = json.getAsString();
                    if (jsonString != null) {
                        try {
                            return AuthType.valueOf(jsonString);
                        } catch (IllegalArgumentException e) {
                            // ignore the error since the connector does not make use of AuthType
                            //GerritCorePlugin.logWarning("Ignoring unkown authentication type: " + jsonString, e);
                        }
                    }
                    return null;
                }
            })
            .registerTypeAdapter(approvalMapType.getType(), new JsonDeserializer<Map<Id, PatchSetApproval>>() {

                @Override
                public Map<Id, PatchSetApproval> deserialize(JsonElement json, Type typeOfT,
                        JsonDeserializationContext context) throws JsonParseException {
                    // Gerrit 2.2: the type of PatchSetPublishDetail.given changed from a map to a list
                    Map<Id, PatchSetApproval> map = new HashMap<ApprovalCategory.Id, PatchSetApproval>();
                    if (json.isJsonArray()) {
                        JsonArray array = json.getAsJsonArray();
                        for (Iterator<JsonElement> it = array.iterator(); it.hasNext();) {
                            JsonElement element = it.next();
                            Id key = context.deserialize(element, Id.class);
                            if (key.get() != null) {
                                // Gerrit < 2.1.x: json is map
                                element = it.next();
                            }
                            PatchSetApproval value = context.deserialize(element, PatchSetApproval.class);
                            if (key.get() == null) {
                                // Gerrit 2.2: json is a list, deduct key from value
                                key = value.getCategoryId();
                            }
                            map.put(key, value);
                        }
                    }
                    return map;
                }
            }).setExclusionStrategies(exclustionStrategy).create();
}

From source file:org.jboss.aerogear.android.impl.unifiedpush.AeroGearGCMPushRegistrar.java

License:Apache License

@Override
public void register(final Context context, final Callback<Void> callback) {
    new AsyncTask<Void, Void, Exception>() {

        @Override/*from w ww  . j  a v  a  2 s  . c  om*/
        protected Exception doInBackground(Void... params) {

            try {

                if (gcm == null) {
                    gcm = gcmProvider.get(context);
                }
                String regid = getRegistrationId(context);

                if (regid.length() == 0) {
                    regid = gcm.register(config.senderIds.toArray(new String[] {}));
                    AeroGearGCMPushRegistrar.this.setRegistrationId(context, regid);
                }

                config.setDeviceToken(regid);

                URL deviceRegistryURL = UrlUtils.appendToBaseURL(pushServerURI.toURL(), registryDeviceEndpoint);
                HttpRestProviderForPush httpProvider = httpProviderProvider.get(deviceRegistryURL, TIMEOUT);
                httpProvider.setPasswordAuthentication(config.getVariantID(), config.getSecret());

                Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
                    private final ImmutableSet<String> fields;

                    {
                        fields = ImmutableSet.<String>builder().add("deviceToken").add("deviceType")
                                .add("alias").add("operatingSystem").add("osVersion").add("categories").build();
                    }

                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return !(f.getDeclaringClass() == PushConfig.class && fields.contains(f.getName()));
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> arg0) {
                        return false;
                    }
                }).create();
                try {
                    httpProvider.post(gson.toJson(config));
                    return null;
                } catch (HttpException ex) {
                    return ex;
                }

            } catch (Exception ex) {
                return ex;
            }

        }

        @SuppressWarnings("unchecked")
        @Override
        protected void onPostExecute(Exception result) {
            if (result == null) {
                callback.onSuccess(null);
            } else {
                callback.onFailure(result);
            }
        };

    }.execute((Void) null);

}

From source file:ru.payqr.dadataexample.rest.DaDataRestClient.java

License:Open Source License

private DaDataRestClient() {
    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
        @Override//w w  w.j a  va2s  . c om
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getDeclaringClass().equals(RealmObject.class);
        }

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

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
            .setEndpoint(BASE_URL).setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("Content-Type", "application/json");
                    request.addHeader("Accept", "application/json");
                    request.addHeader("Authorization", "Token ".concat(BuildConfig.DADATA_API_KEY));
                }
            }).setConverter(new GsonConverter(gson)).build();

    apiService = restAdapter.create(DaDataService.class);
}

From source file:ru.payqr.kladapiexample.rest.KladrRestClient.java

License:Open Source License

private KladrRestClient() {
    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
        @Override//  w  w  w  .  j a v a  2s  .  c o  m
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getDeclaringClass().equals(RealmObject.class);
        }

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

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
            .setEndpoint(BASE_URL).setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addQueryParam("token", BuildConfig.KLADR_TOKEN_KEY);
                    request.addQueryParam("key", BuildConfig.KLADR_API_KEY);
                }
            }).setConverter(new GsonConverter(gson)).build();

    apiService = restAdapter.create(KladrService.class);
}