首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较JSON和Test::Deep?

如何比较JSON和Test::Deep?
EN

Stack Overflow用户
提问于 2020-01-20 07:08:02
回答 1查看 162关注 0票数 2

当我运行以下代码时:

代码语言:javascript
复制
use strict;
use warnings;
use Test::Deep;
use Test::Deep::JSON;

cmp_deeply(json("{}"), json("{}"));

我得到了

代码语言:javascript
复制
Found a special comparison in $data
You can only use specials in the expects structure at /usr/local/share/perl/5.26.1/Test/Deep.pm line 348.
    Test::Deep::descend(Test::Deep::JSON=HASH(0x5559ec3b3810), Test::Deep::JSON=HASH(0x5559ecb236a8)) called at /usr/local/share/perl/5.26.1/Test/Deep.pm line 221
    Test::Deep::cmp_details(Test::Deep::JSON=HASH(0x5559ec3b3810), Test::Deep::JSON=HASH(0x5559ecb236a8)) called at /usr/local/share/perl/5.26.1/Test/Deep.pm line 202
    Test::Deep::cmp_deeply(Test::Deep::JSON=HASH(0x5559ec3b3810), Test::Deep::JSON=HASH(0x5559ecb236a8)) called at jsondiff.pl line 6

为什么会这样?我如何比较两个JSON字符串?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-20 08:42:00

代码语言:javascript
复制
use Test::More tests => 1;

use Test::Deep;
use Test::Deep::JSON;

my $json_to_check = '{}';

cmp_deeply($json_to_check, json({}));

如果您想从JSON开始,只需在将其传递给json之前对其进行解码。

代码语言:javascript
复制
use feature qw( state );

use Test::More tests => 1;

use Test::Deep;
use Test::Deep::JSON;

use Cpanel::JSON::XS qw( );

sub from_json {
   state $json = Cpanel::JSON::XS->new();
   return $json->decode($_[0]);
}

my $json_to_check = '{}';

cmp_deeply($json_to_check, json(from_json('{}')));
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59815231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档