Java Code Examples for com.google.gson.GsonBuilder#excludeFieldsWithoutExposeAnnotation()

The following examples show how to use com.google.gson.GsonBuilder#excludeFieldsWithoutExposeAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Articles.java    From NoraUi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void deserialize(String jsonString) {
    Type listType = new TypeToken<ArrayList<Article>>() {
    }.getType();

    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();

    List<Article> list = gson.fromJson(jsonString, listType);
    for (int i = 0; i < list.size(); i++) {
        list.get(i).setNid(i + 1);
    }
    this.addAll(list);
}
 
Example 2
Source File: NearbyServlet.java    From nearbydemo with Eclipse Public License 1.0 6 votes vote down vote up
protected void setSuccess(HttpServletResponse resp, Object obj)
		throws IOException {

	GsonBuilder builder = new GsonBuilder();
	// 不转换没有 @Expose 注解的字段
	builder.excludeFieldsWithoutExposeAnnotation();
	Gson gson = builder.create();

	String json = gson.toJson(obj);

	resp.setStatus(HttpServletResponse.SC_OK);
	resp.setContentType("application/json");
	resp.setCharacterEncoding("UTF-8");
	PrintWriter out = resp.getWriter();
	out.print(json);
	out.flush();
	out.close();

}
 
Example 3
Source File: UserServlet.java    From nearbydemo with Eclipse Public License 1.0 6 votes vote down vote up
protected void setSuccess(HttpServletResponse resp, Object obj)
		throws IOException {

	GsonBuilder builder = new GsonBuilder();
	// 不转换没有 @Expose 注解的字段
	builder.excludeFieldsWithoutExposeAnnotation();
	Gson gson = builder.create();

	String json = gson.toJson(obj);

	resp.setStatus(HttpServletResponse.SC_OK);
	resp.setContentType("application/json");
	resp.setCharacterEncoding("UTF-8");
	PrintWriter out = resp.getWriter();
	out.print(json);
	out.flush();
	out.close();

}
 
Example 4
Source File: Article.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void deserialize(String jsonString) {
    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();
    Article w = gson.fromJson(jsonString, Article.class);
    this.nid = w.nid;
    this.title = w.title;
    this.text = w.text;
    this.author = w.author;
    this.note = w.note;
}
 
Example 5
Source File: CommonModels.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String serialize() {
    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    builder.disableHtmlEscaping();
    final Gson gson = builder.create();
    return gson.toJson(this);
}
 
Example 6
Source File: CommonModel.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String serialize() {
    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    builder.disableHtmlEscaping();
    final Gson gson = builder.create();
    return gson.toJson(this);
}
 
Example 7
Source File: VoxelGamesLibModule.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
@Provides
@Nonnull
public Gson getGson(@Nonnull Injector injector) {
    GsonBuilder builder = new GsonBuilder();
    addTypeAdapters(builder, injector);
    builder.setPrettyPrinting();
    builder.excludeFieldsWithoutExposeAnnotation();
    return builder.create();
}
 
Example 8
Source File: JSONUtil.java    From FoxBPM with Apache License 2.0 5 votes vote down vote up
private 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 (StringUtil.isEmpty(datePattern))
	// datePattern = DEFAULT_DATE_PATTERN;
	builder.setDateFormat(datePattern);
	if (excludesFieldsWithoutExpose)
		builder.excludeFieldsWithoutExposeAnnotation();
	String result = null;
	Gson gson = builder.create();
	try {
		if (targetType != null) {
			result = gson.toJson(target, targetType);
		} else {
			result = gson.toJson(target);
		}
	} catch (Exception ex) {
		if (target instanceof Collection || target instanceof Iterator || target instanceof Enumeration || target.getClass().isArray()) {
			result = EMPTY_JSON_ARRAY;
		} else
			result = EMPTY_JSON;
	}
	return result;
}
 
Example 9
Source File: GsonCodec.java    From gocd with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 10
Source File: JSONUtil.java    From fixflow with Apache License 2.0 5 votes vote down vote up
private 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 (StringUtil.isEmpty(datePattern))   
//            datePattern = DEFAULT_DATE_PATTERN;   
        builder.setDateFormat(datePattern);   
        if (excludesFieldsWithoutExpose)   
            builder.excludeFieldsWithoutExposeAnnotation();   
        String result = null;   
        Gson gson = builder.create();   
        try {   
            if (targetType != null) {   
                result = gson.toJson(target, targetType);   
            } else {   
                result = gson.toJson(target);   
            }   
        } catch (Exception ex) {      
            if (target instanceof Collection || target instanceof Iterator   
                    || target instanceof Enumeration || target.getClass().isArray()) {   
                result = EMPTY_JSON_ARRAY;   
            } else  
                result = EMPTY_JSON;   
        }   
        return result;   
    }
 
Example 11
Source File: JsonUtil.java    From AndroidRobot with Apache License 2.0 4 votes vote down vote up
/**
 * 将给定的目标对象根据指定的条件参数转换成 {@code JSON} 格式的字符串。
 * <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} 格式的字符串。
 */
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 (isEmpty(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }

    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose) {
        builder.excludeFieldsWithoutExposeAnnotation();
    }

    String result = "";

    Gson gson = builder.create();

    try {
        if (targetType != null) {
            result = gson.toJson(target, targetType);
        } else {
            result = gson.toJson(target);
        }
    } catch (Exception ex) {
        if (target instanceof Collection || target instanceof Iterator
            || target instanceof Enumeration || target.getClass().isArray()) {
            result = EMPTY_JSON_ARRAY;
        } else {
            result = EMPTY_JSON;
        }

    }

    return result;
}