You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A tiny library providing Json formats for refined types.
Context
Refined types come in handy to limit the valid values accepted in a function. However as values are often not available at compile time you need to validate them as soon as they enter your application.
In case of JSON inputs we need a format to convert from JSON to a refined type. (e.g. We want to read a non empty list or a positive integer directly from JSON).
Setup
In order to use play-json-refined you need to add the following lines to your build.sbt:
Importing play.json.refined._ adds implicit definitions to derive Json formats for refined types.
Example
Let's take a basic example to illustrate the usage:
// import the refined that you need// here we use numeric and collectionimporteu.timepit.refined.collection._importeu.timepit.refined.numeric._typePosInt=IntRefinedPositivetypeNonEmptyString=StringRefinedNonEmptyfinalcaseclassData(
i: PosInt,
s: NonEmptyString
)
implicitvaldataFormat:OFormat[Data] =Json.format[Data]
valdata=Data(1, "a")
// convert to JSONvaljson=Json.toJson(data)
// convert from JSONvalparsed= json.as[Data]