可变参数需要慎用,不然scala 调用的时候会出现以下问题。
Error:(29, 31) ambiguous reference to overloaded definition,
both method toJSONString in object JSON of type (x$1: Any, x$2: com.alibaba.fastjson.serializer.SerializerFeature*)String
and method toJSONString in object JSON of type (x$1: Any)String
match argument types (com.sydney.dream.example.fastjson.module.Group)
val jsonString = JSON.toJSONString(group)
看原文代码, scala 不同于java 不知道调用哪一个函数,
public static String toJSONString(Object object) {
return toJSONString(object, new SerializerFeature[0]);
}
public static String toJSONString(Object object, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
try {
JSONSerializer serializer = new JSONSerializer(out);
for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
serializer.config(feature, true);
}
serializer.write(object);
return out.toString();
} finally {
out.close();
}
}
所以我们调用的时候,显式调用不定长参数的方法。
JSON.toJSONString(group, new ArraySerializeFilter)