annotation CrSerializer::Annotations::SkipWhenEmpty

Overview

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

NOTE Can be used on any type that defines an #empty? method.

class SkipWhenEmpty
  include CrSerializer

  def initialize; end

  property id : Int64 = 1

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

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

obj = SkipWhenEmpty.new
obj.to_json # => {"id":1,"value":"value","values":["one","two","three"]}

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

obj.to_json # => {"id":1}

Defined in:

annotations.cr