Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
add more toJSONBytes method. for issue #1628
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 1, 2018
1 parent b0de5cf commit 75d7ae9
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ public static final String toJSONStringZ(Object object, SerializeConfig mapping,
return toJSONString(object, SerializeConfig.globalInstance, null, null, 0, features);
}

/**
* @since 1.2.42 and 1.1.68.android
*/
public static final byte[] toJSONBytes(Object object, SerializeConfig config, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter((Writer)null, JSON.DEFAULT_GENERATE_FEATURE, features);

Expand All @@ -476,6 +479,68 @@ public static final byte[] toJSONBytes(Object object, SerializeConfig config, Se
}
}

/**
* @since 1.2.11 and 1.1.68.android
*/
public static byte[] toJSONBytes(Object object, SerializeConfig config, int defaultFeatures, SerializerFeature... features) {
return toJSONBytes(object, config, new SerializeFilter[0], defaultFeatures, features);
}

/**
* @since 1.2.42 and 1.1.68.android
*/
public static byte[] toJSONBytes(Object object, SerializeFilter[] filters, SerializerFeature... features) {
return toJSONBytes(object, SerializeConfig.globalInstance, filters, DEFAULT_GENERATE_FEATURE, features);
}

/**
* @since 1.2.42 and 1.1.68.android
*/
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, int defaultFeatures, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter(null, defaultFeatures, features);

try {
JSONSerializer serializer = new JSONSerializer(out, config);

if (filters != null) {
for (SerializeFilter filter : filters) {
if (filter == null) {
continue;
}

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.toBytes("UTF-8");
} finally {
out.close();
}
}

public static final String toJSONString(Object object, boolean prettyFormat) {
if (!prettyFormat) {
return toJSONString(object);
Expand Down

0 comments on commit 75d7ae9

Please sign in to comment.