Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Struct from JSON with extra data

require "json"

struct Person
  include JSON::Serializable

  getter name : String
  getter email : String
end

json_str = %[{
  "name": "Bar",
  "email": "bar@foobar.com",
  "address" : "Somewhere"
}]
prs = Person.from_json(json_str)
pp! prs

p! prs.name
prs # => Person(@email="bar@foobar.com", @name="Bar")
prs.name # => "Bar"