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

Add Json.newBuilder utility #876

Merged
merged 2 commits into from
May 30, 2023
Merged

Conversation

cchantep
Copy link
Member

@cchantep cchantep commented May 7, 2023

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

Add an utility function Json.newBuilder (similar to Map.newBuilder):

import play.api.libs.json.{ Json, JsObject }

// Create a new builder
val builder: JsObjectBuilder = JsObject.newBuilder

// Add key-value pairs to the builder
builder += ("name" -> "John Doe")
builder += ("age" -> 25)

// Clear the builder
builder.clear()

// Add more key-value pairs
builder += ("email" -> "john.doe@example.com")
builder += ("address" -> "123 Street")

// Build the final JsObject
val result: JsObject = builder.result()

// Print the resulting JsObject
println(result)

Will print:

{"email":"john.doe@example.com","address":"123 Street"}

@cchantep cchantep force-pushed the feature/jsobj_builder branch from 72070d9 to cb0f4a1 Compare May 7, 2023 16:02
@cchantep cchantep requested a review from mkurz May 14, 2023 11:29
Copy link
Member

@mkurz mkurz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks alright.

@mkurz mkurz merged commit 6a44c27 into playframework:main May 30, 2023
@cchantep cchantep deleted the feature/jsobj_builder branch May 30, 2023 19:08
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