crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.36k stars 1.62k forks source link

XML Serialization #12376

Open jkthorne opened 2 years ago

jkthorne commented 2 years ago

Crystal should have an XML::Serializable module I have a draft that was derived from an API I integrated with.

An example of adding serialization include XML::Serializable.

class Foo
  include XML::Serializable
end

One question is what default pattern should be used. Should the values be gathered from the text content or the attributes of the XML document. I was integrating with Rail active model serializes and that puts all the values in the text content.

I think you should be able to customize the the serialization to pull from attributes. example

class Foo
  include XML::Serializable

  @[XML::Element(src: Attribute)]
end

XML formats seem to have different ways to store types. I dont know if we should serialize and deserialize XML with types in the document.

XML serailzers: YAXLib: text ExtendedXmlSerializer: text XMLEncoder: text with types XmlMapper: text

Blacksmoke16 commented 2 years ago

The library we use at work has a multitude of attributes/annotations to control the exact behavior of how the XML is (de)serialized. E.g. https://jmsyst.com/libs/serializer/master/reference/annotations#xmlattribute which tells the serializer it should (de)serialize the value to/from an attribute versus child node. That could be a good source of inspiration.