Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Friday February 01, 2008
04:38 AM

Bermuda - YAML and JSON Serialization

[ #35551 ]

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:

<?xml version="1.0" encoding="utf-8"?>
  <dummy version="3" name="Victoria" some_val="4">
  <personal_email>victoria@example.com</personal_email>
  <work_email>victoria@some_company.com</work_email>
</dummy>
---
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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.