amnaredo / test

0 stars 0 forks source link

Can't create a JSON object with a single value #227

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

I get a compile error here:

import ujson.Js
Js.Obj("a" -> 1)

Error:

overloaded method value apply with alternatives:
  (value: scala.collection.mutable.Map[String,ujson.Js.Value])ujson.Js.Obj <and>
  (items: (String, ujson.Js.Value)*)ujson.Js.Obj
 cannot be applied to ((String, Int))
val res2 = Js.Obj("a" -> 1)

The same thing happens with Js.Obj("a" -> "b"). However, JsObj("a" ->1, "b" -> 2) works.

import ujson.Js
Js.Obj("a" -> 1, "b" -> 2)
res2: Js.Obj = Obj(Map("b" -> Num(2.0), "a" -> Num(1.0)))

ID: 230 Original Author: winitzki

amnaredo commented 3 years ago

Seems like a type inference bug. Temporary workaround:

@ ujson.Js.Obj("a" -> 1)
cmd0.sc:1: overloaded method value apply with alternatives:
  (value: scala.collection.mutable.LinkedHashMap[String,ujson.Js.Value])ujson.Js.Obj <and>
  (items: (String, ujson.Js.Value)*)ujson.Js.Obj
 cannot be applied to ((String, Int))
val res0 = ujson.Js.Obj("a" -> 1)
                    ^
Compilation Failed

@ ujson.Js.Obj("a" -> ujson.Js.Num(1))
res0: ujson.Js.Obj = Obj(Map("a" -> Num(1.0)))

Original Author: lihaoyi

amnaredo commented 3 years ago

This change seems to break the following use-case, for example used in the (older?) autowire examples serializing to Js.Value:

  data = ujson.write(Js.Obj(req.args.toSeq:_*)))

This can be worked around with

  data = ujson.write(Js.Obj(req.args.head, req.args.tail.toSeq:_*))

but needs extra care if req.args is empty.

Not sure what is the best solution, just a FYI.

Original Author: Dennis4b