json_mapping.cr v0.1.1

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

  1. Add the dependency to your shard.yml:
dependencies:
  json_mapping:
    github: crystal-lang/json_mapping.cr
  1. 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}}])

Contributing

  1. Fork it (https://github.com/crystal-lang/json_mapping.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
Repository

json_mapping.cr

Owner
Statistic
  • 18
  • 14
  • 0
  • 27
  • 0
  • almost 3 years ago
  • May 20, 2020
License

MIT License

Links
Synced at

Wed, 27 Mar 2024 22:50:21 GMT

Languages