com.alibaba.fastjson.serializer.SerializeFilter Java Examples

The following examples show how to use com.alibaba.fastjson.serializer.SerializeFilter. 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: Endpoint.java    From actframework with Apache License 2.0 6 votes vote down vote up
private String generateSampleJson(BeanSpec spec, Map<String, Class> typeParamLookup, boolean isReturn) {
    Class<?> type = spec.rawType();
    if (Result.class.isAssignableFrom(type) || void.class == type) {
        return null;
    }
    returnSampleObject = generateSampleData(spec, typeParamLookup, new HashSet<Type>(), new ArrayList<String>(), isReturn);
    if (null == returnSampleObject) {
        return null;
    }
    if (returnSampleObject instanceof Map && ((Map) returnSampleObject).isEmpty()) {
        return null;
    }
    if (!type.isArray() && $.isSimpleType(type) && (String.class != type || !isValidJson(S.string(returnSampleObject)))) {
        returnSampleObject = C.Map("result", returnSampleObject);
    }
    SerializeFilter[] filters = new SerializeFilter[null == fastJsonPropertyPreFilter ? 0 : 1];
    if (null != fastJsonPropertyPreFilter) {
        filters[0] = fastJsonPropertyPreFilter;
    }
    SerializerFeature[] features = new SerializerFeature[] {
            SerializerFeature.PrettyFormat
    };
    return JSON.toJSONString(returnSampleObject, SerializeConfig.globalInstance, filters, features);
}
 
Example #2
Source File: FastJsonConfig.java    From metrics with Apache License 2.0 5 votes vote down vote up
public FastJsonConfig(SerializeConfig serializeConfig, SerializerFeature[] serializerFeatures,
                      Map<Class<?>, SerializeFilter> serializeFilters, ParserConfig parserConfig,
                      Feature[] features) {
    this.serializeConfig = serializeConfig;
    this.parserConfig = parserConfig;
    this.serializerFeatures = serializerFeatures;
    this.features = features;
    this.serializeFilters = serializeFilters;
}
 
Example #3
Source File: FastJsonConfig.java    From fastjson-jaxrs-json-provider with Apache License 2.0 5 votes vote down vote up
public FastJsonConfig(SerializeConfig serializeConfig, SerializerFeature[] serializerFeatures, Map<Class<?>, SerializeFilter> serializeFilters, ParserConfig parserConfig, Feature[] features) {
	this.serializeConfig = serializeConfig;
	this.parserConfig = parserConfig;
	this.serializerFeatures = serializerFeatures;
	this.features = features;
	this.serializeFilters = serializeFilters;
}
 
Example #4
Source File: FastJsonConfig.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * init param.
 */
public FastJsonConfig() {

    this.charset = Charset.forName("UTF-8");

    this.serializeConfig = SerializeConfig.getGlobalInstance();
    this.parserConfig = new ParserConfig();

    this.serializerFeatures = new SerializerFeature[] {
            SerializerFeature.BrowserSecure
    };

    this.serializeFilters = new SerializeFilter[0];
    this.features = new Feature[0];
}
 
Example #5
Source File: FastJsonHttpMessageConverter.java    From mcg-helper with Apache License 2.0 5 votes vote down vote up
public void addSerializeFilter(SerializeFilter filter) {
    if (filter == null) {
        return;
    }

    SerializeFilter[] filters = new SerializeFilter[this.filters.length + 1];
    System.arraycopy(this.filters, 0, filter, 0, this.filters.length);
    filters[filters.length - 1] = filter;
    this.filters = filters;
}
 
Example #6
Source File: ActContext.java    From actframework with Apache License 2.0 5 votes vote down vote up
public SerializeFilter[] fastjsonFilters() {
    if (null == fastjsonFilters) {
        return null;
    }
    int len = fastjsonFilters.length;
    SerializeFilter[] filters = new SerializeFilter[len];
    for (int i = 0; i < len; ++i) {
        filters[i] = app().getInstance(fastjsonFilters[i]);
    }
    return filters;
}
 
Example #7
Source File: Json.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param bean
 * @param serializeFilter
 * @return
 * @author tanyaowu
 */
public static String toJson(Object bean, SerializeFilter serializeFilter) {
	if (serializeFilter != null) {
		return JSON.toJSONString(bean, mapping, serializeFilter, SerializerFeature.DisableCircularReferenceDetect);
	} else {
		return JSON.toJSONString(bean, mapping, SerializerFeature.DisableCircularReferenceDetect);
	}
}
 
