1. 程式人生 > >scala 呼叫fastjson 的toJSONString() 報錯

scala 呼叫fastjson 的toJSONString() 報錯

可變引數需要慎用,不然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)