Any suggestions for how to format an RFC822 email as JSON?
Not done it specifically, but the Mail gem includes a method for serializing methods to YAML, and also for parsing messages from YAML. It does this by constructing a Hash
object and then serializing that object to JSON. That also means it should be easy to convert to JSON.
This would prolly look (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 endend
From the Message#to_yaml
method of mikel's Mail gem.
You could take a similar approach in any language. If you're using Ruby though, this would fit great with your existing tooling (the Mail gem).