Polyjuice library for mapping JSON objects with dynamic JavaScript code in Scala
What it does
+----------------+
| |
| Dynamic |
| JavaScript code|
| + |
+-------|--------+
|
|
|
v
+------------+ +---------------+ +-------------+
JVM | | | | mapped object | | JVM
+-object---> Json4s +--object as JSON --> Polyjuice +----------by-------> Json4s +--mapped object->
| serialize | | | given js code | Deserialize |
| | | | as JSON | |
+------------+ +---------------+ +-------------+
Why it was build
Let's say you have an expense report which is modeled something like this
case class Report(createdAt: Option[LocalDateTime], currency: Currency, amount: Double, country: Option[String], customFields: Map[String,CustomField])
Here at VATBox we are dealing with a lot of Reports (no they don't look like this unfortunately), each one of them have different custom fields which is customer depending. For example one customer can have a custom field called "center" with ~20 different values one for each country it has a subsidiary in. Since each "center" is in a different country there are different rules hence we need to map each report by different custom fields with countries etc.
This way we can write customer's report specific mapping code and just "upload" it to an already running applications without having them altered for this.
Features
- Nashorn
- Supports 'primitive' types or your own custom models via Json4s
- Malicious code safety (endless loops, external libraries, native code)
SBT
For json4s
3.5.0 and scala-logging
3.5.0 use polyjuice
version 1.1.9
libraryDependencies ++= Seq(
"com.vatbox" %% "polyjuice" % "1.1.9",
/* Unless you already have them */
"com.typesafe.scala-logging" %% "scala-logging" % "3.5.0",
"org.json4s" %% "json4s-native" % "3.5.0",
"org.json4s" %% "json4s-ext" % "3.5.0"
)
For json4s
3.4.0 and scala-logging
3.1.0 use polyjuice
version 1.1.7
libraryDependencies ++= Seq(
"com.vatbox" %% "polyjuice" % "1.1.7",
/* Unless you already have them */
"com.typesafe.scala-logging" %% "scala-logging" % "3.4.0",
"org.json4s" %% "json4s-native" % "3.4.0",
"org.json4s" %% "json4s-ext" % "3.1.0"
)
Examples
There are more examples in Tests
- Return a String value of the "name" key
"Return String" in {
val mapper = Polyjuice.createMapper(varName = "report", userCode = s"""return report.name;""")
val triedT = mapper.map[String](s"""{"name" : "hello World"}""")
triedT.futureValue.value shouldBe "hello World"
}
- Return an Int
"Return Int" in {
val mapper = Polyjuice.createMapper("expense", s"""if (expense.expense > 400) {return 1;} else {return 2;}""")
val triedT = mapper.map[Int](s"""{"expense" : 500}""")
triedT.futureValue.value shouldBe 1
}
- Return a Double (well you got the idea)
"Return Double" in {
val mapper = Polyjuice.createMapper("unused", s"""return 3.3;""")
val triedT = mapper.map[Double](s"""{"irrelevant" : 500}""")
triedT.futureValue.value shouldBe 3.3
}
- Return a LocalDateTime
"Return Date as LocalDateTime" in {
val mapper = Polyjuice.createMapper("unused", s"""return new Date();""")
val triedT = mapper.map[LocalDateTime](s"""{"irrelevant" : 500}""")
triedT.futureValue.value shouldBe a [LocalDateTime]
}
- Now return that same Date but now as 8601 String
"Return Date as String" in {
val mapper = Polyjuice.createMapper("unused", s"""return new Date();""")
val triedT = mapper.map[String](s"""{"irrelevant" : 500}""")
triedT.futureValue.value shouldBe a [String]
// 2016-10-19T10:07:05.427Z
}
- Return a custom object
case class SimpleTestModel(name: String, value: Int)
"Return Object as Object" in {
val mapper = Polyjuice.createMapper("report", s"""return {name: report.firstName, value : 5};""")
val triedT = mapper.map[SimpleTestModel](s"""{"firstName" : "Cool"}""")
triedT.futureValue.value shouldBe SimpleTestModel("Cool", 5)
}
Safety part - endless loop
"Infinite loop" in {
val mapper = Polyjuice.createMapper("whatever",
s"""
|while(true){
|}""".stripMargin)
val triedT = mapper.map(jsonObject = s"""{ "name" : "somename" }""", timeout = 3 seconds)
triedT.failed.futureValue shouldBe a [TimedoutExecution]
}