简体   繁体   English

这些JSON Perl模块之间有什么区别?

[英]What's the difference between these JSON Perl modules?

What's the difference between the Perl JSON modules below? 下面的Perl JSON模块有什么区别?

I have come across JSON::PP and JSON::XS . 我遇到过JSON :: PPJSON :: XS The documentation of JSON::PP says it is compatible with JSON::XS . JSON :: PP的文档说它与JSON :: XS兼容。 What does that mean? 那是什么意思?

I am not sure what the difference between them are, let alone which of them to use. 我不确定它们之间有什么区别,更不用说它们中的哪一个了。 Can someone clarify? 有人可以澄清吗?

Perl modules sometimes have different implementations. Perl模块有时会有不同的实现。 The ::PP suffix is for the Pure Perl implementation (ie for portability), the ::XS suffix is for the C-based implementation (ie for speed), and JSON is just the top-level module itself (ie the one you actually use). ::PP后缀用于Pure Perl实现(即可移植性), ::XS后缀用于基于C的实现(即速度),而JSON只是顶层模块本身(即你的那个)实际上使用)。

As noted by @Quentin, this site has a good description of them. 正如@Quentin所指出的, 这个网站对它们有很好的描述。 To quote: 报价:

JSON JSON

JSON.pm is a wrapper around JSON::PP and JSON::XS - it also does a bunch of moderately crazy things for compatibility reasons, including extra shim code for very old perls [...] JSON.pm是JSON::PPJSON::XS的包装器 - 出于兼容性原因,它还会做一些中等程度的疯狂事情,包括非常旧的perls的额外填充代码[...]

JSON::PP JSON :: PP

This is the standard pure perl implementation, and if you're not performance dependent, there's nothing wrong with using it directly [...] 这是标准的纯perl实现,如果你不依赖于性能,直接使用它没有任何问题[...]

JSON::XS JSON :: XS

Ridiculously fast JSON implementation in C. Absolutely wonderful [...] 在C中实现快速JSON实施。绝对精彩[...]

As you can see, just installing the top-level JSON module should do it for you. 如您所见,只需安装顶级JSON模块就可以为您完成。 The part about compatibility just means that they both do the same thing, ie you should get the same output from both. 关于兼容性的部分只是意味着它们都做同样的事情,即你应该从两者得到相同的输出。

I installed the Perl JSON module a few years ago on a RHEL server I managed and it was a really straightforward process: just install (or build) the module from the CPAN site and you're done. 几年前我在我管理的RHEL服务器上安装了Perl JSON模块,这是一个非常简单的过程:只需从CPAN站点安装(或构建)模块就可以了。

Installing should be a simple case of either using the OS package manager (if in GNU/Linux), using the cpan utility, or building from source. 安装应该是使用OS包管理器(如果在GNU / Linux中),使用cpan实用程序或从源构建的简单情况。 The OS package manager is recommended, as it helps keep things updated automatically. 建议使用OS包管理器,因为它有助于自动更新。

To verify that it's installed, just try the following command from the terminal (assuming GNU/Linux): 要验证它是否已安装,只需从终端尝试以下命令(假设为GNU / Linux):

$ perl -e 'use JSON;'

If it doesn't complain, then you should be good to go. 如果它没有抱怨,那么你应该好好去。 If you get errors, then you should get ready to go in an adventure. 如果你遇到错误,那么你应该准备好去冒险。

You can install JSON module, cpan install JSON 您可以安装JSON模块,cpan install JSON

use JSON;

my $result = from_json($json);
if($result->{field})
{
  # YOUR CODE
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM