annotation CrSerializer::Annotations::Name

Overview

Defines the name to use on deserialization and serialization. If not provided, the name defaults to the name of the property. Also allows defining aliases that can be used for that property when deserializing.

class Example
  include CrSerializer

  def initialize; end

  @[CRS::Name(serialize: "myAddress")]
  property my_home_address : String = "123 Fake Street"

  @[CRS::Name(deserialize: "some_key", serialize: "a_value")]
  property both_names : String = "str"

  @[CRS::Name(aliases: ["val", "value", "some_value"])]
  property some_value : String? = "some_val"
end

Example.new.to_json # => {"myAddress":"123 Fake Street","a_value":"str","some_value":"some_val"}
obj = Example.from_json %({"my_home_address":"555 Mason Ave","some_key":"deserialized from diff key","value":"some_other_val"})
obj.my_home_address # => "555 Mason Ave"
obj.both_names      # => "deserialized from diff key"
obj.some_value      # => "some_other_val"

Defined in:

annotations.cr