Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If a webservice offers responses in XML, YAML, and JSON formats, which is the best choice to consume? I'd normally choose XML and use XML::LibXML to parse. But I may eventually upload to CPAN, and would like to make it easier on users and avoid unecessary prereqs.

I took a look at many of the WebService::* modules and they mostly consume XML, and as expected, there's no consensus on an XML parser. There was one module, WebService::Eventful, which strangley enough, consumed all three formats: XML, YAML, and JSON.

I'm leaning towards YAML now, since some of the internal perl stuff is using it (e.g. META.yml), but am open to opinions.

  • Comment on Which format is best to consume from a webservice?

Replies are listed 'Best First'.
Re: Which format is best to consume from a webservice?
by clinton (Priest) on Aug 14, 2007 at 10:42 UTC
    For readability, I'd rule out JSON. So then you have to choose between XML and YAML. XML::LibXML is (probably) stricter and has been around for longer. It is also considerably heavier than YAML::Syck, which is light and fast.

    As far as cross platform usability, well probably six of one, a half dozen of the other.(See cpandeps for XML::LibXML vs cpandeps for YAML::Syck)

    Personally, I've found both modules very reliable. However, I would avoid the pure Perl YAML, as I've been bitten by bugs in it on more than one occasion.

    Clint

      After looking around a bit more, I like the idea of JSON::Any. Amazed there isn't a YAML::Any.
Re: Which format is best to consume from a webservice?
by moritz (Cardinal) on Aug 14, 2007 at 08:04 UTC
    This reminds of the Yahoo! API which offers these three formats.

    You can see the parser's depencies on cpandeps and decide for yourself.

      I just looked at some of Yahoo's apis and see they offer the PHP serialization format, which reminds me a lot of the bencode format. Lucky I don't have to deal with that.
Re: Which format is best to consume from a webservice?
by jbert (Priest) on Aug 14, 2007 at 09:38 UTC
    I guess it's a hot topic, you'll find (possibly strong) opinions on both sides.

    I think I'd go for the smallest format which preserves all needed information (as long as it doesn't bring other headaches with it).

    That's probably YAML, but maybe JSON. (Although the "just eval it" approach to JSON does give me the shivers. Do people really do that in AJAX? Or do they do the parse in javascript code too?)

      ... the "just eval it" approach to JSON does give me the shivers. Do people really do that in AJAX?

      For the most part: yes they do. Two reasons:

      1. it's by far the fastest way to "parse" complex data structures in javascript (though eval()ing very large strings in IE is still slow).

      2. you'll only receive the data from your own server anyway (can't access any other server via XMLHTTP), so you can already make sure the data is in the desired format on the server side.

Re: Which format is best to consume from a webservice?
by maspalio (Scribe) on Aug 14, 2007 at 09:53 UTC
    I'd wonder about two things. Do you want to use XPath? Do you want to validate against a schema?
      no to both. in fact, i was hoping that using one of the others would get me around the encoding and entity problems that frequently pop up with xml feeds.
Re: Which format is best to consume from a webservice?
by dmorgo (Pilgrim) on Aug 14, 2007 at 22:37 UTC
    I'll throw in one more vote for JSON, having used it on a recent project and had great success with that. The above mentioned readability concern is a non-issue imo, because the Perl JSON module has a built-in 'pretty' parameter that takes care of that:
    my $str = $json->objToJson($obj, {pretty => 1, indent => 4});
Re: Which format is best to consume from a webservice?
by CountZero (Bishop) on Aug 14, 2007 at 15:15 UTC
    I find YAML easy to use, especially if you want / need to translate the data straight into a Perl variable structure. XML of course is much more flexible and has uses beyond what YAML can provide.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Which format is best to consume from a webservice?
by DrHyde (Prior) on Aug 15, 2007 at 09:53 UTC
    I'd say give your users the choice. XML would make a sensible default, not because it's the best solution, but because it's the one people are most familiar with.
      Isn't the point of consuming the data in a WebService module supposed to isolate the user from the initial data? I process and give them perlish, meaningful data.
        What if some of your users already have YAML modules installed, others have XML installed, and others have JSON installed? What if for some reason a user *can't* install YAML (or JSON, or XML)?