module Nya::Serializable
Defined in:
nya_serializable/serializable.crnya_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
- .deserialize(xml : Node, type : U.class, ctx : SerializationContext | Nil = nil) forall U
- .deserialize(xstr : String, type : Serializable.class, ctx : SerializationContext | Nil = nil)
- .guard(name, &block)
- .log
- .log=(log)
-
.parse_number(text : String, int_class : Int.class)
Parses decimal, binary, octal or hexadecimal number into a specified integer class.
- .parse_number(text : String, int_class : Float.class)
Instance Method Summary
- #serialize(xml : Builder)
- #serialize
-
#xml_name
Returns XML compatible name of an object type
Macro Summary
-
also_known_as(name)
Generates an XML shortcut for type
-
attribute(*props)
A macro to declare serializable attributes
- deserializator(&block)
-
serializable(*props)
A macro to declare serializable properties
- serializator(&block)
-
translate_type(type)
Translates Crystal type name into XML compatible tag name
Class Method Detail
Parses decimal, binary, octal or hexadecimal number into a specified integer class.
Accepts numbers like 123
(decimal), 0123
(octal), 0xDEADBEEF
(hex),
0b10010000
(binary)
Instance Method Detail
Macro Detail
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
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
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.