-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Summarize the JSON package and provide relevant links. #6669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
| ## The Play JSON library | ||
|
|
||
| The [`play.api.libs.json`](api/scala/play/api/libs/json/package.html) package contains data structures for representing JSON data and utilities for converting between these data structures and other data representations. Types of interest are: | ||
| The [`play.api.libs.json`](api/scala/play/api/libs/json/package.html) package contains data structures for representing JSON data and utilities for converting between these data structures and other data representations. Some of the features of this package are: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also worth mentioning that it can be used outside a Play application as an independent library?
libraryDependencies += "com.tyesafe.play" %% "play-json" % playVersion|
|
||
| @[auto-custom-naming-format](code/ScalaJsonAutomatedSpec.scala) | ||
|
|
||
| ### Bringing it all together |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an odd place to put this example, in the section about custom naming strategies.
I would probably just expand the examples we already have in the first section. We already show how to generate a Reads or Writes for a case class, but don't show it being used.
| "age" : 4 | ||
| }""") | ||
|
|
||
| Json.fromJson[Resident](jsonString) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be clearer to assign to a variable:
val residentFromJson: Resident = Json.fromJson[Resident](jsonString)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestions. I've updated my branch.
|
|
||
| val resident = Resident(name="Fiver", age=4, role=None) | ||
|
|
||
| Json.toJson(resident) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, would be useful to have a variable or something with a type to indicate what this returns:
val residentJson: JsValue = Json.toJson(resident)| "age" : 4 | ||
| }""") | ||
|
|
||
| val residentFromJson: JsResult[Resident] = Json.fromJson[Resident](jsonString) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's necessary/useful to explain how to extract a value from a JsResult safely (using pattern matching, fold, etc.), or is this self-explanatory to you given the other documentation?
|
well maybe we should also summarize that one could use a body parser where he can either use json or tolerantJson and maybe with / without reads. I guess the latter one is explained nowhere. Basically what I wanted to say is that many people will probably skip the BodyParser docs and directly read into |
|
@schmitch That's super useful. I didn't know Play could do that. I'll add that in as well. |
…with a case class.
|
OK I've added an example of a body parser typed with a case class. I can't really spend much more time on this so please merge it (unless there are minor grammatical errors/typos, etc.). Thanks |
|
If you're processing json in a body parser, you should use So you can process the error result in the event of bad JSON. |
|
@wsargent I've not got as far as validation yet (i.e. I'm new to Play and don't know how to do it). How does your example apply to what I've added? For instance, I've written that you need to forego validation if you use a typed body parser. If that's not right, please can you show me how my code needs to be updated to allow a typed body parser to validate a request. Thanks |
|
@boosh what @wsargent actually said was that if you use def validateJson[A : Reads] = parse.json.validate(
_.validate[A].asEither.left.map(e => BadRequest(JsError.toJson(e)))
)you can actually return a JSON response also in a def myAction = Action(validateJson[MyClass]) { implicit request => Ok } |
|
Also the advantage of that approach is that it parses your body into the correct type, so |
|
OK that's really cool thanks. I'll update my PR again. It did seem a bit weird that using a typed body parser prevented validation... |
| - [[Automatic conversion|ScalaJsonAutomated]] to and from case classes with minimal boilerplate. If you want to get up and running quickly with minimal code, this is probably the place to start. | ||
| - [[Custom validation|ScalaJsonCombinators#Validation-with-Reads]] while parsing. | ||
| - [[Automatic parsing|ScalaBodyParsers#The-default-body-parser]] of JSON in request bodies, with auto-generated errors if content isn't parseable or incorrect Content-type headers are supplied. | ||
| - Can be used outside of a Play application as a standalone library. Just add `libraryDependencies += "com.tyesafe.play" %% "play-json" % playVersion` to your `build.sbt` file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is typo here. "tyesafe" must be "typesafe"
libraryDependencies += "com.typesafe.play" %% "play-json" % playVersionis right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well spotted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I think there is redundant double percent symbol.
@gmethvin , is it must be single?
* Summarize the JSON package and provide relevant links. * Fix code samples * Incorporate suggestions * Standalone not stand-alone * Grammar * Incorporate suggestions, including an example of a body parser typed with a case class. * Add an example of using the body parser to validate JSON as well * Fix typo
Pull Request Checklist
Mostly...
Helpful things
Fixes
Fixes #xxxx
Purpose
What does this PR do?
Adds a summary of the capabilities of the Scala JSON package so users know it's able to convert to and from case classes automatically. Also provided an example of doing that.
Background Context
Why did you take this approach?
The first page of the JSON docs is pretty intimidating for new users. Now it highlights some of the features of the package so users don't get bogged down in the details if they just want to serialise to/from case classes.
References
Are there any relevant issues / PRs / mailing lists discussions?
#6663