com.alibaba.fastjson.serializer.PropertyFilter Java Examples

The following examples show how to use com.alibaba.fastjson.serializer.PropertyFilter. 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: MetricObjectSerializationTest.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testPartiallySerialize() {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            boolean ignore = "value".equals(name) || "timestamp".equals(name);
            return !ignore;
        }
    };

    MetricObject obj = MetricObject.named("test").withLevel(MetricLevel.CRITICAL)
            .withType(MetricObject.MetricType.GAUGE).build();
    String json = JSON.toJSONString(obj, filter); // 序列化的时候传入filter
    JSONObject des = JSON.parseObject(json);
    Assert.assertTrue(des.containsKey("metric"));
    Assert.assertEquals("test", des.getString("metric"));
    Assert.assertTrue(des.containsKey("metricLevel"));
    Assert.assertEquals("CRITICAL", des.getString("metricLevel"));
    Assert.assertTrue(des.containsKey("metricType"));
    Assert.assertEquals("GAUGE", des.getString("metricType"));
}
 
Example #2
Source File: JsonUtils.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 可以允许指定一些过滤字段进行生成json对象
 */
public static String marshalToString(Object obj, String... fliterFields) {
    final List<String> propertyFliters = Arrays.asList(fliterFields);
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                return !propertyFliters.contains(name);
            }

        });
        serializer.write(obj);
        return out.toString();
    } finally {
        out.close();
    }
}
 
Example #3
Source File: JsonUtils.java    From feeyo-hlsserver with Apache License 2.0 6 votes vote down vote up
/**
 * 可以允许指定一些过滤字段进行生成json对象
 */
public static String marshalToString(Object obj, String... fliterFields) {
    final List<String> propertyFliters = Arrays.asList(fliterFields);
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                return !propertyFliters.contains(name);
            }

        });
        serializer.write(obj);
        return out.toString();
    } finally {
        out.close();
    }
}
 
Example #4
Source File: JsonUtil.java    From game-server with MIT License 6 votes vote down vote up
/**
 * 对象转json
 * <p>
 * 需要jsonFiled注解
 * </p>
 *
 * @param object a {@link java.lang.Object} object.
 * @return a {@link java.lang.String} object.
 */
public static String toJSONString(Object object) {
	SerializeWriter out = new SerializeWriter();
	try {
		Map<String, Field> fieldCacheMap = new HashMap<>();
		ParserConfig.parserAllFieldToCache(object.getClass(), fieldCacheMap);
		JSONSerializer serializer = new JSONSerializer(out);
		PropertyFilter filter = (Object source, String name, Object value) -> {
			Field field = ParserConfig.getFieldFromCache(name, fieldCacheMap);
			if (field != null) {
				JSONField fieldAnnotation = field.getAnnotation(JSONField.class);
				if (fieldAnnotation != null) {
					return true;
				}
				return false;
			}
			return true;
		};
		serializer.getPropertyFilters().add(filter);
		serializer.write(object);
		return out.toString();
	} finally {
		out.close();
	}
}
 
Example #5
Source File: JsonUtils.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 可以允许指定一些过滤字段进行生成json对象
 */
public static String marshalToString(Object obj, String... fliterFields) {
    final List<String> propertyFliters = Arrays.asList(fliterFields);
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                return !propertyFliters.contains(name);
            }

        });
        serializer.write(obj);
        return out.toString();
    } finally {
        out.close();
    }
}
 
Example #6
Source File: JsonUtil.java    From jframe with Apache License 2.0 6 votes vote down vote up
/**
 * 过滤除此外的参数
 * 
 * @param params
 * @return
 */
public static PropertyFilter createPropertyFilter(final String[] params) {
	return new PropertyFilter() {

		@Override
		public boolean apply(Object object, String name, Object value) {
			if (value == null)
				return false;
			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					if (name.equals(params[i])) {
						return true;
					}
				}
			}
			if (name.equals(Fields.F_rspCode)
					|| name.equals(Fields.F_rspDesc)) {
				return true;
			}
			return false;
		}

	};
}
 
Example #7
Source File: JsonUtil.java    From jframe with Apache License 2.0 6 votes vote down vote up
public static PropertyFilter createExcludePropertyFilter(
		final String[] params) {
	return new PropertyFilter() {

		@Override
		public boolean apply(Object object, String name, Object value) {
			if (value == null)
				return false;

			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					if (name.equals(params[i])) {
						return false;
					}
				}
			}
			if (name.equals(Fields.F_rspCode)
					|| name.equals(Fields.F_rspDesc)) {
				return true;
			}
			return true;
		}

	};
}
 
Example #8
Source File: JsonUtils.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 可以允许指定一些过滤字段进行生成json对象
 */
public static String marshalToString(Object obj, String... fliterFields) {
    final List<String> propertyFliters = Arrays.asList(fliterFields);
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                return !propertyFliters.contains(name);
            }

        });
        serializer.write(obj);
        return out.toString();
    } finally {
        out.close();
    }
}
 
Example #9
Source File: FastJson.java    From pay with Apache License 2.0 5 votes vote down vote up
public static String toJsonString(final Object data, PropertyFilter filter) {
    try {
        return JSON.toJSONString(data, filter, SerializerFeature.WriteDateUseDateFormat);
    } catch(Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
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 #11
Source File: ResponseHelper.java    From pay with Apache License 2.0 4 votes vote down vote up
public static Result wrapAjax(Object data, PropertyFilter filter) {
    return wrapAjax(FastJson.toJsonString(data, filter));
}