Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F62897630
JsonDemos.scala
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, May 16, 09:22
Size
1 KB
Mime Type
text/x-c++
Expires
Sat, May 18, 09:22 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17705034
Attached To
R2664 SHRINE MedCo Fork
JsonDemos.scala
View Options
package net.shrine.integration
import java.io.InputStream
import jawn.{AsyncParser, Parser, ast}
import jawn.ast.JValue
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{FlatSpec, Matchers}
import scala.collection.mutable
import scala.util.Try
/**
* @by ty
* @since 1/24/17
*/
@RunWith(classOf[JUnitRunner])
class JsonDemos extends FlatSpec with Matchers {
val parser : AsyncParser[JValue] = ast.JParser.async(mode = AsyncParser.UnwrapArray)
val exampleJson: String =
"""
|{
| "a" : {
| "a" : {"a" : {"a" : "a"}},
| "b" : {"b" : {"b" : "b"}}
| },
| "b" : {"b" : {"b" : {"b" : "b"}}}
|}
""".stripMargin
val jawnJson = ast.JObject(mutable.Map(
"a" -> ast.JObject(mutable.Map(
"a" -> ast.JObject(mutable.Map("a" -> ast.JObject(mutable.Map("a" -> ast.JString("a"))))),
"b" -> ast.JObject(mutable.Map("b" -> ast.JObject(mutable.Map("b" -> ast.JString("b"))))))),
"b" -> ast.JObject(mutable.Map("b" -> ast.JObject(mutable.Map("b" -> ast.JObject(mutable.Map("b" -> ast.JString("b")))))))))
"The simple, synchronous parser" should "parse the example string" in {
Parser.parseFromString(exampleJson)(jawn.ast.JawnFacade) shouldBe Try(jawnJson)
}
"The async parser" should "parse the file stream and return values as they appear" in {
val stream: InputStream = getClass.getResourceAsStream("/test_data.json")
val lines = scala.io.Source.fromInputStream(stream).getLines
.flatMap(l => parser.absorb(l).right.get)
.toSeq
lines.length shouldBe 24
lines.foreach(_.valueType shouldBe "object")
}
}
Event Timeline
Log In to Comment