cq365423762 cq365423762 - 11 months ago 86
Scala Question

ambiguous reference to overloaded definition when call method in java library

import com.alibaba.fastjson.JSON

object test {
def main(args: Array[String]) = {
val map = new util.HashMap[CharSequence, CharSequence]()
map.put("123", "22333")
map.put("test", null)
val ret = JSON.toJSONString(map)

functiones :

public static String toJSONString(Object object) {
return toJSONString(object, emptyFilters, new SerializerFeature[0]);

public static String toJSONString(Object object, SerializerFeature... features) {
return toJSONString(object, DEFAULT_GENERATE_FEATURE, features);

the error:

Error: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 (java.util.HashMap[CharSequence,CharSequence])
val ret = JSON.toJSONString(map)

Answer Source

For some reason, Scala overloading logic does not match Java logic. You have to call it like this:

JSON.toJSONString(map, SerializerFeature.PrettyFormat)

Have a nice day!