Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from patientplatypus/readjsonfile.scala
Last active August 7, 2017 19:58
Show Gist options
  • Save Jacoby6000/93c42c6a8d10497ef45a87e786fad63f to your computer and use it in GitHub Desktop.
Save Jacoby6000/93c42c6a8d10497ef45a87e786fad63f to your computer and use it in GitHub Desktop.
im trying to get a simple read of some json object class in scala and running into trouble
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
import play.api.libs.json._
import play.api.libs.functional.syntax._
import JsonFormats._
/**
* This controller creates an `Action` to handle HTTP requests to the
* application's home page.
*/
case class Person(name: String, country: String, id: Int)
// I like to define a JsonFormats object which contains all of the json formats for all of my domain objects. When I
// want to read/write some json, I just import this.
object JsonFormats {
// The two lines below create json readers and writers using a macro.
// For it to work, you must have a reads/writes for each type making up the case class.
// Play comes with reads/writes for String and Int, so this "just works"
implicit val personReads = Json.reads[Person]
implicit val personWrites = Json.writes[Person]
}
@Singleton
class HomeController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
/**
* Create an Action to render an HTML page.
*
* The configuration in the `routes` file means that this method
* will be called when the application receives a `GET` request with
* a path of `/`.
*/
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
def getName = Action {
Ok("Jim")
}
def parseJson(json: JsObject) = Action { implicit request =>
val person = json.as[Person]
Ok("name : " + person.name)
}
// implicit val placeReads: Reads[Place] = (
// (JsPath \ "name").read[String] and
// (JsPath \ "location").read[Location] and
// (JsPath \ "residents").read[Seq[Resident]]
// )(Place.apply _)
}
@patientplatypus
Copy link

This gives the error

[info] Compiling 7 Scala sources and 1 Java source to /Users/patientplatypus/Documents/scalaproj1/scalarest/target/scala-2.12/classes...
[error] /Users/patientplatypus/Documents/scalaproj1/scalarest/conf/routes:15: value parseperson is not a member of controllers.HomeController
[error] POST  /parseperson controllers.HomeController.parseperson
[error] one error found
[error] (compile:compileIncremental) Compilation failed

My routes is like this:

# Routes
# This file defines all application routes (Higher priority routes first)
# https://www.playframework.com/documentation/latest/ScalaRouting
# ~~~~
# An example controller showing a sample home page
GET     /                           controllers.HomeController.index
# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
#REST ENDPOINTS
GET		/name				controllers.HomeController.getName
POST  /parseperson controllers.HomeController.parseperson

Changing line 43 to
def parseperson(json: JsObject) = Action { implicit request =>
results in

[info] Compiling 7 Scala sources and 1 Java source to /Users/patientplatypus/Documents/scalaproj1/scalarest/target/scala-2.12/classes...
[error] /Users/patientplatypus/Documents/scalaproj1/scalarest/conf/routes:15: missing argument list for method parseperson in class HomeController
[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `parseperson _` or `parseperson(_)` instead of `parseperson`.
[error] POST  /parseperson controllers.HomeController.parseperson
[error] one error found
[error] (compile:compileIncremental) Compilation failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment