Not done on purpose, but the Mail gem includes a method for serializing methods for YAML, as well as for parsing messages from YAML. He does this by creating a Hash object and then serializing that object in JSON. It also means that it is easy to convert to JSON.
It will look like (ish) like this ...
require 'json' module Mail class Message def to_json(opts = {}) hash = {} hash['headers'] = {} header.fields.each do |field| hash['headers'][field.name] = field.value end hash['delivery_handler'] = delivery_handler.to_s if delivery_handler hash['transport_encoding'] = transport_encoding.to_s special_variables = [:@header, :@delivery_handler, :@transport_encoding] (instance_variables.map(&:to_sym) - special_variables).each do |var| hash[var.to_s] = instance_variable_get(var) end hash.to_json(opts) end end end
From the Message#to_yaml method of the Message#to_yaml mailbox.
You can use a similar approach in any language. If you use Ruby, it will go well with the existing tool (mail pearls).
source share