https://github.com/typeness/spray-json-refined
spray-json instances for refined library
https://github.com/typeness/spray-json-refined
json refinement-types scala
Last synced: 5 months ago
JSON representation
spray-json instances for refined library
- Host: GitHub
- URL: https://github.com/typeness/spray-json-refined
- Owner: typeness
- License: mit
- Created: 2020-04-29T21:24:02.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T19:04:39.000Z (over 3 years ago)
- Last Synced: 2025-05-14T11:51:09.334Z (about 1 year ago)
- Topics: json, refinement-types, scala
- Language: Scala
- Homepage:
- Size: 97.7 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# spray-json-refined
[](https://repo1.maven.org/maven2/io/github/typeness/spray-json-refined_2.13)
A library that integrates [spray-json](https://github.com/spray/spray-json) and [refined](https://github.com/fthomas/refined)
Given instance of `JsonFormat[T]` this library derives instance of `JsonFormat[T Refined P]`
## Installation
Supported Scala versions: `2.13`, `2.12`
Add the following to your `build.sbt`:
`libraryDependencies += "io.github.typeness" %% "spray-json-refined" % ""`
## Usage
### Read from JSON
``` scala
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined._
import eu.timepit.refined.collection.NonEmpty
import spray.json.DefaultJsonProtocol._
import spray.json._
import io.github.typeness.spray.json.refined._
case class Id(value: String Refined NonEmpty)
implicit val idFormat: RootJsonFormat[Id] = jsonFormat1(Id.apply)
assert(JsObject("value" -> JsString("1234")).convertTo[Id] == Id("1234"))
```
### Write to JSON
``` scala
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined._
import eu.timepit.refined.collection.NonEmpty
import spray.json.DefaultJsonProtocol._
import spray.json._
import io.github.typeness.spray.json.refined._
case class Id(value: String Refined NonEmpty)
implicit val idFormat: RootJsonFormat[Id] = jsonFormat1(Id.apply)
val id = "1234"
assert(refineV[NonEmpty](id).map(Id).toJson == JsObject("value" -> JsString(id)))
```