50

I've been using the YAML format with reasonable success in the last 6 months or so.

However, the pure Perl implementation of the YAML parser is fairly fidgety to hand-write a readable file for and has (in my opinion) annoying quirks such as requiring a newline at end of the file. It's also gigantically slow compared to the rest of my program.

I'm pondering the next evolution of my project, and I'm considering using JSON instead (a mostly strict subset of YAML, as it turns out). But which format has the most community traction and effort in Perl?

Which appears today to be the better long-term format for simple data description in Perl, YAML or JSON, and why?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
  • 1
    Can you give an example of date you are talking about? – Ivan Nevostruev Dec 09 '09 at 20:48
  • Let's say all leaf data is generic perl scalars. I combine scalars in arrays and perl hashes. Arrays and hashes can have has elements arrays and hashes. This is pretty vanilla data structure stuff. :-) – Paul Nathan Dec 09 '09 at 20:51
  • 7
    I've found YAML::Tiny to be reliable, and pure Perl, as long as you're willing to accept its limitations. – Schwern Dec 09 '09 at 23:29
  • 1
    What else are you doing with the data, and who else has to consume it? There's no general answer, really. – brian d foy Dec 10 '09 at 18:14
  • `json` if you ever need to pass this data around. `yaml` only if you plan to edit by hnad (config files) or you want to display (e.g. config/results dump) – Dima Tisnek Sep 17 '14 at 06:06

7 Answers7

84

YAML vs JSON is something very much not settled in Perl, and I will admit I tend to be in the middle of that. I would advice that either is going to get you about as much community traction. I'd make the decision based on the various pros and cons of the formats. I break down the various data serializing options like so (I'm going to community wiki this so people can add to it):

YAML Pros

  • Human friendly, people write basic YAML without even knowing it
  • WYSIWYG strings
  • Expressive (it has the TMTOWDI nature)
  • Expandable type/metadata system
  • Perl compatible data types
  • Portable
  • Familiar (a lot of the inline and string syntax looks like Perl code)
  • Good implementations if you have a compiler (YAML::XS)
  • Good ability to dump Perl data
  • Compact use of screen space (possible, you can format to fit in one line)

YAML Cons

  • Large spec
  • Unreliable/incomplete pure Perl implementations
  • Whitespace as syntax can be contentious.

JSON Pros

  • Human readable/writable
  • Small spec
  • Good implementations
  • Portable
  • Perlish syntax
  • YAML 1.2 is a superset of JSON
  • Compact use of screen space
  • Perl friendly data types
  • Lots of things handle JSON

JSON Cons

  • Strings are not WYSIWYG
  • No expandability
  • Some Perl structures have to be expressed ad-hoc (objects & globs)
  • Lack of expressibility

XML Pros

  • Widespread use
  • Syntax familiar to web developers
  • Large corpus of good XML modules
  • Schemas
  • Technologies to search and transform the data
  • Portable

XML Cons

  • Tedious for humans to read and write
  • Data structures foreign to Perl
  • Lack of expressibility
  • Large spec
  • Verbose

Perl/Data::Dumper Pros

  • No dependencies
  • Surprisingly compact (with the right flags)
  • Perl friendly
  • Can dump pretty much anything (via DDS)
  • Expressive
  • Compact use of screen space
  • WYSIWYG strings
  • Familiar

Perl/Data::Dumper Cons

  • Non-portable (to other languages)
  • Insecure (without heroic measures)
  • Inscrutable to non-Perl programmers

