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

The following examples show how to use com.google.gson.GsonBuilder#setExclusionStrategies() . 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: JobDescriptor.java    From xml-job-to-job-dsl-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String toString() {
    GsonBuilder builder = new GsonBuilder();
    builder.setExclusionStrategies(new ExclusionStrategy() {

        @Override
        public boolean shouldSkipField(FieldAttributes fieldAttributes) {
            return fieldAttributes.getName().equals("parent");
        }

        @Override
        public boolean shouldSkipClass(Class<?> aClass) {
            return false;
        }
    });
    builder.setPrettyPrinting();
    return builder.create().toJson(this).replaceAll(Pattern.quote("\\r"), Matcher.quoteReplacement(""));
}
 
Example 2
Source File: PropertyDescriptor.java    From xml-job-to-job-dsl-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String toString() {
    GsonBuilder builder = new GsonBuilder();
    builder.setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes fieldAttributes) {
            return fieldAttributes.getName().equals("parent");
        }

        @Override
        public boolean shouldSkipClass(Class<?> aClass) {
            return false;
        }
    });
    return builder.create().toJson(this).replaceAll(Pattern.quote("\\r"), Matcher.quoteReplacement(""));
}
 
Example 3
Source File: WJsonUtils.java    From DAFramework with MIT License 6 votes vote down vote up
/**
 * 构建通用GsonBuilder, 封装初始化工作
 *
 * @return
 */
public static GsonBuilder getGsonBuilder(boolean prettyPrinting) {
	GsonBuilder gb = new GsonBuilder();
	gb.setDateFormat("yyyy-MM-dd HH:mm:ss:mss");
	gb.setExclusionStrategies(new ExclusionStrategy() {
		@Override
		public boolean shouldSkipField(FieldAttributes f) {
			return f.getAnnotation(WJsonExclued.class) != null;
		}

		@Override
		public boolean shouldSkipClass(Class<?> clazz) {
			return clazz.getAnnotation(WJsonExclued.class) != null;
		}
	});
	if (prettyPrinting)
		gb.setPrettyPrinting();
	return gb;
}
 
Example 4
Source File: WJsonUtils.java    From wES with MIT License 6 votes vote down vote up
/**
 * 构建通用GsonBuilder, 封装初始化工作
 *
 * @return
 */
public static GsonBuilder getGsonBuilder(boolean prettyPrinting) {
	GsonBuilder gb = new GsonBuilder();
	gb.setDateFormat("yyyy-MM-dd HH:mm:ss:mss");
	gb.setExclusionStrategies(new ExclusionStrategy() {
		@Override
		public boolean shouldSkipField(FieldAttributes f) {
			return f.getAnnotation(WJsonExclued.class) != null;
		}

		@Override
		public boolean shouldSkipClass(Class<?> clazz) {
			return clazz.getAnnotation(WJsonExclued.class) != null;
		}
	});
	if (prettyPrinting)
		gb.setPrettyPrinting();
	return gb;
}
 
Example 5
Source File: WGsonConvert.java    From wES with MIT License 6 votes vote down vote up
/**
 * 构建通用GsonBuilder, 封装初始化工作
 *
 * @return
 */
public static GsonBuilder getGsonBuilder(boolean prettyPrinting) {
	GsonBuilder gb = new GsonBuilder();
	gb.setDateFormat("yyyy-MM-dd HH:mm:ss:mss");
	gb.setExclusionStrategies(new ExclusionStrategy() {
		@Override
		public boolean shouldSkipField(FieldAttributes f) {
			return f.getAnnotation(WJsonExclued.class) != null;
		}

		@Override
		public boolean shouldSkipClass(Class<?> clazz) {
			return clazz.getAnnotation(WJsonExclued.class) != null;
		}
	});
	if (prettyPrinting)
		gb.setPrettyPrinting();
	return gb;
}
 
Example 6
Source File: CubaJavaScriptComponent.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected static GsonBuilder createSharedGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            Expose expose = f.getAnnotation(Expose.class);
            return expose != null && !expose.serialize();
        }

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

    setDefaultProperties(builder);
    return builder;
}
 
