本文整理汇总了Scala中io.scalajs.JSON类的典型用法代码示例。如果您正苦于以下问题:Scala JSON类的具体用法?Scala JSON怎么用?Scala JSON使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JSON类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CheerioTest
//设置package包名称以及导入依赖的类
package io.scalajs.npm.cheerio
import io.scalajs.JSON
import io.scalajs.nodejs.Assert
import org.scalatest.FunSpec
class CheerioTest extends FunSpec {
describe("Cheerio") {
it("supports manipulating HTML") {
val input = """<h2 class="title">Hello world</h2>"""
val $ = Cheerio.load(input)
$("h2.title").text("Hello there!")
$("h2").addClass("welcome")
val output = $.html()
info(s"before: $input")
info(s"after: $output")
Assert.equal(output, """<h2 class="title welcome">Hello there!</h2>""")
}
it("supports text extraction") {
val $ = Cheerio.load("""<ul><li class="orange">Hello world</li></ul>""")
val text = $("li[class=orange]").html()
info(s"text: $text")
Assert.equal(text, "Hello world")
}
it("supports reading component state") {
val $ = Cheerio.load("""<input name="agree" type="checkbox">""")
val value = $("input[type='checkbox']").prop("checked")
info(s"isChecked: $value")
Assert.equal(value, false)
}
it("supports updating component state") {
val $ = Cheerio.load("""<input name="agree" type="checkbox">""")
val value = $("input[type='checkbox']").prop("checked", true).`val`()
info(s"setChecked: $value")
Assert.equal(value, "on")
}
it("supports serialization") {
val $ = Cheerio.load("""<input name="agree" type="checkbox">""")
val value = $("""<form><input name="foo" value="bar" /></form>""").serializeArray()
info(s"value: ${JSON.stringify(value)}")
Assert(value, """[{"name":"foo","value":"bar"}]""")
}
it("supports $.root()") {
val $ = Cheerio.load("""<input name="agree" type="checkbox">""")
val value = $.root().append("""<ul id="vegetables"></ul>""").html()
info(s"value: $value")
}
}
}
示例2: OffsetTest
//设置package包名称以及导入依赖的类
package io.scalajs.npm.kafkanode
import io.scalajs.JSON
import io.scalajs.nodejs.process
import org.scalatest.FunSpec
import scala.scalajs.js
class OffsetTest extends FunSpec {
describe("Offset") {
it("can fetch data") {
process.env.get("ZK_HOST") match {
case None =>
info("No Zookeeper host was specified. Set 'ZK_HOST=localhost:2181'")
case Some(zkConnect) =>
val client = new Client(zkConnect)
val offset = new Offset(client)
val payloads = js.Array(new FetchRequest(topic = "t", partition = 0))
offset.fetch[js.Any](payloads, (err, data) => {
info(s"data: ${JSON.stringify(data)}")
client.close()
})
}
}
}
}
示例3: ClientTest
//设置package包名称以及导入依赖的类
package io.scalajs.npm.nzc
import io.scalajs.JSON
import io.scalajs.nodejs._
import io.scalajs.util.ScalaJsHelper._
import org.scalatest.FunSpec
import scala.concurrent.Future
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js
class ClientTest extends FunSpec {
describe("Client") {
it("can cycle recursively through children") {
process.env.get("ZK_HOST") match {
case Some(zkHost) =>
val client = NodeZookeeperClient.createClient(zkHost)
client.onceConnected(() => {
info(s"Connected to $zkHost...")
client.getChildren("/", (error: String, paths: js.Array[String]) => {
if (isDefined(error))
alert(s"Failed to execute the getChildren: ${JSON.stringify(error)}, results: ${JSON.stringify(paths)}")
else info(JSON.stringify(paths))
val tasks = Future.sequence(paths.toSeq map { path =>
client.getDataFuture[String](path = s"/$path").map(path -> _)
})
tasks foreach { tuples =>
tuples foreach { case (path, values) =>
info(s"$path: ${JSON.stringify(values)}")
}
}
tasks onComplete { _ =>
client.close()
}
})
})
client.connect()
case None =>
info("No Zookeeper host was specified. Set 'ZK_HOST=localhost:2181'")
}
}
}
}
示例4: MongoDSLTest
//设置package包名称以及导入依赖的类
package io.scalajs.npm.mongodb
import io.scalajs.JSON
import org.scalatest.FunSpec
class MongoDSLTest extends FunSpec {
describe("MongoDB-DSL") {
it("has the ability to generate queries containing the $elemMatch operator") {
val query = doc("students" $elemMatch doc("school" -> 102, "age" $gt 10))
assert(JSON.stringify(query) == """{"students":{"$elemMatch":{"school":102,"age":{"$gt":10}}}}""")
}
it("can generate a query containing the $text operator") {
val query = doc(
$text(
new TextSearchOptions(
$search = "hello",
$caseSensitive = true,
$diacriticSensitive = false
)))
assert(
JSON.stringify(query) == """{"$text":{"$search":"hello","$caseSensitive":true,"$diacriticSensitive":false}}""")
}
}
}
示例5: ProcessTest
//设置package包名称以及导入依赖的类
package io.scalajs.nodejs
import io.scalajs.JSON
import io.scalajs.nodejs.Process.ProcessEnvExtensions
import org.scalatest.FunSpec
import scala.scalajs.js
class ProcessTest extends FunSpec {
describe("Process") {
it("contains the following properties") {
show("process.arch", process.arch)
show("process.argv", process.argv)
show("process.config", process.config)
show("process.connected", process.connected)
show("process.cwd()", process.cwd())
show("process.domain", process.domain)
show("process.env", process.env)
show("process.env.NODE_ENV", process.env.NODE_ENV)
show("process.env.PATH", process.env.PATH)
show("process.execArgv", process.execArgv)
show("process.execPath", process.execPath)
show("process.features", process.features)
show("process.moduleLoadList", process.moduleLoadList)
show("process.title", process.title)
show("process.version", process.version)
show("process.versions", JSON.stringify(process.versions))
//show("process.stdout.isTTY", process.stdout.isTTY)
//show("process.stderr.isTTY", process.stderr.isTTY)
}
}
private def show(label: String, value: js.Any) = info(s"$label: $value")
}
示例6: EventEmitterTest
//设置package包名称以及导入依赖的类
package io.scalajs.nodejs.events
import io.scalajs.JSON
import org.scalatest.FunSpec
import scala.scalajs.js
class EventEmitterTest extends FunSpec {
describe("EventEmitter") {
it("should handle events with arguments") {
val myEmitter = new EventEmitter()
myEmitter.on("event", (args: js.Array[Int]) => {
info(s"a custom event was fired with args: ${JSON.stringify(args)}")
})
myEmitter.emit("event", js.Array(1, 2, 3))
}
it("should handle one-time events") {
val myEmitter = new EventEmitter()
var n = 0
myEmitter.once("event", () => {
n += 1
info(s"n = $n")
})
myEmitter.emit("event")
// Prints: 1
myEmitter.emit("event")
// Ignored
}
it("should handle repeated events") {
val myEmitter = new EventEmitter()
var n = 0
myEmitter.on("event", () => {
n += 1
info(s"n = $n")
})
myEmitter.emit("event")
// Prints: 1
myEmitter.emit("event")
// Prints: 2
}
it("should count the number of fired events") {
val myEmitter = new EventEmitter()
myEmitter.on("event", () => {})
myEmitter.on("event", () => {})
assert(myEmitter.listenerCount("event") == 2)
}
}
}
示例7: NodeFetchTest
//设置package包名称以及导入依赖的类
package io.scalajs.npm.nodefetch
import io.scalajs.JSON
import io.scalajs.nodejs.fs.Fs
import org.scalatest.FunSpec
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.util.{Failure, Success}
class NodeFetchTest extends FunSpec {
describe("NodeFetch") {
it("asynchronously retrieves contents via HTTP") {
NodeFetch("https://assets-cdn.github.com/images/modules/logos_page/Octocat.png").toFuture onComplete {
case Success(resp) =>
val dest = Fs.createWriteStream("./octocat.png")
resp.body.pipe(dest)
case Failure(e) =>
alert(e.getMessage)
}
}
it("returns a response containing the typical properties") {
NodeFetch("https://github.com/").toFuture onComplete {
case Success(res) =>
info(s"ok => ${res.ok}")
info(s"status => ${res.status}")
info(s"statusText => ${res.statusText}")
info(s"headers.raw() => ${JSON.stringify(res.headers.raw())}")
info(s"headers.get('content-type') => ${res.headers.get("content-type").orNull}")
case Failure(e) =>
alert(e.getMessage)
}
}
}
}