auto_json
Version, currently 0.21 version
- 0.2latestFeb 2, 2017
github.com/kostya/auto_json
Auto JSON convertations for classes and structs, based on auto_constructor fields
20 stars
0 dependents
License: MIT
Installation
# Add this to your shard.yml
dependencies:
auto_json:
github: kostya/auto_json
version: ~> 0.2Then run:
shards installshard.yml
- Crystal
0.20.5- License
- MIT
- Author
- Konstantin Makarchev <kostya27@gmail.com>
- Target
auto_jsonfrom src/auto_json.cr
Dependencies
Runtime Dependencies
- auto_constructor0.2github: kostya/auto_constructor
README
# auto_json
Auto JSON convertations for classes and structs, based on auto_constructor fields
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
auto_constructor:
github: kostya/auto_json
```
## Usage
```crystal
require "auto_json"
struct A
include AutoJson
field :a, Int32
field :b, String, default: "def"
field :c, Int32
field :d, String?
field :e, Float64
end
puts A.new(a: 1, c: 3, e: 1.0).to_json # => {"a":1,"b":"def","c":3,"e":1.0}
puts A.from_json(%q<{"a":1,"c":3,"e":1.1}>) # => A(@a=1, @b="def", @c=3, @d=nil, @e=1.1)
```
## Field options
```
:key - set serialized key
:json_key - set serialized key for json
:serialize - serialize field? [true]
:json - serialize field for json? [true]
:converter - converter
:json_converter - json_converter
:emit_null - emit null in generate
:json_emit_null - emit null in generate json
```
## Advanced Example
```crystal
require "auto_json"
struct A
include AutoJson
json_options(strict: true)
field :a, Int32, key: "bla"
field :b, String, default: "def"
field :c, Int32, json_key: "blc"
field :d, String?, json_emit_null: true
field :e, Float64, default: 1.1, json: false
field :t, Time?, json_converter: Time::Format.new("%F %T")
end
a = A.new(a: 1, b: "b", c: 2, t: Time.now)
json = a.to_json
puts json # => {"bla":1,"b":"b","blc":2,"d":null,"t":"2017-02-02 01:38:31"}
a2 = A.from_json(json)
p a2 # => A(@a=1, @b="b", @c=2, @d=nil, @e=1.1, @t=2017-02-02 01:38:31)
```
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.2- Tagged
- Feb 2, 2017
- Commit
fc02fdcafc74- Crystal
0.20.5- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/kostya/auto_json
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 12, 2026
- Synced
- Aug 12, 2026
- Versions
- 1