json_mapping.cr v0.1.1
[!IMPORTANT] This library is no longer supported or updated by the Crystal Team, therefore we have archived the repository.
The contents are still available readonly and continue to work as a shards dependency.
If you wish to continue development yourself, we recommend you fork it. We can also arrange to transfer ownership.
If you have further questions, please reach out on on https://forum.crystal-lang.org or crystal@manas.tech
json_mapping
Provides the legacy JSON.mapping
macro method.
This shard is provided as-is and considered deprecated. It won't receive feature enhancements.
Please consider using JSON::Serializable
instead, the successor included in Crystal's standard library.
Installation
- Add the dependency to your
shard.yml
:
dependencies:
json_mapping:
github: crystal-lang/json_mapping.cr
- Run
shards install
Usage
require "json_mapping"
class Location
JSON.mapping(
lat: Float64,
lng: Float64,
)
end
class House
JSON.mapping(
address: String,
location: {type: Location, nilable: true},
)
end
house = House.from_json(%({"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}))
house.address # => "Crystal Road 1234"
house.location # => #<Location:0x10cd93d80 @lat=12.3, @lng=34.5>
house.to_json # => %({"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}})
houses = Array(House).from_json(%([{"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}]))
houses.size # => 1
houses.to_json # => %([{"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}}])
json_mapping.cr
- 18
- 14
- 0
- 31
- 0
- 28 days ago
- May 20, 2020
MIT License
Tue, 04 Feb 2025 21:01:37 GMT