Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/massivefermion/juno
Flexible JSON decoding
https://github.com/massivefermion/juno
decode decoder decoding deserialization deserialize flexible gleam json thoas
Last synced: about 1 month ago
JSON representation
Flexible JSON decoding
- Host: GitHub
- URL: https://github.com/massivefermion/juno
- Owner: massivefermion
- License: apache-2.0
- Created: 2023-11-25T01:29:30.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-12T17:04:52.000Z (about 1 month ago)
- Last Synced: 2024-11-12T18:19:33.809Z (about 1 month ago)
- Topics: decode, decoder, decoding, deserialization, deserialize, flexible, gleam, json, thoas
- Language: Gleam
- Homepage: https://hexdocs.pm/juno
- Size: 256 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
![juno](https://raw.githubusercontent.com/massivefermion/juno/main/banner.png)
[![Package Version](https://img.shields.io/hexpm/v/juno)](https://hex.pm/packages/juno)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/juno)# juno
Juno is a tool for decoding JSON in a more flexible way, which can be useful in situations where the exact structure of the data is not known but there are still patterns in it that need to be captured.
## 🦋 Quick start
```sh
gleam test # Run the tests
gleam shell # Run an Erlang shell
```## 🦋 Installation
```sh
gleam add juno
```## 🦋 Usage
```gleam
import gleam/dynamic
import junopub type Entity {
Player(id: String, score: Int, eliminated: Bool)
Game(id: String, players: List(String), winner: String, prize: String)
}pub fn main() {
let json = "{
\"2023-11-24\": {
\"weather\": \"rainy\",
\"attending\": {
\"anton\": {
\"id\": \"P36\",
\"scores\": [45, 55],
\"eliminated\": true
},
\"vincent\": {
\"id\": \"P3\",
\"score\": 28,
\"eliminated\": false
},
\"gerome\": {
\"id\": \"P6\",
\"score\": 15,
\"eliminated\": false
},
\"irene\": {
\"id\": \"P10\",
\"score\": 21,
\"eliminated\": false
}
},
\"14:00\": {
\"id\": \"G528\",
\"players\": [
\"P10\",
\"P6\"
],
\"winner\": \"P10\",
\"prize\": \"vacation\"
},
\"16:00->21:00\": [
{
\"id\": \"G561\",
\"players\": [
\"P10\",
\"P3\"
],
\"winner\": \"P10\",
\"prize\": \"toaster\"
},
{
\"id\": \"G595\",
\"players\": [
\"P3\",
\"P6\"
],
\"winner\": \"P3\",
\"prize\": \"car\"
}
]
}
}"let active_player_decoder =
dynamic.decode3(
Player,
dynamic.field("id", dynamic.string),
dynamic.field("score", dynamic.int),
dynamic.field("eliminated", dynamic.bool),
)let game_decoder =
dynamic.decode4(
Game,
dynamic.field("id", dynamic.string),
dynamic.field("players", dynamic.list(dynamic.string)),
dynamic.field("winner", dynamic.string),
dynamic.field("prize", dynamic.string),
)juno.decode(json, [active_player_decoder, game_decoder])
}
```