module CrSerializer

Overview

Annotation based serialization/deserialization library.

Features

Concepts

Example Usage

@[CRS::ExclusionPolicy(:all)]
@[CRS::AccessorOrder(:alphabetical)]
class Example
  include CrSerializer

  @[CRS::Expose]
  @[CRS::Groups("details")]
  property name : String

  @[CRS::Expose]
  @[CRS::Name(deserialize: "a_prop", serialize: "a_prop")]
  property some_prop : String

  @[CRS::Expose]
  @[CRS::Groups("default", "details")]
  @[CRS::Accessor(getter: get_title)]
  property title : String

  @[CRS::ReadOnly]
  property password : String?

  getter first_name : String?
  getter last_name : String?

  @[CRS::PostDeserialize]
  def split_name : Nil
    @first_name, @last_name = @name.split(' ')
  end

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

  private def get_title : String
    @title.downcase
  end
end

obj = Example.from_json %({"name":"FIRST LAST","a_prop":"STR","title":"TITLE","password":"monkey123"})
obj.inspect                                                             # => #<Example:0x7f3e3b106740 @name="FIRST LAST", @some_prop="STR", @title="TITLE", @password=nil, @first_name="FIRST", @last_name="LAST">
obj.to_json                                                             # => {"a_prop":"STR","get_val":"VAL","name":"FIRST LAST","title":"title"}
obj.to_json CrSerializer::SerializationContext.new.groups = ["details"] # => {"name":"FIRST LAST","title":"title"}

Defined in:

CrSerializer.cr:18
property_metadata.cr
formats/json.cr
CrSerializer.cr:98

Constructors

Instance Method Summary

Constructor Detail

def self.deserialize(format : CrSerializer::Format.class, string_or_io : String | IO, context : CrSerializer::DeserializationContext = CrSerializer::DeserializationContext.new) : self #

Deserializes the given string_or_io into self from the given format, optionally with the given context.

NOTE This method is defined within a macro included hook. This definition is simply for documentation.


[View source]

Instance Method Detail

def serialize(format : CrSerializer::Format.class, context : CrSerializer::SerializationContext = CrSerializer::SerializationContext.new) : String #

Serializes self into the given format, optionally with the given context.


[View source]