annotation CrSerializer::Annotations::AccessorOrder

Overview

Defines the order of properties within a class/struct. Valid values: :alphabetical, and :custom.

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

class Default
  include CrSerializer

  def initialize; end

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

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

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

@[CRS::AccessorOrder(:alphabetical)]
class Abc
  include CrSerializer

  def initialize; end

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

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

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

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

  def initialize; end

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

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

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

Defined in:

annotations.cr