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

Added rich wrapper for JsField with specialised field getters #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added rich wrapper for JsField with specialised field getters #64

wants to merge 1 commit into from

Conversation

cvrabie
Copy link

@cvrabie cvrabie commented Aug 6, 2013

When writing a custom JsonFormat the currently proposed way of using pattern matching/extractors on JsObject.getFields is elegant and concise especially when all the fields are required.

However we often meet the situation where, even though the fields in the domain object are required, we can provide default values for some of these fields, thus making some part of the incoming json optional. For example, a User class might require a language and timezone parameters but we we can use the system defaults if the incoming json does not specify these fields.

I experimented with multiple options that would make easier the writing of JsonFormats that allow optional fields and I think the best one is to have a rich wrapper for JsObject that offers specialised getters. If these getters return Either[String,T] a for comprehension can be used on their Right projection.

This also has the added advantage of not polluting the base implementation of JsObject and can be used only when facing the optional fields case.

The whole thing is demonstrated in the RichJsObjectSpec.

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

Successfully merging this pull request may close these issues.

3 participants