I'm using the new Play 2.1-RC1 framework and I have a class that has an Option[] field, something like this:
import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
import play.api.libs.json.Writes._
import play.api.libs.json.Format._
import play.api.libs.functional.syntax._
case class Test(name: String, value: Option[String])
object Test {
implicit val testFormat = (
(__ \ "name").format[String] and
(__ \ "value").format[Option[String]]
)(Test.apply, unlift(Test.unapply))
def fromJson(js: String): Test = {
Json.fromJson[Test](Json.parse(js)).fold(
valid = { t => t},
invalid = { e => {
val missingField = (e(0)._1).toString.substring(1)
val badJs = js.trim
val newJs = badJs.substring(0, badJs.length()-1)+",\""+missingField+"\":null}"
fromJson(newJs)
}}
)
}
}
I want to be able to handle JSON strings that omit the optional "value" data, e.g.
val y = """{"name":"someone"}"""
(edited question)
I can rewrite the json string (rather clumsily) as shown in the validation step, but
is there a simpler pattern I can use to supply None for missing Optional fields? Note that this rewrite does not work with nested structures, or anywhere where I can't simply append the missing field.
You can simply do this:
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class Test(name: String, value: Option[String])
implicit val testFormat = Json.format[Test]
def hoge = Action(Json.parse.json) { request =>
Json.fromJson[Test](request.body)
...
}
OK ... so the answer is very simple. Use
fomatOpt()
for optional fields. So the test formatter now looks like this:
import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
import play.api.libs.json.Writes._
import play.api.libs.json.Format._
import play.api.libs.functional.syntax._
case class Test(name: String, value: Option[String])
object Test {
implicit val testFormat = (
(__ \ "name").format[String] and
(__ \ "value").formatOpt[String]
)(Test.apply, unlift(Test.unapply))
def fromJson(js: String): Test = {
Json.fromJson[Test](Json.parse(js)).fold(
valid = { t => t},
invalid = { e => {
println("BAD JSON!")
null
}}
)
}
}
Related
I am trying to reproduce the devign experiment. When joern is updated, some error messages appear in the graph-for-funcs.sc script that parses the bin file into JSON. I modified some, and there are still some errors as shown in the figure below. Did you encounter similar errors and how did you solve them?
graph-for-funcs.sc
import scala.jdk.CollectionConverters._
import io.circe.syntax._
import io.circe.generic.semiauto._
import io.circe.{Encoder, Json}
import io.shiftleft.semanticcpg.language.types.expressions.generalizations.CfgNode
import io.shiftleft.codepropertygraph.generated.EdgeTypes
import io.shiftleft.codepropertygraph.generated.NodeTypes
import io.shiftleft.codepropertygraph.generated.nodes
import io.shiftleft.dataflowengineoss.language._
import io.shiftleft.semanticcpg.language._
import io.shiftleft.semanticcpg.language.types.expressions.Call
import io.shiftleft.semanticcpg.language.types.structure.Local
import io.shiftleft.codepropertygraph.generated.nodes.MethodParameterIn
import overflowdb._
import overflowdb.traversal._
final case class GraphForFuncsFunction(function: String,
file: String,
id: String,
AST: List[nodes.AstNode],
CFG: List[nodes.AstNode],
PDG: List[nodes.AstNode])
final case class GraphForFuncsResult(functions: List[GraphForFuncsFunction])
implicit val encodeEdge: Encoder[Edge] =
(edge: Edge) =>
Json.obj(
("id", Json.fromString(edge.toString)),
("in", Json.fromString(edge.inNode.toString)),
("out", Json.fromString(edge.outNode.toString))
)
implicit val encodeNode: Encoder[nodes.AstNode] =
(node: nodes.AstNode) =>
Json.obj(
("id", Json.fromString(node.toString)),
("edges",
Json.fromValues((node.inE("AST", "CFG").l ++ node.outE("AST", "CFG").l).map(_.asJson))),
("properties", Json.fromValues(node.propertyMap.asScala.toList.map { case (key, value) =>
Json.obj(
("key", Json.fromString(key)),
("value", Json.fromString(value.toString))
)
}))
)
implicit val encodeFuncFunction: Encoder[GraphForFuncsFunction] = deriveEncoder
implicit val encodeFuncResult: Encoder[GraphForFuncsResult] = deriveEncoder
#main def main(): Json = {
GraphForFuncsResult(
cpg.method.map { method =>
val methodName = method.fullName
val methodId = method.toString
val methodFile = method.location.filename
val astChildren = method.astMinusRoot.l
val cfgChildren = method.out(EdgeTypes.CONTAINS).asScala.collect { case node: nodes.CfgNode => node }.toList
val local = new NodeSteps(
method
.out(EdgeTypes.CONTAINS)
.hasLabel(NodeTypes.BLOCK)
.out(EdgeTypes.AST)
.hasLabel(NodeTypes.LOCAL)
.cast[nodes.Local])
val sink = local.evalType(".*").referencingIdentifiers.dedup
val source = new NodeSteps(method.out(EdgeTypes.CONTAINS).hasLabel(NodeTypes.CALL).cast[nodes.Call]).nameNot("<operator>.*").dedup
val pdgChildren = sink
.reachableByFlows(source)
.l
.flatMap { path =>
path.elements
.map {
case trackingPoint # (_: MethodParameterIn) => trackingPoint.start.method.head
case trackingPoint => trackingPoint.cfgNode
}
}
.filter(_.toString != methodId)
GraphForFuncsFunction(methodName, methodFile, methodId, astChildren, cfgChildren, pdgChildren.distinct)
}.l
).asJson
}
error
graph-for-funcs.sc:92: value evalType is not a member of io.shiftleft.semanticcpg.language.NodeSteps[io.shiftleft.codepropertygraph.generated.nodes.Local]
val sink = local.evalType(".*").referencingIdentifiers.dedup
^
graph-for-funcs.sc:93: value nameNot is not a member of io.shiftleft.semanticcpg.language.NodeSteps[io.shiftleft.codepropertygraph.generated.nodes.Call]
val source = new NodeSteps(method.out(EdgeTypes.CONTAINS).hasLabel(NodeTypes.CALL).cast[nodes.Call]).nameNot("<operator>.*").dedup
^
java.lang.RuntimeException: Compilation Failed
io.shiftleft.console.scripting.AmmoniteExecutor.$anonfun$runScript$7(AmmoniteExecutor.scala:50)
cats.effect.internals.IORunLoop$.liftedTree3$1(IORunLoop.scala:229)
cats.effect.internals.IORunLoop$.step(IORunLoop.scala:229)
cats.effect.IO.unsafeRunTimed(IO.scala:320)
cats.effect.IO.unsafeRunSync(IO.scala:239)
io.shiftleft.console.scripting.ScriptManager.runScript(ScriptManager.scala:130)
io.shiftleft.console.scripting.ScriptManager$CpgScriptRunner.runScript(ScriptManager.scala:64)
io.shiftleft.console.scripting.ScriptManager$CpgScriptRunner.runScript(ScriptManager.scala:54)
ammonite.$sess.cmd8$.<clinit>(cmd8.sc:1)
I am trying to use play.api.lib.json to convert a json to my object. But then this happend...
case class Foo(foo:Option[Map[String,String]])
case class Bar(bar:String,foo:Foo)
def barJsonToModel(foobarJson:JsValue):Bar = {
implicit val fooReads: Reads[Foo] = (
( JsPath \ "foo" ).readNullable[Map[String,String]]
)(Foo.apply _)
}
Expression of type Reads[Option[Map[String,String]]] doesn't comfort to expect type Reads[Foo]
You are using the functional syntax of play-json, but so an import is missing before:
import play.api.libs.functional.syntax._
I am writing a Customer Serializer. In that Serializer I would like to somehow say: "and this thing you already know how to serialize".
My current approach looks like that:
import org.json4s.native.Serialization._
import org.json4s.JsonDSL.WithBigDecimal._
object WindowSerializer extends CustomSerializer[Window](format =>
( [omitted],
{
case Window(frame, size) =>
( "size" -> size ) ~
( "frame" -> parse(write(frame)) )
}))
That parse(write(frame)) things is both ugly and inefficient. How to fix that?
You can call Extraction.decompose(a: Any)(implicit formats: Formats): JValue which produces a JValue from some value using runtime reflection.
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL._
import java.util.UUID
case class Thing(name: String)
case class Box(id: String, thing: Thing)
class BoxSerializer extends CustomSerializer[Box](format => ({
case jv: JValue =>
val id = (jv \ "id").extract[String]
val thing = (jv \ "thing").extract[Thing]
Box(id, thing)
}, {
case b: Box =>
("token" -> UUID.randomUUID().toString()) ~
("id" -> box.id) ~
("thing" -> Extraction.decompose(box.thing))
}))
implicit val formats = DefaultFormats + new BoxSerializer
val box = Box("1000", Thing("a thing"))
// decompose the value to JSON
val json = Extraction.decompose(box)
println(pretty(json))
// {
// "token" : "d9bd49dc-11b4-4380-ab10-f6df005a384c",
// "id" : "1000",
// "thing" : {
// "name" : "a thing"
// }
// }
// and read a value of type Box back from the JSON
println(json.extract[Box])
// Box(1000,Thing(a thing))
I am using Play framework 2.1.1 with scala.I query a database table return to controller as list and then convert list to string and return to ajax call from javascript code.
How to return query result as json and return to ajax call throught controller?
Application.scala
import play.api._
import play.api.mvc._
import play.api.data._
import views.html._
import models._
object Application extends Controller {
def index = Action {
Ok(views.html.index())
}
def getSummaryTable = Action{
var sum="Summary Table";
Ok(ajax_result.render((Timesheet.getAll).mkString("\n")))
}
def javascriptRoutes = Action { implicit request =>
import routes.javascript._
Ok(
Routes.javascriptRouter("jsRoutes")(
// Routes
controllers.routes.javascript.Application.getSummaryTable
)
).as("text/javascript")
}
}
TimeSheet.scala
// Use PostgresDriver to connect to a Postgres database
import scala.slick.driver.PostgresDriver.simple._
import scala.slick.lifted.{MappedTypeMapper,BaseTypeMapper,TypeMapperDelegate}
import scala.slick.driver.BasicProfile
import scala.slick.session.{PositionedParameters,PositionedResult}
// Use the implicit threadLocalSession
import Database.threadLocalSession
import java.sql.Date
import java.sql.Time
case class Timesheet(ID: Int, dateVal: String, entryTime: Time, exitTime: Time, someVal: String)
object Timesheet {
//Definition of Timesheet table
// object TS extends Table[(Int,String,Time,Time,String)]("timesheet"){
val TSTable = new Table[Timesheet]("timesheet"){
def ID = column[Int]("id")
def dateVal = column[String]("date")
def entryTime = column[Time]("entry_time")
def exitTime = column[Time]("exit_time")
def someVal = column[String]("someval")
def * = ID ~ dateVal ~ entryTime ~ exitTime ~ someVal <> (Timesheet.apply _, Timesheet.unapply _)
}
def getAll: Seq[Timesheet] = {
Database.forURL("jdbc:postgresql://localhost:5432/my_db", "postgres", "password",null, driver="org.postgresql.Driver") withSession{
val q = Query(TSTable)
val qFiltered = q.filter(_.ID === 41 )
val qDateFilter = qFiltered.filter(_.dateVal === "01/03/2013")
val qSorted = qDateFilter.sortBy(_.entryTime)
qSorted.list
}
}
}
Also, don't forget to provide an implicit (or not) Json deserializer for your model, otherwise, Scala compiler will yell at you :-). You can do something like :
def allTimesheet = Action {
val timesheetWrites = Json.writes[Timesheet] // here it's the deserializer
val listofTimeSheet = Timesheet.getAll
Ok( Json.toJson( listofTimeSheet )( timesheetWrites ) )
}
or you can use implicits like :
def allTimesheet = Action {
implicit val timesheetWrites = Json.writes[Timesheet] // here it's the deserializer
val listofTimeSheet = Timesheet.getAll
Ok( Json.toJson( listofTimeSheet ) )
}
and even declare your deserializer in your model companion object like :
companion object
object Timesheet {
implicit val timesheetWrites = Json.writes[Timesheet] // here it's the deserializer
....
}
and in the controller
import models.Timesheet.timesheetWrites
def allTimesheet = Action {
val listofTimeSheet = Timesheet.getAll
Ok( Json.toJson( listofTimeSheet ) )
}
I recommend you use play.api.libs.Json.toJson.
Here's an example:
object Products extends Controller {
def list = Action {
val productCodes = Product.findAll.map(_.ean)
Ok(Json.toJson(productCodes))
}
Json.toJson returns a JsValue for which Play will automatically add a application/json header.
See Play For Scala chapter 8.
In PlayFramework 2.1 I want to read a json and create a case class. The structure is different, so I cannot use the default reader. What do I have to write to merge the both Strings to the java.util.Date?
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
case class Person(name: String, lastSeen: Date)
val jsonString = Json.parse("""{name: "Joe", day: "2013-03-28", time: "09:35"}""")
val personLastSeen: Reads[Person] = (
(__ \ "name").read[String] ~
// (__ \ "day").read[String] (__ \ "time").read[String] // #TODO
)(Person)
Should I swap this read? I tried val dateStructure = new SimpleDateFormat("yyyy-MM-dd HH:mm") with dateStructure.parse(...) but I think I need a Reads[Date] there
I think you cannot do this using Reads[T] combinators but you can define a trait Reads and its method def read(json: JsValue): JsResult[T]
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
import java.util.Date
import java.text.SimpleDateFormat
case class Person(name: String, lastSeen: Date) {
}
object Person {
implicit val personLastSeen = new Reads[Person] {
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm")
def reads(js: JsValue): JsResult[Person] = {
JsSuccess(Person(
(js \ "name").as[String],
sdf.parse((js \ "day").as[String] + " " + (js \ "time").as[Float])))
}
}
}
object Test extends App {
val jsonString = Json.parse("""{name: "Joe", day: "2013-03-28", time: "09:35"}""")
}