Example #8
Source File: FastJsonHttpMessageConverter.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@Deprecated
public void addSerializeFilter(SerializeFilter filter) {
    if (filter == null) {
        return;
    }

    int length = this.fastJsonConfig.getSerializeFilters().length;
    SerializeFilter[] filters = new SerializeFilter[length + 1];
    System.arraycopy(this.fastJsonConfig.getSerializeFilters(), 0, filters, 0, length);
    filters[filters.length - 1] = filter;
    this.fastJsonConfig.setSerializeFilters(filters);
}
 
Example #9
Source File: JsonUtil.java    From jeesupport with MIT License 5 votes vote down vote up
public static String toString( Object _obj, SerializeFilter[] _filters, int _features ){
    return toString( _obj, _filters, _features,
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty );
}
 
Example #10
Source File: FastJsonProvider.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Deprecated
public SerializeFilter[] getFilters() {
    return this.fastJsonConfig.getSerializeFilters();
}
 
Example #11
Source File: FastJsonConfig.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
 * @param serializeFilters the serializeFilters to set
 */
public void setSerializeFilters(SerializeFilter... serializeFilters) {
    this.serializeFilters = serializeFilters;
}
 
Example #12
Source File: FastJsonProvider.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setFilters(SerializeFilter... filters) {
    this.fastJsonConfig.setSerializeFilters(filters);
}
 
Example #13
Source File: JsonUtil.java    From jeesupport with MIT License 4 votes vote down vote up
public static String toString( Object _obj, SerializeFilter[] _filters ){
    return toString( _obj, _filters, features );
}
 
Example #14
Source File: JsonUtil.java    From jeesupport with MIT License 4 votes vote down vote up
public static String toString( Object _obj, SerializeFilter[] _filters, int _features, SerializerFeature..._serializerFeatures ){
    return JSON.toJSONString( _obj, SerializeConfig.globalInstance, _filters, null, _features, _serializerFeatures );
}
 
Example #15
Source File: NodeSerializer.java    From java-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
		throws IOException {
	SerializeWriter out = serializer.getWriter();
	if (object == null) {
		out.writeNull();
		return;
	}

	Node<?> node = (Node<?>) object;

	out.write('{');

	out.writeFieldName("children");
	serializer.write(node.getChildren());
	out.write(",");

	out.writeFieldName("leaf");
	out.write(node.getLeaf());
	out.write(",");

	if (!Strings.isNullOrEmpty(node.getIcon())) {
		out.writeFieldName("icon");
		out.writeString(node.getIcon());
		out.write(",");
	}

	if (node.getChecked() != null) {
		out.writeFieldName("checked");
		out.write(node.getChecked());
		out.write(",");
	}

	if (node.getExpanded() != null) {
		out.writeFieldName("expanded");
		out.write(node.getExpanded());
		out.write(",");
	}

	List<? extends SerializeFilter> serializeFilters = serializer.getPropertyPreFilters();

	Object data = node.getData();

	// 处理父节点
	if (data != null && data instanceof Treeable && serializeFilters != null && serializeFilters.size() > 0) {
		boolean parentProcess = false;
		for (SerializeFilter filter : serializeFilters) {
			if (filter instanceof IncludesPropertyPreFilter) {
				Set<String> includes = ((IncludesPropertyPreFilter) filter).getIncludes();
				parentProcess = includes.contains("parent");
				if (parentProcess) {
					break;
				}
			}
		}
		if (parentProcess) {
			Object parent = ((Treeable<?>) data).getParent();
			if (parent != null) {
				out.writeFieldName("parent");
				out.write(JSON.toJSONString(parent, serializeConfig,
						Iterables.toArray(serializeFilters, SerializeFilter.class)));
				out.write(",");
			}
		}
	}

	// 处理对象值
	String dataString = serializeFilters == null ? JSON.toJSONString(data, serializeConfig)
			: JSON.toJSONString(data, serializeConfig, Iterables.toArray(serializeFilters, SerializeFilter.class));
	dataString = StringUtils.substring(dataString, 1);
	out.write(dataString);
}
 
Example #16
Source File: ActContext.java    From actframework with Apache License 2.0 4 votes vote down vote up
public CTX fastjsonFilters(Class<? extends SerializeFilter>[] filters) {
    this.fastjsonFilters = filters;
    return me();
}
 
Example #17
Source File: FastJsonConfig.java    From fastjson-jaxrs-json-provider with Apache License 2.0 4 votes vote down vote up
public FastJsonConfig(SerializeConfig serializeConfig, SerializerFeature[] serializerFeatures, Map<Class<?>, SerializeFilter> serializeFilters) {
	this(serializeConfig, serializerFeatures, serializeFilters, new ParserConfig(), null);
}
 
