Skip to content

annotation Athena::Serializer::Annotations::AccessorOrder #

Can be applied to a type to control the order of properties when serialized. Valid values: :alphabetical, and :custom.

By default properties are ordered in the order in which they are defined.

Fields#

  • order - Used to specify the order of the properties when using :custom ordering.

Example#

class Default
  include ASR::Serializable

  def initialize; end

  property a : String = "A"
  property z : String = "Z"
  property two : String = "two"
  property one : String = "one"
  property a_a : Int32 = 123

  @[ASRA::VirtualProperty]
  def get_val : String
    "VAL"
  end
end

ASR.serializer.serialize Default.new, :json # => {"a":"A","z":"Z","two":"two","one":"one","a_a":123,"get_val":"VAL"}

@[ASRA::AccessorOrder(:alphabetical)]
class Abc
  include ASR::Serializable

  def initialize; end

  property a : String = "A"
  property z : String = "Z"
  property two : String = "two"
  property one : String = "one"
  property a_a : Int32 = 123

  @[ASRA::VirtualProperty]
  def get_val : String
    "VAL"
  end
end

ASR.serializer.serialize Abc.new, :json # => {"a":"A","a_a":123,"get_val":"VAL","one":"one","two":"two","z":"Z"}

@[ASRA::AccessorOrder(:custom, order: ["two", "z", "get_val", "a", "one", "a_a"])]
class Custom
  include ASR::Serializable

  def initialize; end

  property a : String = "A"
  property z : String = "Z"
  property two : String = "two"
  property one : String = "one"
  property a_a : Int32 = 123

  @[ASRA::VirtualProperty]
  def get_val : String
    "VAL"
  end
end

ASR.serializer.serialize Custom.new, :json # => {"two":"two","z":"Z","get_val":"VAL","a":"A","one":"one","a_a":123}