Skip to content
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

Custom field names for tuples Reads/Writes #896

Merged
merged 1 commit into from
Jul 16, 2023

Conversation

cchantep
Copy link
Member

Pull Request Checklist

  • Have you read through the contributor guidelines?
  • Have you squashed your commits?
  • Have you added copyright headers to new files?
  • Have you updated the documentation?
  • Have you added tests for any changed functionality?

Purpose

Handle simple JSON objects as Tuple{2,3,4} with custom JSON field names without having to defined case classes.

val tuple2Reads: Reads[(String, Int)] = Reads.tuple2[String, Int]("name", "age")
val tuple2Writes: OWrites[(String, Int)] = OWrites.tuple2[String, Int]("name", "age")

val tuple2ExampleJson: JsValue = Json.obj("name" -> "Alice", "age" -> 25)
val tuple2Result: JsResult[(String, Int)] = tuple2Reads.reads(tuple2ExampleJson)
println(tuple2Result)  // JsSuccess(("Alice", 25))

val tuple2Json: JsValue = tuple2Writes.writes("Bob" -> 30)
println(tuple2Json)  // {"name":"Bob","age":30}

@cchantep cchantep requested a review from mkurz July 2, 2023 14:23
@mkurz mkurz merged commit 6649729 into playframework:main Jul 16, 2023
@cchantep cchantep deleted the feature/tuples branch July 16, 2023 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants