module CrSerializer
Overview
Annotation based serialization/deserialization library.
Features
- Options are defined on ivars, no custom DSL.
- Can be used in conjunction with other shards, such as ORMs, as long as they use properties and allow adding annotations.
*::Serializable
compatible API.
Concepts
CrSerializer::Annotations
- Used to control how a property gets serialized/deserialized.CrSerializer::ExclusionStrategies
- Determines which properties within a class/struct should be serialized and deserialized. Custom strategies can be defined.CrSerializer::Context
- Represents runtime data about the current serialization/deserialization action. Can be reopened to add custom data.CrSerializer::Format
- Represents a valid serialization/deserialization format. Can be included into a module to register a custom format.
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:18property_metadata.cr
formats/json.cr
CrSerializer.cr:98
Constructors
-
.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.
Instance Method Summary
-
#serialize(format : CrSerializer::Format.class, context : CrSerializer::SerializationContext = CrSerializer::SerializationContext.new) : String
Serializes
self
into the given format, optionally with the given context.
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.
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.