cq365423762
cq365423762

Reputation: 23

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

the toJSONString 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)

Upvotes: 2

Views: 1785

Answers (2)

Colin Wang
Colin Wang

Reputation: 861

with applause!

args SerializerFeature.EMPTY: _* as default format!

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

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

Upvotes: 0

George Lica
George Lica

Reputation: 1816

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!

Upvotes: 2

Related Questions