Which Perl module would you recommend for JSON man

2019-01-19 11:55发布

As usual, I'm happy to deal with CPAN because it got all we need. As usual, I'm lost because there is plenty of stuff.

I can find the core JSON one by myself, and feel enthusiastic by a JSON::Tiny other.

My needs are very simple (parsing stuffs from the Open Library API) and, maybe someday, expose our own data.

Is there any other modules that you like for this task?

6条回答
Ridiculous、
2楼-- · 2019-01-19 12:20

JSON::XS would be a good module.

查看更多
Bombasti
3楼-- · 2019-01-19 12:21

I've started using Mojo::JSON every chance I get. The Mojolicious is easy to install and as Joel Berger showed in his answer, it comes with much more.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-19 12:22

I would recommend JSON::MaybeXS - Uses Cpanel::JSON::XS with a fallback to JSON::XS and JSON::PP.

Cpanel::JSON::XS has improvements over JSON::XS, so JSON::MaybeXS makes your code nice and portable.

Usually I would look to Task::Kensho if I'm not sure of which module to use for a specific situation, though they don't have JSON at the moment, I've reported it to them!

查看更多
欢心
5楼-- · 2019-01-19 12:28

JSON module works like a champ, but if you need a faster parser, use this one: JSON::XS, which requires a native compilation.

Note that JSON version 2.0 and above is merely a front end for JSON::XS (if installed) or JSON::PP (fallback).

查看更多
劫难
6楼-- · 2019-01-19 12:40

Since you say that you are getting the data from an online source, you might consider the Mojolicious tool suite. In that way you can get the data, parse it and maybe even use JSON pointers to extract info.

Basic:

#!/usr/bin/env perl

use strict;
use warnings;

use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
my $data = $ua->get('http://openlibrary.org/search.json?title=perl%20modules')
              ->res
              ->json;

With url constructor and JSON pointer:

#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;

use Mojo::URL;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
my $url = Mojo::URL->new('http://openlibrary.org/search.json')
                   ->query( title => 'perl modules' );

say $ua->get($url)
       ->res
       ->json('/docs/0/title_suggest');

Note that the json method on the response object either returns the whole parsed data structure or can take a pointer string (as in the second example) to return just a subset to get you going quickly. Enjoy.

查看更多
等我变得足够好
7楼-- · 2019-01-19 12:43

I always use JSON::XS. Complete, robust, proven, fast*, easy to use, and even a bit of flexibility if you need it.

It's probably the most used JSON parser, though most access it through JSON (but doing so risks using slower JSON::PP instead).

* — "An order of magnitude" faster than JSON::Tiny, according to JSON::Tiny's docs.

查看更多
登录 后发表回答