Example 7
Source File: GsonMessageBodyHandler.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Gson buildSerializer() {
	GsonBuilder builder = new GsonBuilder();
	builder.setDateFormat(API_JSON_DATETIME_PATTERN);
	builder.serializeNulls();
	builder.setExclusionStrategies(JsUtil.GSON_EXCLUSION_STRATEGIES);
	builder.registerTypeAdapter(NoShortcutSerializationWrapper.class, new JsonSerializer<NoShortcutSerializationWrapper<?>>() {

		@Override
		public JsonElement serialize(NoShortcutSerializationWrapper<?> src, Type typeOfSrc, JsonSerializationContext context) {
			return context.serialize(src.getVo());
		}
	});
	return builder.create();
}
 
Example 8
Source File: GsonMessageBodyHandler.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Gson buildShortcutSerializer() {
	GsonBuilder builder = new GsonBuilder();
	builder.setDateFormat(API_JSON_DATETIME_PATTERN);
	builder.serializeNulls();
	builder.setExclusionStrategies(JsUtil.GSON_EXCLUSION_STRATEGIES);
	builder.registerTypeAdapter(NoShortcutSerializationWrapper.class, new JsonSerializer<NoShortcutSerializationWrapper<?>>() {

		@Override
		public JsonElement serialize(NoShortcutSerializationWrapper<?> src, Type typeOfSrc, JsonSerializationContext context) {
			return serializer.toJsonTree(src.getVo()); // use regular serializer
		}
	});
	JsUtil.registerGsonTypeAdapters(builder, JsUtil.GSON_SHORTCUT_SERIALISATIONS);
	return builder.create();
}
 
Example 9
Source File: JsonResponse.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  GsonBuilder gsonBuilder = new GsonBuilder();
  if (pretty) {
    gsonBuilder.setPrettyPrinting();
  }
  gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
  Gson gson = gsonBuilder.create();
  return gson.toJson(this);
}
 
Example 10
Source File: JsonResponse.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  if (safeGson == null) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (pretty) {
      gsonBuilder.setPrettyPrinting();
    }
    gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());

    // Trick to get the DefaultDateTypeAdatpter instance
    // Create a first instance a Gson
    Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    // Get the date adapter
    TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);

    // Ensure the DateTypeAdapter is null safe
    TypeAdapter<Date> safeDateTypeAdapter = dateTypeAdapter.nullSafe();

    safeGson = new GsonBuilder()
        .registerTypeAdapter(Date.class, safeDateTypeAdapter)
        .registerTypeAdapter(ExperimentId.class, new ExperimentIdSerializer())
        .registerTypeAdapter(ExperimentId.class, new ExperimentIdDeserializer())
        .serializeNulls()
        .create();
  }

  boolean haveDictAnnotation = false;
  try {
    if (null != getResult()) {
      haveDictAnnotation = DictAnnotation.parseDictAnnotation(getResult());
    }
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
  }

  String json = safeGson.toJson(this);
  if (haveDictAnnotation) {
    json = json.replaceAll(CGLIB_PROPERTY_PREFIX, "");
  }

  return json;
}
 
Example 11
Source File: HttpKit.java    From UPMiss with GNU General Public License v3.0 4 votes vote down vote up
public static GsonBuilder getGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setExclusionStrategies(new SpecificClassExclusionStrategy(null, BaseModel.class));
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    return gsonBuilder;
}
 
Example 12
Source File: HttpKit.java    From UPMiss with GNU General Public License v3.0 4 votes vote down vote up
public static GsonBuilder getRspGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setExclusionStrategies(new SpecificClassExclusionStrategy(null, BaseModel.class));
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    return gsonBuilder;
}
 
Example 13
Source File: JsonHelperService.java    From JDeSurvey with GNU Affero General Public License v3.0 4 votes vote down vote up
public String serializeSurveyDefinition(SurveyDefinition surveyDefinition){
	try{
		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));
	}
	
}