在java库中调用方法时对重载定义的模糊引用

时间:2016-11-10 03:59:01

标签: java scala reference ambiguous

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)
    println(ret)
   }
}

toJSONString函数:

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);
}

错误:

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)

2 个答案:

答案 0 :(得分:0)

出于某种原因,Scala重载逻辑与Java逻辑不匹配。你必须这样称呼它:

JSON.toJSONString(map, SerializerFeature.PrettyFormat)

度过美好的一天!

答案 1 :(得分:0)

鼓掌!

args SerializerFeature.EMPTY: _*为默认格式!

val jsonString = JSON.toJSONString(obj, SerializerFeature.EMPTY: _*)

引用:https://github.com/alibaba/fastjson/issues/1282#issuecomment-624461007