module Nya::Serializable

Defined in:

nya_serializable/serializable.cr
nya_serializable/serializable/exception.cr
nya_serializable/serializable/rename.cr
nya_serializable/serializable/serialization_context.cr
nya_serializable/serializable/version.cr

Constant Summary

VERSION = Version.fetch

Class Method Summary

Instance Method Summary

Macro Summary

Class Method Detail

def self.deserialize(xml : Node, type : U.class, ctx : SerializationContext | Nil = nil) forall U #

[View source]
def self.deserialize(xstr : String, type : Serializable.class, ctx : SerializationContext | Nil = nil) #

[View source]
def self.guard(name, &block) #

[View source]
def self.log #

[View source]
def self.log=(log) #

[View source]
def self.parse_number(text : String, int_class : Int.class) #

Parses decimal, binary, octal or hexadecimal number into a specified integer class.

Accepts numbers like 123 (decimal), 0123 (octal), 0xDEADBEEF (hex), 0b10010000 (binary)


[View source]
def self.parse_number(text : String, int_class : Float.class) #

[View source]

Instance Method Detail

def serialize(xml : Builder) #

[View source]
def serialize #

[View source]
def xml_name #

Returns XML compatible name of an object type


[View source]

Macro Detail

macro also_known_as(name) #

Generates an XML shortcut for type


[View source]
macro attribute(*props) #

A macro to declare serializable attributes

In contrast to serializable macro, which declares child nodes, this macro is used to declare XML attributes, so a declared attribute must be a String, Bool, Enum, or have a constructor from String (like floating point or integer values)

So, an example like this

class Foo
  include Nya::Serializable
  
  property foo = "bar", bar = SomeEnum::Value   

  attribute foo : String, bar : SomeEnum

  # ... other props here
end

Will give a following output:

<Foo foo="bar", bar="Value">
  ... other props here ...
</Foo>

NOTE Notice the property macro calls in the class declaration. Like the serializable macro, attribute also calls property accessors.

WARNING As with serializable, serializable class must have a default constructor, so properties must have a default value


[View source]
macro deserializator(&block) #

[View source]
macro serializable(*props) #

A macro to declare serializable properties

Must be called with TypeDeclarations, for example:

serializable foo : String, bar : Array(String), baz : Another::Serializable

serializable foobar : Array(Another::Serializable)

The above example will be serialized to XML like following:

... 
<foo>Foo content</foo>
<bar>
  <item>Bar 1</item>
  <item>Bar 2</item>
  ...
</bar>
<baz>
  <Another:Serializable>
    ...
  </Another:Serializable>
</baz>
<foobar>
  <Another:Serializable>
    ...
  </Another:Serializable>
</foobar>
...

NOTE To serialize or deserialize a property, an object should have property accessor methods, generated with a property macro call or manually written.

WARNING Serializable class must have a default constructor, so properties must have a default value


[View source]
macro serializator(&block) #

[View source]
macro translate_type(type) #

Translates Crystal type name into XML compatible tag name

Converts first part of a Fully::Qualified::Name into a XML namespace and joins the rest with underscores, like Fully:Qualified_Name. Converts generic type names like Generic(A, B) into Generic..A.B-.

WARNING This macro is intended for internal use, generic type conversion is currently not used in the library and may be removed in future.

NOTE Type must be a StringLiteral, Generic, Path, or just an unqualified MacroId. In the latter case, type name is just stringified without any conversion.


[View source]