Storable Pros

  • Compact? (don't have numbers to back it up)
  • Fast? (don't have numbers to back it up)

Storable Cons

  • Human hostile
  • Incompatible across Storable versions
  • Non-portable (to other languages)
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • There was a post somewhere on Stack Overflow about JSON being faster for a very simple, but large data structure than Storable. – Brad Gilbert Dec 10 '09 at 16:51
  • Storable's output will be portable if you use nfreeze() instead of freeze() – nick Oct 20 '10 at 13:00
  • @nick I meant not portable across languages. – Schwern Oct 24 '10 at 04:23
  • Benchmark results for JSON vs YAML are in one of the answers here: https://stackoverflow.com/questions/1726802/what-is-the-difference-between-yaml-and-json/62843005#62843005 – Chris Koknat Jul 15 '20 at 17:06
  • Note that both Data::Dumper and Storable have security issues because they may attempt to inflate Perl objects. That runs code and it might not be the code you expected. – brian d foy Oct 31 '20 at 05:28
13

As with most things, it depends. I think if you want speed and interoperability (with other languages), use JSON, in particular JSON::XS.

If you want something that's only ever going to be used by Perl modules, stick with YAML. It's much more common to find Perl modules on CPAN that support data description with YAML, or which depend on YAML, than JSON.

Note that I am not an authority and this opinion is based largely on hunch and conjecture. In particular, I have not profiled JSON::XS vs. YAML::XS. If I am offensively ignorant, I can only hope I will make someone irate enough to bring useful information to the discussion by correcting me.

Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
11

It's all about human-readability, if this is your main concern choose YAML:

YAML:

american:
  - Boston Red Sox
  - Detroit Tigers
  - New York Yankees
national:
  - New York Mets
  - Chicago Cubs
  - Atlanta Braves

JSON:

{
  "american": [
    "Boston Red Sox", 
    "Detroit Tigers", 
    "New York Yankees"
  ], 
  "national": [
    "New York Mets", 
    "Chicago Cubs", 
    "Atlanta Braves"
  ]
}
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
4

The pure-Perl YAML implementation (YAML module as opposed to YAML::Syck) seems to have some serious problems. I recently ran into issues where it could not process YAML documents with very long lines (32k characters or so).

YAML is able to store and load blessed variables and does so by default (The snippet below was copied from a *sepia-repl* buffer in Emacs):

I need user feedback!  Please send questions or comments to seano@cpan.org.
Sepia version 0.98.
Type ",h" for help, or ",q" to quit.
main @> use YAML
undef
main @> $foo = bless {}, 'asdf'
bless( {}, 'asdf' )
main @> $foo_dump = YAML::Dump $foo
'--- !!perl/hash:asdf {}
'
main @> YAML::Load $foo_dump
bless( {}, 'asdf' )

This is quite scary security-wise because untrusted data can be used to call any DESTROY method that has been defined in your application -- or any of the modules it uses.

The following short program demonstrates the problem:

#!/usr/bin/perl
use YAML;
use Data::Dumper;
package My::Namespace;
sub DESTROY {
    print Data::Dumper::Dumper \@_;
}
package main;
my $var = YAML::Load '--- !!perl/hash:My::Namespace
bar: 2
foo: 1
';

JSON does not allow this by default -- it is possible to serialize Perl "objects", but in order to do that, you have to define TO_JSON methods.

hillu
  • 9,423
  • 4
  • 26
  • 30
  • 2
    +1 for good info & the security note. – Paul Nathan Dec 10 '09 at 15:54
  • 1
    There is currently some work to move the Pure Perl YAML module to YAML::PP. Then having a new YAML module that automatically chooses one of the implementations for you. ( This is what the JSON module currently does ). – Brad Gilbert Dec 10 '09 at 16:54
  • Brad Gilbert: This seems a good thing. Are there any plans to break compatibility in favor of security -- at least for the `Load` function? – hillu Dec 10 '09 at 17:21
1

if you are considering JavaScript Object Notation, why not use "Perl Object Notation"?

JSON:

{"name": "bob", "parents": {"mother": "susan", "father": "bill"}, "nums": [1, 2, 3]}

Perl:

{name => "bob", parents => {mother => "susan", father => "bill"}, nums => [1, 2, 3]}
dolmen
  • 8,126
  • 5
  • 40
  • 42
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
  • 4
    (1)The parser for that is string eval; (2)Data::Dumper load/store requires some gnarly coupling between the loading and storing routine. Also and most importantly, 'PON' is not a standard interchange format. JSON is, and so is YML(within Perl). – Paul Nathan Dec 10 '09 at 15:57
1

I use YAML for tracking status of processes because I can read YML in the middle of the process. You (technically) need fully formed documents to read XML or JS. YAML is nice for tracking status because you can write lots of mini docs to a file. Otherwise, I usually go with XML or JS. Nice summary of pros & cons above, btw.

limeri
  • 121
  • 1
  • 7
0

You might also want to consider using Storable. You will likely get a very good speed boost with it. The trade-offs are:

  • the Storable format is binary and not human readable like JSON or YAML
  • Storable is not a pure Perl module (if that matters)
codelogic
  • 71,764
  • 9
  • 59
  • 54
  • In the context of speed for something like this, I suspect that "not pure Perl" is *better*. – Telemachus Dec 09 '09 at 21:18
  • In this specific context, due to circumstances out of my control, pure perl is required. But, I agree that not pure pure is almost certainly better for speed. – Paul Nathan Dec 09 '09 at 21:26
  • 3
    @Paul Nathan: if the problem is compiling modules which involve C extensions, Storable is a built-in. So there's no need to get it later. If you have Perl, you already have Storable. – Telemachus Dec 09 '09 at 21:37
  • Well then. Ignorance is exposed and reduced. :-) – Paul Nathan Dec 09 '09 at 21:48
  • @Paul: not at all. It's a common misunderstanding. Many, many modules are core or built-ins. You can browse through them here: http://perldoc.perl.org/index-modules-A.html – Telemachus Dec 09 '09 at 22:06
  • 4
    A friend recently did some benchmarks comparing Storable, JSON::XS and YAML::Syck for speed and JSON::XS came out faster by a long shot. – mpeters Dec 10 '09 at 01:44
  • Even though Storable is not 'pure perl', it _is_ a core module, so 'should' be already available within your perl installation. – ericslaw Nov 23 '11 at 16:25
  • Storable has various security issues: http://www.masteringperl.org/2012/12/the-storable-security-problem/ – brian d foy Sep 02 '13 at 21:25