Skip to content

Commit

Permalink
Document after_initialize method for yaml and json serializers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
analogsalad authored Oct 5, 2022
1 parent 268aafd commit 826a631
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/json/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ module JSON
# field, and the rest of the fields, and their meaning, depend on its value.
#
# You can use `JSON::Serializable.use_json_discriminator` for this use case.
#
# ### `after_initialize` method
#
# `#after_initialize` is a method that runs after an instance is deserialized
# from JSON. It can be used as a hook to post-process the initialized object.
#
# Example:
# ```
# require "json"
#
# class Person
# include JSON::Serializable
# getter name : String
#
# def after_initialize
# @name = @name.upcase
# end
# end
#
# person = Person.from_json %({"name": "Jane"})
# person.name # => "JANE"
# ```
module Serializable
annotation Options
end
Expand Down
22 changes: 22 additions & 0 deletions src/yaml/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ module YAML
# field, and the rest of the fields, and their meaning, depend on its value.
#
# You can use `YAML::Serializable.use_yaml_discriminator` for this use case.
#
# ### `after_initialize` method
#
# `#after_initialize` is a method that runs after an instance is deserialized
# from YAML. It can be used as a hook to post-process the initialized object.
#
# Example:
# ```
# require "yaml"
#
# class Person
# include YAML::Serializable
# getter name : String
#
# def after_initialize
# @name = @name.upcase
# end
# end
#
# person = Person.from_yaml "---\nname: Jane\n"
# person.name # => "JANE"
# ```
module Serializable
annotation Options
end
Expand Down

0 comments on commit 826a631

Please sign in to comment.