如何将Perl对象转换为JSON,反之亦然 [英] How to convert Perl objects into JSON and vice versa

查看:184
本文介绍了如何将Perl对象转换为JSON,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在文件Point.pm中定义了Point对象,如下所示:

I have defined a Point object in a file Point.pm as following:

package Point;
sub new {
    my ($class) = @_;
    my $self = {
        _x => 0,
        _y => 0,
    };
    return bless $self => $class;
}

sub X {
    my ($self, $x) = @_;
    $self->{_x} = $x if defined $x;
    return $self->{_x};
}

sub Y {
    my ($self, $y) = @_;
    $self->{_y} = $y if defined $y;
    return $self->{_y};
}

1;

现在,当我使用 JSON 通过以下代码将对象转换为JSON时:

Now when I use JSON to convert the object to JSON by the following code:

use JSON;
use Point;

Point $p = new Point;
$p->X(20);
$p->Y(30);

my $json = encode_json $p;

我收到以下错误:

encountered object 'Point=HASH(0x40017288)', but neither allow_blessed nor convert_blessed settings are enabled at test.pl line 28

如何使用JSON模块在JSON与对象之间进行转换?

How do I convert to and from JSON to an object using the JSON module?

推荐答案

警告会告诉您大多数错误.除非您告诉 JSON 如何处理祝福引用(Perl对象), JSON 仅可处理无祝福的数据结构.

The warning tells you most of what is wrong. Unless you tell JSON how to handle blessed references(Perl objects), JSON handles only un-blessed data structures.

您可以 convert_blessed ,并且可以 allow_blessed ,它说:

You can convert_blessed and you can allow_blessed. For allow_blessed, it says:

如果$enable为false(默认值),则编码在遇到受祝福的对象时将引发异常.

If $enable is false (the default), then encode will throw an exception when it encounters a blessed object.

Point 是一个对象类,因此Point的实例是受祝福的引用,因此JSON的默认值是引发异常.

Point is an object class, thus an instance of Point is a blessed reference, and thus the default for JSON is to throw an exception.

如果启用 convert_blessed ,它将在你的对象.使用像Point这样的简单对象(不包含受祝福成员的对象),您可以像这样轻松地做到这一点:

If you enable convert_blessed, it will call a TO_JSON method on your object. With simple objects like Point (ones that contain no blessed members), you can do that as easily as:

sub TO_JSON { return { %{ shift() } }; }

如果必须下降结构,它将获得 lot 毛发.

If you have to descend a structure, it will get a lot hairier.

下面的评论中有人说我没有介绍如何从JSON中获取对象.

Somebody in the comments below said that I didn't cover how to get objects out of JSON.

基础很简单.所以在这里

The basics are simple. So here goes

my $object = bless( JSON->new->decode( $json_string ), 'ClassIWant' );

我主要介绍了防止的部分,该部分只是简单地将受祝福的对象序列化为JSON.

I mainly covered the part that prevents you from simply serializing a blessed object into JSON.

反序列化的基础很简单,就像序列化的基础很简单一样-一旦知道了窍门.方式上没有错误,只有找到所需内容并将其祝福给正确的班级的任务.

The basics of deserialization are simple, just like the basics of serialization are simple--once you know the trick. There is no error in the way, there is just the task of finding what you need and blessing it into the right class.

如果要将代码耦合到对象,那么您将知道必须祝福的内容以及必须祝福的内容.如果您想要完全解耦的代码,在Perl中,这比在JavaScript本身中更难或更容易.

If you want to have code coupled to the objects, then you'll know what has to be blessed and what it will have to be blessed into. If you want totally decoupled code, this is no harder or easier in Perl than it is in JavaScript itself.

您将不得不在JSON中序列化 marker .如果我需要类似的东西,我将在受祝福的对象中插入一个'__CLASS__'字段.反序列化时,我将遍历整个结构并为所有类似的事情祝福:

You're going to have to serialize a marker in the JSON. If I need something like this, I will insert a '__CLASS__' field into the blessed objects. And when deserializing, I will descend through the structure and bless everything like this:

 bless( $ref, delete $ref->{__CLASS__} );

但是正如我所说,在Perl中这并不容易,也很难,因为JSON对所有语言都提出了相同的挑战.

But as I said, this is no easier or harder in Perl, because JSON presents the same challenge to all languages.

正如Schwern在顶部的评论中所建议的那样,YAML更好地用于序列化和反序列化对象,因为它具有符号. JSON为您提供了一个或多个关联数组.

As Schwern suggested in his comment up top, YAML is much better built for serializing and deserializing objects, because it has notation for it. JSON gives you associative arrays or arrays.

这篇关于如何将Perl对象转换为JSON,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