Bermuda - YAML and JSON Serialization

Ovid on 2008-02-01T09:38:34

It's dawned on me that I get YAML and JSON serialization for free now. My default may not work for your default (in fact, it probably won't), but here's what I have:

print $friend->to_xml;
print $friend->to_yaml;
print $friend->to_json;

That outputs:


  
  victoria@example.com
  victoria@some_company.com

---
attributes:
  name: Victoria
  some_val: 4
  version: 3
elements:
  -
    attributes: {}
    name: personal_email
    value: victoria@example.com
  -
    attributes: {}
    name: work_email
    value: victoria@some_company.com
{
  "elements" : [
    {
      "value" : "victoria@example.com",
      "name" : "personal_email",
      "attributes" : {

      }
    },
    {
      "value" : "victoria@some_company.com",
      "name" : "work_email",
      "attributes" : {

      }
    }
  ],
  "name" : "dummy",
  "attributes" : {
    "version" : 3,
    "name" : "Victoria",
    "some_val" : 4
  }
}

I'll probably have to adjust the code to optionally strip out empty elements and attributes since those look strange in the YAML and JSON.

Update: no, just serializing things directly into JSON and YAML is lousy. This clearly needs work.