-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 _) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gives the error
My routes is like this:
Changing line 43 to
def parseperson(json: JsObject) = Action { implicit request =>
results in