com.alibaba.fastjson.serializer.PropertyPreFilter Java Examples

The following examples show how to use com.alibaba.fastjson.serializer.PropertyPreFilter. 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: FastjsonHttpMessageConverter.java    From java-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {
	Charset charset = getCharset(outputMessage.getHeaders());
	OutputStreamWriter out = new OutputStreamWriter(outputMessage.getBody(), charset);
	SerializeWriter writer = new SerializeWriter(out);
	JSONSerializer serializer = new JSONSerializer(writer, this.serializeConfig);
	serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);

	RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
	if (requestAttributes instanceof ServletRequestAttributes) {
		Class<?> rootClass = filterClass(obj);
		if (rootClass != null) {
			String[] paths = ((ServletRequestAttributes) requestAttributes).getRequest()
					.getParameterValues("paths");
			if (paths != null && paths.length > 0) {
				rootClass = ClassUtils.getUserClass(rootClass);
				Map<Class<?>, PropertyPreFilter> propertyFilters = new HashMap<>();
				pathVisit(propertyFilters, Sets.newHashSet(paths), rootClass);

				for (Entry<Class<?>, PropertyPreFilter> entry : propertyFilters.entrySet()) {
					serializer.getPropertyPreFilters().add(entry.getValue());
				}
			} else {

				serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER);
			}
		} else {
			serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER);
		}
	}
	serializer.write(obj);
	writer.flush();
	out.close();
}
 
Example #2
Source File: FastJsonProvider.java    From metrics with Apache License 2.0 4 votes vote down vote up
public String toJSONString(Object object, SerializeFilter filter, SerializerFeature[] features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out, fastJsonConfig.serializeConfig);
        if (features != null) {
            for (SerializerFeature feature : features) {
                serializer.config(feature, true);
            }
        }

        if (filter != null) {
            if (filter instanceof PropertyPreFilter) {
                serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
            }

            if (filter instanceof NameFilter) {
                serializer.getNameFilters().add((NameFilter) filter);
            }

            if (filter instanceof ValueFilter) {
                serializer.getValueFilters().add((ValueFilter) filter);
            }

            if (filter instanceof PropertyFilter) {
                serializer.getPropertyFilters().add((PropertyFilter) filter);
            }

            if (filter instanceof BeforeFilter) {
                serializer.getBeforeFilters().add((BeforeFilter) filter);
            }

            if (filter instanceof AfterFilter) {
                serializer.getAfterFilters().add((AfterFilter) filter);
            }
        }

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}
 
Example #3
Source File: FastjsonHttpMessageConverter.java    From java-platform with Apache License 2.0 4 votes vote down vote up
private void pathVisit(final Map<Class<?>, PropertyPreFilter> propertyFilters, final Set<String> paths,
		Class<?> beanClass) {
	if (paths == null || beanClass == null) {
		return;
	}

	Set<String> revisePaths = new HashSet<>();
	Map<String, Set<String>> references = new HashMap<>();
	for (String path : paths) {
		if (StringUtils.contains(path, ".")) {
			String referenceProperty = StringUtils.substringBefore(path, ".");
			revisePaths.add(referenceProperty);

			Set<String> referencePaths = references.get(referenceProperty);
			if (referencePaths == null) {
				referencePaths = new HashSet<>();
				references.put(referenceProperty, referencePaths);
			}
			referencePaths.add(StringUtils.substringAfter(path, "."));
		} else {
			revisePaths.add(path);
		}
	}

	if (revisePaths.size() > 0) {
		PropertyPreFilter preFilter = propertyFilters.get(beanClass);
		if (preFilter != null) {
			if (preFilter instanceof IncludesPropertyPreFilter) {
				((IncludesPropertyPreFilter) preFilter).getIncludes().addAll(revisePaths);
			}
		} else {
			propertyFilters.put(beanClass, new IncludesPropertyPreFilter(beanClass, revisePaths));
		}

	}

	if (references.size() > 0) {
		for (Entry<String, Set<String>> reference : references.entrySet()) {
			pathVisit(propertyFilters, reference.getValue(),
					PropertyUtil.getPropertyType(beanClass, reference.getKey()));
		}
	}
}