https://github.com/bel-framework/bel-json
JSON encoder and decoder for OTP
https://github.com/bel-framework/bel-json
Last synced: about 1 year ago
JSON representation
JSON encoder and decoder for OTP
- Host: GitHub
- URL: https://github.com/bel-framework/bel-json
- Owner: bel-framework
- License: apache-2.0
- Created: 2024-04-30T05:37:52.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-30T05:38:10.000Z (about 2 years ago)
- Last Synced: 2025-02-09T14:46:54.033Z (over 1 year ago)
- Language: Erlang
- Size: 12.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# bel-framework/bel-json
A JSON encoder and decoder built on top of the `json` module proposed in [EEP 68](https://www.erlang.org/eeps/eep-0068) and [introduced in OTP 27](https://www.erlang.org/news/168#highlights-for-rc2).
Uses [json_polyfill](https://github.com/williamthome/json_polyfill) to support OTP versions < 27.
## Encode
```erlang
1> bel_json:encode(#{foo => bar}).
<<"{\"foo\":\"bar\"}">>
```
### Null
```erlang
1> bel_json:encode([foo, null, undefined, bar], #{null_values => [null, undefined]}).
<<"[\"foo\",null,null,\"bar\"]">>
```
### Proplist
```erlang
1> bel_json:encode([{foo, foo}, bar], #{list_codecs => [proplist]}).
<<"{\"foo\":\"foo\",\"bar\":true}">>
```
### Datetime
```erlang
1> bel_json:encode([{foo, foo}, bar], #{tuple_codecs => [datetime]}).
<<"{\"foo\":\"2024-04-29T22:34:35Z\"}">>
```
### Timestamp
```erlang
1> bel_json:encode({0,0,0}, #{tuple_codecs => [timestamp]}).
<<"\"1970-01-01T00:00:00.000Z\"">>
```
### IPv4
```erlang
1> bel_json:encode({0,0,0,0}, #{tuple_codecs => [ipv4]}).
<<"\"0.0.0.0\"">>
```
### IPv6
```erlang
1> bel_json:encode({16#fe80,0,0,0,16#204,16#acff,16#fe17,16#bf38}, #{tuple_codecs => [ipv6]}).
<<"\"fe80::204:acff:fe17:bf38\"">>
```
### Record
```erlang
% -record(foo, {foo, bar}).
1> bel_json:encode(#foo{foo = foo, bar = bar}, #{tuple_codecs => [{record, [{foo, record_info(fields, foo)}]}]}).
<<"{\"foo\":\"foo\",\"bar\":\"bar\"}">>
```
## Decode
```erlang
3> bel_json:decode(<<"{\"foo\":\"bar\"}">>).
#{<<"foo">> => <<"bar">>}
```
## Build
```shell
$ rebar3 compile
```