https://github.com/aparo/zio-json-extra
Several extensions to ZIO-Json to speed up migration of projects from Circe to ZIO-Json
https://github.com/aparo/zio-json-extra
Last synced: 9 months ago
JSON representation
Several extensions to ZIO-Json to speed up migration of projects from Circe to ZIO-Json
- Host: GitHub
- URL: https://github.com/aparo/zio-json-extra
- Owner: aparo
- License: apache-2.0
- Created: 2023-01-22T09:27:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-23T17:54:22.000Z (almost 3 years ago)
- Last Synced: 2025-01-29T10:44:17.975Z (over 1 year ago)
- Language: Scala
- Size: 48.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zio-json-extra
This is a collection of extensions to zio-json, very opionated, required to fast migrate circe code to zio-json (mainly [zio-elasticsearch](https://github.com/aparo/zio-elasticsearch)).
It generally targets some functionalities that are missing in zio-json such as:
- Derivation of Enumeration in string
- Help methods to manipulate JSON AST
There are three modules:
- zio-json-extra that extends zio-json
- zio-json-diffson (WIP) that provides [diffson](https://github.com/gnieh/diffson) support (A scala diff/patch library for Json)
- zio-json-exception that extends Exception managements with classes that can be module-scoped and easy "wireable".
## Derivation Enumeration in String
This capability adds DeriveJsonDecoderEnum/DeriveJsonEncoderEnum that provides enum support based on plain trait/objects
```
@jsonEnumUpperCase
sealed trait Status extends EnumUpperCase
case object Running extends Status
case object Failure extends Status
case class Entity(name: String, status: Status)
implicit val statusDecoder: JsonDecoder[Status] = DeriveJsonDecoderEnum.gen[Status]
implicit val statusEncoder: JsonEncoder[Status] = DeriveJsonEncoderEnum.gen[Status]
implicit val entityDecoder: JsonDecoder[Entity] = DeriveJsonDecoder.gen[Entity]
implicit val entityEncoder: JsonEncoder[Entity] = DeriveJsonEncoder.gen[Entity]
# sample
test("simple enum upper value") {
import exampleupperenums._
assert(""""RUNNING"""".fromJson[Status])(isRight(equalTo(Running)))
},
test("case class with enum upper value") {
import exampleupperenums._
assert("""{"name":"Sample","status":"FAILURE"}""".fromJson[Entity])(
isRight(equalTo(Entity(name = "Sample", status = Failure)))
)
}
```
More example in [tests](zio-json-extra/src/test/scala/zio/json/ast/DeriveJsonEnumSpec.scala)