Say I have a case class:
case class Name(first: String, last: String)
val n1 = Name("John", "Doe")
val n2 = Name("Mary", "Doe")
and I have a java script object:
@js.native
trait MyJSObject extends js.Object {
def add(name: js.Object): Unit = js.native
}
such that name should be of the format
{"first":"John","last":"Doe"}
What is the best way to convert my case object to a js.Object?
I know this can be achieved doing a upickle.write() to convert it to a json String so I guess my question is is this the best way?
If you control the case class, you can just add the @JSExportAll
annotation to it:
@JSExportAll
case class Name(first: String, last: String)
This will create properties named first
and last
. Note that this is not the same as a simple object. However, depending on what the add
method does, it is sufficient (and much more lightweight than converting to JSON and parsing again).
In case you need to cross compile the case class, you can include the scalajs-stubs
library in your JVM project. It will provide dummy annotations (that are not written to .class
files).
If you need a js.Object
, you can use the export typechecker:
val x = Name("foo", "bar")
js.use(x).as[js.Object]
This will even work with a trait that defines the fields you need:
@js.native
trait Options extends js.Object {
def first: String = js.native
def last: String = js.native
}
val x = Name("foo", "bar")
js.use(x).as[Options]
The js.use(x).as
call will fail, if there is a mismatch between the types (i.e. if Options
defines a field that Name
doesn't define).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With