Example usage for com.google.gson GsonBuilder excludeFieldsWithoutExposeAnnotation

List of usage examples for com.google.gson GsonBuilder excludeFieldsWithoutExposeAnnotation

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder excludeFieldsWithoutExposeAnnotation.

Prototype

public GsonBuilder excludeFieldsWithoutExposeAnnotation() 

Source Link

Document

Configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the com.google.gson.annotations.Expose annotation.

Usage

From source file:com.goplace.service.generic.implementation.ViewServiceGenImpl.java

License:Open Source License

@Override
public String getPage(int intRegsPerPag, int intPage, ArrayList<FilterBeanHelper> alFilter,
        HashMap<String, String> hmOrder) throws Exception {
    String data = null;/*from   w  w w.  ja  va2 s  .c  o  m*/
    try {
        oConnection.setAutoCommit(false);
        BeanGenImpl oGenericBean = (BeanGenImpl) Class
                .forName("com.goplace.bean.generic.specific.implementation." + strPojo + "BeanGenSpImpl")
                .newInstance();
        Constructor c = Class
                .forName("com.goplace.dao.generic.specific.implementation." + strPojo + "DaoGenSpImpl")
                .getConstructor(String.class, Connection.class);
        TableDaoGenImpl oGenericDao = (TableDaoGenImpl) c.newInstance(strObjectName, oConnection);
        List<BeanInterface> loGenericBean = oGenericDao.getPage(intRegsPerPag, intPage, alFilter, hmOrder);
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("dd/MM/yyyy");
        Gson gson = gsonBuilder.excludeFieldsWithoutExposeAnnotation().create();
        data = gson.toJson(loGenericBean);
        data = "{\"list\":" + data + "}";
    } catch (Exception ex) {
        oConnection.rollback();
        ExceptionBooster.boost(new Exception(this.getClass().getName() + ":getPage ERROR: " + ex.getMessage()));
    } finally {
        oConnection.commit();
    }
    return data;
}

From source file:com.hmwg.utils.GSONUtils.java

License:Apache License

/**
 * ????? {@code JSON} ?// w w  w .j a va 2s  .  c  o  m
 * <p />
 * <strong>???? <code>"{}"</code> ?
 * <code>"[]"</code> </strong>
 * 
 * @param target
 *            
 * @param targetType
 *            
 * @param isSerializeNulls
 *            ?? {@code null} 
 * @param version
 *            ?
 * @param datePattern
 *            ??
 * @param excludesFieldsWithoutExpose
 *            ? {@literal @Expose} 
 * @return  {@code JSON} ?
 * @since 1.0
 */
public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null) {
        return EMPTY_JSON;
    }
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls) {
        builder.serializeNulls();
    }
    if (version != null) {
        builder.setVersion(version.doubleValue());
    }
    if (isBlankString(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose) {
        builder.excludeFieldsWithoutExposeAnnotation();
    }
    return toJson(target, targetType, builder);
}

From source file:com.hybris.datahub.outbound.utils.CommonUtils.java

License:Open Source License

/**
 * @param whetherExposeAnnotation/*www .j ava 2 s  .c om*/
 * @param dateFormat
 * @return Gson instance
 */
public static Gson getGsonByBuilder(final boolean whetherExposeAnnotation, String dateFormat) {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    if (whetherExposeAnnotation) {
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    }

    gsonBuilder.enableComplexMapKeySerialization();

    if (StringUtils.isEmpty(dateFormat)) {
        dateFormat = "yyyy-MM-dd HH:mm:ss";
    }
    gsonBuilder.serializeNulls().setDateFormat(dateFormat);
    gsonBuilder.setVersion(1.0);
    //      gsonBuilder.disableHtmlEscaping();
    return gsonBuilder.create();
}

From source file:com.lib.lapp.net.utils.JSONUtils.java

License:Apache License

/**
 * ????? {@code JSON} ?//from w w  w.  ja v  a 2  s. co m
 * <p/>
 * <strong>???? <code>"{}"</code> ?
 * <code>"[]"</code> </strong>
 *
 * @param target                      
 * @param targetType                  
 * @param isSerializeNulls            ?? {@code null} 
 * @param version                     ?
 * @param datePattern                 ??
 * @param excludesFieldsWithoutExpose ? {@literal @Expose} 
 * @return  {@code JSON} ?
 * @since 1.0
 */
public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null)
        return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls)
        builder.serializeNulls();
    if (version != null)
        builder.setVersion(version.doubleValue());
    if (TextUtils.isEmpty(datePattern))
        datePattern = DEFAULT_DATE_PATTERN;
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    return toJson(target, targetType, builder);
}

From source file:com.logisticsShop.utils.JsonUtil.java

License:Apache License

/**
 * ????? {@code JSON} ??//from   ww  w .j ava  2  s . co  m
 * <p />
 * <b>??????<code>"{}"</code>???<code>"[]"</code> </b>
 * 
 * @param target
 *            ?
 * @param targetType
 *            ?
 * @param isSerializeNulls
 *            ???{@code null} ?
 * @param version
 *            ??
 * @param datePattern
 *            ???
 * @param excludesFieldsWithoutExpose
 *            ??{@literal @Expose} ?
 * @return ?{@code JSON} ??
 * @since 1.0
 */
public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null)
        return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls)
        builder.serializeNulls();
    if (version != null)
        builder.setVersion(version.doubleValue());
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    return toJson(target, targetType, builder);
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServicePush.java

License:Open Source License

