Skip to content

Commit

Permalink
add more toJSONBytes method.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 8, 2017
1 parent e24f80e commit ebf74f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/main/java/com/alibaba/fastjson/JSON.java
Expand Up @@ -614,6 +614,10 @@ public static String toJSONString(Object object, SerializeFilter[] filters, Seri
public static byte[] toJSONBytes(Object object, SerializerFeature... features) {
return toJSONBytes(object, DEFAULT_GENERATE_FEATURE, features);
}

public static byte[] toJSONBytes(Object object, SerializeFilter filter, SerializerFeature... features) {
return toJSONBytes(object, SerializeConfig.globalInstance, new SerializeFilter[] {filter}, DEFAULT_GENERATE_FEATURE, features);
}

/**
* @since 1.2.11
Expand Down Expand Up @@ -681,18 +685,42 @@ public static String toJSONStringZ(Object object, SerializeConfig mapping, Seria
return toJSONString(object, mapping, emptyFilters, null, 0, features);
}

/**
* @since 1.2.42
*/
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializerFeature... features) {
return toJSONBytes(object, config, DEFAULT_GENERATE_FEATURE, features);
return toJSONBytes(object, config, emptyFilters, DEFAULT_GENERATE_FEATURE, features);
}

/**
* @since 1.2.11
* @since 1.2.11
*/
public static byte[] toJSONBytes(Object object, SerializeConfig config, int defaultFeatures, SerializerFeature... features) {
return toJSONBytes(object, config, emptyFilters, defaultFeatures, features);
}

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

/**
* @since 1.2.42
*/
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) {
serializer.addFilter(filter);
}
}

serializer.write(object);
return out.toBytes(IOUtils.UTF8);
} finally {
Expand Down Expand Up @@ -1027,5 +1055,5 @@ public static <T> void handleResovleTask(DefaultJSONParser parser, T value) {
parser.handleResovleTask(value);
}

public final static String VERSION = "1.2.41";
public final static String VERSION = "1.2.42";
}
18 changes: 18 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1600/Issue1628.java
@@ -0,0 +1,18 @@
package com.alibaba.json.bvt.issue_1600;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import junit.framework.TestCase;

import java.util.HashMap;
import java.util.Map;

public class Issue1628 extends TestCase {
public void test_toJSONBytes() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", 1001);
map.put("b", 2002);
byte[] bytes = JSON.toJSONBytes(map, new SimplePropertyPreFilter("a"));
assertEquals("{\"a\":1001}", new String(bytes));
}
}

0 comments on commit ebf74f0

Please sign in to comment.