Skip to content

annotation Athena::Serializer::Annotations::SkipWhenEmpty #

Indicates that a property should not be serialized when it is empty.

Example#

class Example
  include ASR::Serializable

  def initialize; end

  property id : Int64 = 1

  @[ASRA::SkipWhenEmpty]
  property value : String = "value"

  @[ASRA::SkipWhenEmpty]
  property values : Array(String) = %w(one two three)
end

obj = Example.new

ASR.serializer.serialize obj, :json # => {"id":1,"value":"value","values":["one","two","three"]}

obj.value = ""
obj.values = [] of String

ASR.serializer.serialize obj, :json # => {"id":1}

!!!tip: Can be used on any type that defines an #empty? method.