Json helper that will simplify common json outputs for objects. It can help you build and scaffold JSON APIs faster, by providing simple to use json mappings.
-
Add the dependency to your
shard.yml
:dependencies: jsonable: github: evvo/jsonable
-
Run
shards install
class User
include Jsonable
getter test_field = ["string1", "string2"]
public_json_fields [["test_field"]]
end
user = User.new
user.get_json # { "test_field" : ["string1", "string2"] }
The public_json_fields
are the fields that will be included when you call the get_json
method.
public_json_fields
has 2 params - for single entry and for a collection (optional):
class User
include Jsonable
getter test_field = ["string1", "string2"]
getter test_field_for_collection = ["string1", "string2"]
public_json_fields [["test_field"], ["test_field_for_collection"]]
end
user = User.new
user2 = User.new
User.get_json_collection([user, user2]) # [{ "test_field_for_collection" : ["string1", "string2"] } ...]
You can run the tests by executing:
foo@bar:~$ crystal spec
TODO: Write development instructions here
- Fork it (https://github.com/evvo/jsonable/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Evtimiy Mihaylov - creator and maintainer