0

I made this simple project: https://github.com/eje211/scalajswithlift

I tried to create a JSON object with Lift DSL and print it in the HTML console with Scala.js . But I get this error:

[info] Compiling 1 Scala source to /Users/eeytan/src/scalajs/target/scala-2.11/classes...
[info] Fast optimizing /Users/eeytan/src/scalajs/target/scala-2.11/scala-js-tutorial-fastopt.js
[error] Referring to non-existent class Lnet_liftweb_json_JsonDSL$
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent class Lnet_liftweb_json_package$
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent class Lnet_liftweb_json_JsonAST$
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonAST$.render__Lnet_liftweb_json_JsonAST$JValue__s_text_Document
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonDSL$.int2jvalue__I__Lnet_liftweb_json_JsonAST$JInt
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonDSL$.pair2Assoc__T2__F1__Lnet_liftweb_json_JsonDSL$JsonAssoc
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonDSL$.string2jvalue__T__Lnet_liftweb_json_JsonAST$JString
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_package$.pretty__s_text_Document__T
[error]   called from Ltutorial_webapp_TutorialApp$.main__V
[error]   called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error]   called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error]   called from Ltutorial_webapp_TutorialApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   Ltutorial_webapp_TutorialApp$
[trace] Stack trace suppressed: run last compile:fastOptJS for the full output.
[error] (compile:fastOptJS) There were linking errors
[error] Total time: 1 s, completed Oct 30, 2015 5:21:03 PM

I don't understand why.

If I remove the comment from build.sbt, I get this error:

> ~fastOptJS
[trace] Stack trace suppressed: run last compile:scalaJSPreLinkClasspath for the full output.
[error] (compile:scalaJSPreLinkClasspath) org.scalajs.core.tools.classpath.JSLibResolveException: Some references to JS libraries could not be resolved:
[error] - Ambiguous reference to a JS library: jquery.js
[error]   Possible paths found on the classpath:
[error]   - scala/tools/nsc/doc/html/resource/lib/jquery.js
[error]   - META-INF/resources/webjars/jquery/2.1.3/jquery.js
[error]   originating from: scalajs:compile
[error] Total time: 1 s, completed Oct 30, 2015 5:35:09 PM

I don't understand what's going on. Can't I use the Lift JSON DSL in Scala.js?

eje211
  • 2,385
  • 3
  • 28
  • 44

1 Answers1

2

Lift, and Lift JSON, are JVM-only projects. They have not been ported/built for Scala.js, as far as I know. So no, you cannot use them in Scala.js.

In general, libraries need to be cross-compiled and published for both Scala/JVM and Scala.js for you to be able to use them. If it makes sense, you could ask Lift developers to cross-compile (some of) their libraries.

That said, in Scala.js, you could typically use JS APIs to work with JSON data structures:

import scala.scalajs.js
import js.Dynamic.{literal => lit}

val j = lit(a = 2, b = "Hello.")
println(js.JSON.stringify(j, space = 2))

Moreover, typically, you wouldn't even manipulate JSON directly at all. Instead, you would use any of the serialization libraries available for Scala.js, such as uPickle, Prickle or BooPickle.

sjrd
  • 21,805
  • 2
  • 61
  • 91
  • I'll look into those. Lift was my introduction to Scala. Its JSON libraries bridge the static typed world of Scala and the dynamically typed world of JavaScript beautifully, particularly for its ability to represent case classes as JSON and the other way round. But that does not mean they're the perfect solution to everything. I'll look into these other ones. Thanks! – eje211 Oct 30 '15 at 09:27