Example #18
Source File: JsonUtil.java    From jframe with Apache License 2.0 4 votes vote down vote up
public static String encode(Map<String, Object> map, SerializeFilter filter) {
	return JSON.toJSONString(map, filter);
}
 
Example #19
Source File: JsonUtil.java    From jframe with Apache License 2.0 4 votes vote down vote up
public static String toJson(Object obj, SerializeFilter filter) {
	if (obj == null)
		return "";
	return JSON.toJSONString(obj, filter);
}
 
Example #20
Source File: FastJsonJsonView.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Deprecated
public SerializeFilter[] getFilters() {
    return this.fastJsonConfig.getSerializeFilters();
}
 
Example #21
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 #22
Source File: FastJsonConfig.java    From metrics with Apache License 2.0 4 votes vote down vote up
public FastJsonConfig(SerializeConfig serializeConfig, SerializerFeature[] serializerFeatures, Map<Class<?>,
        SerializeFilter> serializeFilters) {
    this(serializeConfig, serializerFeatures, serializeFilters, new ParserConfig(), null);
}
 
Example #23
Source File: FastJsonHttpMessageConverter.java    From mcg-helper with Apache License 2.0 4 votes vote down vote up
public SerializeFilter[] getFilters() {
    return filters;
}
 
Example #24
Source File: FastJsonHttpMessageConverter.java    From mcg-helper with Apache License 2.0 4 votes vote down vote up
public void setFilters(SerializeFilter... filters) {
    this.filters = filters;
}
 
Example #25
Source File: JsonMessageConverter.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    try (ByteArrayOutputStream outnew = new ByteArrayOutputStream()) {
        HttpHeaders headers = outputMessage.getHeaders();

        //获取全局配置的filter
        SerializeFilter[] globalFilters = getFastJsonConfig().getSerializeFilters();
        List<SerializeFilter> allFilters = new ArrayList<>(Arrays.asList(globalFilters));

        boolean isJsonp = false;
        Object value = strangeCodeForJackson(object);

        if (value instanceof FastJsonContainer) {
            FastJsonContainer fastJsonContainer = (FastJsonContainer) value;
            PropertyPreFilters filters = fastJsonContainer.getFilters();
            allFilters.addAll(filters.getFilters());
            value = fastJsonContainer.getValue();
        }

        if (value instanceof MappingFastJsonValue) {
            isJsonp = true;
            value = ((MappingFastJsonValue) value).getValue();
        } else if (value instanceof JSONPObject) {
            isJsonp = true;
        }


        int len = writePrefix(outnew, object);
        len += JSON.writeJSONString(outnew, //
                getFastJsonConfig().getCharset(), //
                value, //
                getFastJsonConfig().getSerializeConfig(), //
                allFilters.toArray(new SerializeFilter[allFilters.size()]),
                getFastJsonConfig().getDateFormat(), //
                JSON.DEFAULT_GENERATE_FEATURE, //
                getFastJsonConfig().getSerializerFeatures());
        len += writeSuffix(outnew, object);

        if (isJsonp) {
            headers.setContentType(APPLICATION_JAVASCRIPT);
        }
        if (getFastJsonConfig().isWriteContentLength()) {
            headers.setContentLength(len);
        }

        headers.set("carrera_logid", RequestContext.getLogId());
        RequestContext.sendJsonResponse(outnew.toString());

        outnew.writeTo(outputMessage.getBody());

    } catch (JSONException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
 
Example #26
Source File: Json.java    From t-io with Apache License 2.0 4 votes vote down vote up
public static SerializeConfig put(Class<?> clazz, SerializeFilter filter) {
	mapping.addFilter(clazz, filter);
	return mapping;
}
 
Example #27
Source File: FastJsonConfig.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
 * @return the classSerializeFilters
 */
public Map<Class<?>, SerializeFilter> getClassSerializeFilters() {
    return classSerializeFilters;
}
 
Example #28
Source File: FastJsonJsonView.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setFilters(SerializeFilter... filters) {
    this.fastJsonConfig.setSerializeFilters(filters);
}
 
Example #29
Source File: FastJsonHttpMessageConverter.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Deprecated
public SerializeFilter[] getFilters() {
    return this.fastJsonConfig.getSerializeFilters();
}
 
Example #30
Source File: FastJsonHttpMessageConverter.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setFilters(SerializeFilter... filters) {
    this.fastJsonConfig.setSerializeFilters(filters);
}