auto_json
THIS PROJECT DEPRECATED, just use JSON::Serializable module instead.
auto_json
Auto JSON convertations for classes and structs, based on auto_constructor fields
Installation
Add this to your application's shard.yml
:
dependencies:
auto_json:
github: kostya/auto_json
Usage
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
require "auto_json"
struct A
include AutoJson
json_options(strict: true, emit_nulls: 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)
Repository
auto_json
Owner
Statistic
- 19
- 2
- 0
- 0
- 1
- over 6 years ago
- February 2, 2017
License
MIT License
Links
Synced at
Fri, 22 Nov 2024 00:18:52 GMT
Languages