Java Code Examples for com.alibaba.fastjson.serializer.SerializeWriter#toString()

The following examples show how to use com.alibaba.fastjson.serializer.SerializeWriter#toString() . 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: 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 2
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 3
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 4
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 5
Source File: JsonUtil.java    From dpCms with Apache License 2.0 6 votes vote down vote up
/**
 *  将一个包含Date类型数据的Object序列化成字符串
 * @param object
 * @param dateFormat
 * @param features
 * @return
 */
public static String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features) {
       SerializeWriter out = new SerializeWriter();
       try {
           SerializeConfig config = new SerializeConfig();
		JSONSerializer serializer = new JSONSerializer(out,config );
           if(dateFormat != null) {
           	serializer.setDateFormat(dateFormat);
           }
           for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
               serializer.config(feature, true);
           }
           serializer.write(object);
           return out.toString();
       } finally {
           out.close();
       }
   }
 
Example 6
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 7
Source File: JsonUtil.java    From dpCms with Apache License 2.0 5 votes vote down vote up
/**
 * @see 将一个Object序列化成字符串
 * @param object
 * @param features
 * @return
 */
public static String toJSONString(Object object, SerializerFeature... features) {
       SerializeWriter out = new SerializeWriter();
       try {
           SerializeConfig config = new SerializeConfig();
		JSONSerializer serializer = new JSONSerializer(out,config );
           for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
               serializer.config(feature, true);
           }
           serializer.write(object);
           return out.toString();
       } finally {
           out.close();
       }
   }
 
Example 8
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();
    }
}