/**
 * Updates a registration/*from  ww w .ja v  a  2s . c o  m*/
 * 
 * @param registration
 *            The registration to update
 * @param callback
 *            The operation callback
 */
private void upsertRegistrationInternal(final Registration registration,
        final UpsertRegistrationInternalCallback callback) {

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.excludeFieldsWithoutExposeAnnotation();

    Gson gson = gsonBuilder.create();

    String resource = registration.getURI();
    JsonElement json = gson.toJsonTree(registration);
    String body = json.toString();
    byte[] content = null;

    try {
        content = body.getBytes(MobileServiceClient.UTF8_ENCODING);
    } catch (UnsupportedEncodingException e) {
        callback.onUpsert(registration, e);
        return;
    }

    List<Pair<String, String>> requestHeaders = new ArrayList<Pair<String, String>>();

    requestHeaders.add(new Pair<String, String>(HTTP.CONTENT_TYPE, MobileServiceConnection.JSON_CONTENTTYPE));

    mClient.invokeApiInternal(resource, content, "PUT", requestHeaders, null, MobileServiceClient.PNS_API_URL,
            new ServiceFilterResponseCallback() {

                @Override
                public void onResponse(ServiceFilterResponse response, Exception exception) {
                    if (exception != null) {
                        if (response.getStatus().getStatusCode() == 410) {
                            exception = new RegistrationGoneException(exception);
                        }

                        callback.onUpsert(registration, exception);

                        return;
                    } else {
                        callback.onUpsert(registration, null);

                        return;
                    }
                }
            });
}

From source file:com.microsoft.windowsazure.mobileservices.notifications.MobileServicePush.java

License:Open Source License

/**
 * Updates a registration//from  w  w  w.  ja v  a2s.com
 *
 * @param registration The registration to update
 * @param callback     The operation callback
 * @throws java.io.UnsupportedEncodingException
 * @throws Exception
 */
private ListenableFuture<Void> upsertRegistrationInternal(final Registration registration) {

    final SettableFuture<Void> resultFuture = SettableFuture.create();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.excludeFieldsWithoutExposeAnnotation();

    Gson gson = gsonBuilder.create();

    String resource = registration.getURI();
    String path = PNS_API_URL + resource;
    JsonElement json = gson.toJsonTree(registration);
    String body = json.toString();

    byte[] content;
    try {
        content = body.getBytes(MobileServiceClient.UTF8_ENCODING);
    } catch (UnsupportedEncodingException e) {
        resultFuture.setException(e);
        return resultFuture;
    }

    List<Pair<String, String>> requestHeaders = new ArrayList<Pair<String, String>>();

    requestHeaders.add(new Pair<String, String>(HTTP.CONTENT_TYPE, MobileServiceConnection.JSON_CONTENTTYPE));

    ListenableFuture<ServiceFilterResponse> serviceFilterFuture = mHttpClient.request(path, content, "PUT",
            requestHeaders, null);

    Futures.addCallback(serviceFilterFuture, new FutureCallback<ServiceFilterResponse>() {
        @Override
        public void onFailure(Throwable exception) {
            resultFuture.setException(exception);
        }

        @Override
        public void onSuccess(ServiceFilterResponse response) {
            resultFuture.set(null);
        }
    });

    return resultFuture;
}

From source file:com.microsoft.windowsazure.mobileservices.notifications.PnsSpecificRegistrationFactory.java

License:Open Source License

/**
 * Parses a native registration according the PNS supported on device
 *
 * @param registrationJson The Json representation of the registration
 *//*from   www . j  a  v  a  2  s  .c o m*/
public Registration parseNativeRegistration(JsonObject registrationJson) {
    GsonBuilder builder = new GsonBuilder();
    builder = builder.excludeFieldsWithoutExposeAnnotation();
    Gson gson = builder.create();
    Class<? extends Registration> clazz = GcmNativeRegistration.class;
    Registration registration = gson.fromJson(registrationJson, clazz);

    registration.setName(Registration.DEFAULT_REGISTRATION_NAME);

    return registration;
}

From source file:com.microsoft.windowsazure.mobileservices.notifications.PnsSpecificRegistrationFactory.java

License:Open Source License

/**
 * Parses a template registration according the PNS supported on device
 *
 * @param registrationJson The Json representation of the registration
 *///from w w w  .  j a  v  a  2s  . c o m
public TemplateRegistration parseTemplateRegistration(JsonObject registrationJson) {
    GsonBuilder builder = new GsonBuilder();
    builder = builder.excludeFieldsWithoutExposeAnnotation();
    Gson gson = builder.create();
    Class<? extends TemplateRegistration> clazz = GcmTemplateRegistration.class;
    return gson.fromJson(registrationJson, clazz);
}

From source file:com.paysafe.common.impl.AddressContainerAdapter.java

License:Open Source License

/**
 * Instantiates a new address container adapter.
 *///from  w  ww.j  a  v a2s. c om
public AddressContainerAdapter() {
    final GsonBuilder gsonSerializerBuilder = new GsonBuilder();
    gsonSerializerBuilder.excludeFieldsWithoutExposeAnnotation();
    gsonSerializerBuilder.registerTypeHierarchyAdapter(Id.class, new IdAdapter());
    gsonSerializerBuilder.registerTypeHierarchyAdapter(Boolean.class, new BooleanAdapter());
    gsonSerializer = gsonSerializerBuilder.create();

    final GsonBuilder gsonDeserializerBuilder = new GsonBuilder();
    gsonDeserializerBuilder.registerTypeHierarchyAdapter(Id.class, new IdAdapter());
    gsonDeserializer = gsonDeserializerBuilder.create();
}