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.thoughtworks.go.plugin.configrepo.codec.GsonCodec.java

License:Apache License

public GsonCodec(GsonBuilder builder) {
    // here we can register extra configurations, policies, adapters
    builder.registerTypeAdapter(CRMaterial.class, new MaterialTypeAdapter());
    builder.registerTypeAdapter(CRTask.class, new TaskTypeAdapter());
    builder.registerTypeAdapter(CRArtifact.class, new ArtifactTypeAdapter());
    builder.excludeFieldsWithoutExposeAnnotation();

    gson = builder.create();/*from  w w w  .  j  ava  2  s  . co  m*/
}

From source file:com.thoughtworks.go.util.json.JsonHelper.java

License:Apache License

public static <T> T safeFromJson(final String jsonString, final Class<T> clazz) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.excludeFieldsWithoutExposeAnnotation().create();
    try {/*from   w w w  .ja  v  a  2 s  . co  m*/
        return gson.fromJson(jsonString, clazz);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.udeyrishi.androidelasticsearchdatamanager.JsonFormatter.java

License:Apache License

/**
 * Gets the {@link Gson} object configured with the settings of this {@link JsonFormatter}.
 *
 * @return The properly configured {@link Gson}.
 *//*from  ww  w  .  j  av  a2s. c om*/
public Gson getGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();

    if (getUsePrettyJson()) {
        gsonBuilder.setPrettyPrinting();
    }

    if (getUseExplicitExposeAnnotation()) {
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    }

    // Register custom serializers
    for (Map.Entry<Class<?>, JsonSerializer<?>> entry : serializers.entrySet()) {
        gsonBuilder.registerTypeHierarchyAdapter(entry.getKey(), entry.getValue());
    }

    for (Map.Entry<Class<?>, JsonDeserializer<?>> entry : deserializers.entrySet()) {
        gsonBuilder.registerTypeHierarchyAdapter(entry.getKey(), entry.getValue());
    }

    return gsonBuilder.create();
}

From source file:com.yattatech.dbtc.facade.SystemFacade.java

License:Open Source License

public BackupInputStream getBackupInputStream() throws IOException {
    Debug.d(TAG, "getBackupInputStream");
    final List<Task> tasks = getTasksWithChecks();
    final TaskList taskList = new TaskList();
    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();
    final Type type = new TypeToken<List<Task>>() {
    }.getType();/*from  w w  w .  j a v a 2  s. co m*/
    final String json = gson.toJson(tasks, type);
    taskList.mChecksum = calculateChecksum(json);
    taskList.mTasks = tasks;
    final String json2 = gson.toJson(taskList);
    final BackupInputStream in = new BackupInputStream(IOUtils.toInputStream(json2, "UTF-8"));
    in.mLength = json2.length();
    if (Debug.isDebugable()) {
        Debug.d(TAG, "tasks=" + tasks.size() + " json=" + json + " length=" + in.mLength);
    }
    return in;
}

From source file:com.yattatech.dbtc.facade.SystemFacade.java

License:Open Source License

public List<Task> getTasksFromJson(final String json) {
    if (Debug.isDebugable()) {
        Debug.d(TAG, "getTasksFromJson json=" + json);
    }//  w w w  .  j  a  v a2s .  co  m
    final Type type = new TypeToken<List<Task>>() {
    }.getType();
    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();

    final TaskList taskList = gson.fromJson(json, TaskList.class);
    if (taskList != null) {
        final long checksum = taskList.mChecksum;
        final String json2 = gson.toJson(taskList.mTasks, type);
        if (isChecksumValid(checksum, json2)) {
            return taskList.mTasks;
        }
    }
    return null;
}

From source file:de.rwth.dbis.acis.bazaar.service.exception.ExceptionHandler.java

License:Apache License

public String toJSON(BazaarException exception) {
    GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();
    return gson.toJson(exception);
}

From source file:eg.agrimarket.controller.GetGategoriesController.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    CategoryDao categoryDaoImp = new CategoryDaoImp();
    GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    Gson gson = builder.create();/*from w w  w  .j a va  2 s  . c  om*/
    List<Category> categories = categoryDaoImp.getAllCategories();
    out.print(gson.toJson(categories));

}

From source file:eu.vranckaert.worktime.web.json.model.JsonEntity.java

License:Apache License

public String toJSON() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateTimeSerializer());
    builder.excludeFieldsWithoutExposeAnnotation();
    builder.setPrettyPrinting();/*  w w w.  j av  a2s. c om*/
    Gson gson = builder.create();

    String json = gson.toJson(this);
    return json;
}

From source file:fi.craplab.roameo.data.JsonExporter.java

License:Open Source License

private Gson createGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();/*from   w w  w.  ja v a2  s.co  m*/
    gsonBuilder.excludeFieldsWithoutExposeAnnotation();

    if (!mExportPhoneNumbers) {
        gsonBuilder.setExclusionStrategies(new PhoneNumberExclusionStrategy());
    }

    return gsonBuilder.create();
}

From source file:net.daw.helper.statics.AppConfigurationHelper.java

License:MIT License

public static Gson getGson() throws Exception {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("dd/MM/yyyy");
    Gson oGson = gsonBuilder.excludeFieldsWithoutExposeAnnotation().create();
    return oGson;
}