github.com/devnote-dev/xmlt
0.3.0 / published Sep 12, 2022 / repository
A library for serializing and deserializing XML documents with classes and structs.
XML Transformer
A library for serializing and deserializing XML documents with classes and structs. This follows the same includable syntax used in the standard library's JSON and YAML modules.
Installation
- Add the dependency to your
shard.yml:
dependencies:
xmlt:
github: devnote-dev/xmlt
- Run
shards install
Basic Usage
require "xmlt"
xml_str = %(<Numbers><one>1</one><two>2</two><three>3</three></Numbers>)
hash = Hash(String, Int32).from_xml xml_str, root: "Numbers"
puts hash # => {"one" => 1, "two" => 2, "three" => 3}
puts hash.to_xml # => <one>1</one><two>2</two><three>3</three>
Structured Usage
require "xmlt"
class Animal
# include the module so we can (de)serialize it
include XMLT::Serializable
property name : String
@[XMT::Field(key: "species")]
property kind : String
@[XMLT::Field(omit_nil: true)]
property family : String?
def initialize(@name, @kind, @family = nil)
end
end
fox = Animal.new "fox", "mammal"
fox.to_xml # => <Animal><species>mammal</species></Animal>
fox.family = "dog"
fox.to_xml # => <Animal><species>mammal</species><family>dog</family></Animal>
Contributing
- Fork it (https://github.com/devnote-dev/xmlt/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Devonte - creator and maintainer
This repository is managed under the MIT license.
© 2022 devnote-dev
API
- Array(T)
An
Arrayis an ordered, integer-indexed collection of objects of type T. - Bool
Bool has only two possible values:
trueandfalse. - Char
A
Charrepresents a Unicode code point. - Deque(T)
A Deque ("double-ended queue") is a collection of objects of type T that behaves much like an Array.
- Float32
- Float64
- Int128
- Int16
- Int32
- Int64
- Int8
- Nil
The
Niltype has only one possible value:nil. - Object
Objectis the base type of all Crystal objects. - Path
A
Pathrepresents a filesystem path and allows path-handling operations such as querying its components as well as semantic manipulations. - String
A
Stringrepresents an immutable sequence of UTF-8 characters. - Symbol
A symbol is a constant that is identified by a name without you having to give it a numeric value.
- Tuple(*T)
A tuple is a fixed-size, immutable, stack-allocated sequence of values of possibly different types.
- Union(*T)
A union type represents the possibility of a variable or an expression having more than one possible type at compile time.
- XMLT
A library for serializing and deserializing XML documents with classes and structs.