如何将Moose对象转换为JSON以在Catalyst中使用? [英] How do I turn Moose objects into JSON for use in Catalyst?

查看:133
本文介绍了如何将Moose对象转换为JSON以在Catalyst中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将一系列Moose对象提供给 Catalyst :: View :: JSON . JSON :: XS无法编码受祝福的数据结构.我知道有 MooseX: :Storage :: Format :: JSON 可以-有点-做我想做的;但是,它似乎太重了.我要找的基本上是与 XXX.pm相同的信息. 提供.我只希望递归地保留原始数据结构,以便JSON::XS(C:V:JSON在内部使用的JSON::Any驱动程序)可以显示它.

I have a series of Moose objects that I'm looking to feed to JSON::XS by way of Catalyst::View::JSON. JSON::XS is unable to encode blessed data-structures. I know that there is MooseX::Storage::Format::JSON which can -- kinda -- do what I want; but, it seems pretty overly heavy. What I'm looking for is essentially the same information that XXX.pm provides. I just want the raw-data structures recursively unblessed so JSON::XS (the driver for JSON::Any that C:V:JSON uses internally) can display it.

Catalyst::View::JSONJSON::XSMoose对象一起使用的最佳方法是什么?看来我有四个明显的选择:

What is the best way go about using Catalyst::View::JSON and JSON::XS with Moose objects? It seems I have four obvious choices:

  1. 通过修补C:V:JSON以便从freeze读取内容,使Catalyst::View::JSON与Moose对象一起工作,如果公开的参数是Moose Object,则最终确定请求.
  2. 修补JSON::XS以回退到$obj->freeze if $obj->isa('Moose') && $obj->does('MooseX::Storage::Format::JSON')的返回值.我应该研究MX:S:F:JSON以确保JSON::Any和代理MX:S:F:JSON使用的类是JSON::XS(如果JSON::Any为Moose选择了不同的内部编码器,讨厌想到那些臭名昭著的bug.调用JSON::XS的对象.
  3. 找出如何递归的方法,并让Catalyst::View::JSON做它的事情.
  4. 完全不使用Catalyst::View::JSON.只需写入STDOUT $obj->freeze并手动完成请求即可..这似乎是最骇人听闻的事情.
  1. Make Catalyst::View::JSON work with Moose Objects, by patching C:V:JSON to read from freeze and finalize the request if the argument exposed is a Moose Object.
  2. Patch JSON::XS to fallback to return value of $obj->freeze if $obj->isa('Moose') && $obj->does('MooseX::Storage::Format::JSON'). I should look into MX:S:F:JSON to make sure that the class used by JSON::Any, and by proxy MX:S:F:JSON, is JSON::XS (hate to think of the bugs galore if JSON::Any choses a different internal encoder for the Moose object that JSON::XS is called to use.
  3. Figure out how to recursively-unbless and let Catalyst::View::JSON do its thing.
  4. Don't use Catalyst::View::JSON at all. Just write to STDOUT $obj->freeze and manually finalize requests.. This seems the most hackish.

我确定还有其他选择,有什么想法吗?我最好的选择是什么?

I'm sure there are some other options, any ideas? What is my best bet?

推荐答案

喜欢或讨厌它,我的最终解决方案是恢复到现已弃用的

Like it or hate it my ultimate solution was to revert back to the now deprecated JSON::Syck which does exactly what I want. Here is a quick non-Moose example of logical non-Moose serialization to JSON.

use JSON::Syck;
use URI;
use feature ':5.10';
say JSON::Syck::Dump(
 bless { foo => URI->new("http://www.evancarroll.com") }
)

我写了有关 JSON ::的作者XS 他并不是添加功能的游戏.这是来自 Marc Lehmann JSON :: XS维护者:

I wrote the author about JSON::XS he was not game for adding the functionality. Here is some of the text (taken out of context from numerous messages to show why this feature isn't there) from Marc Lehmann the JSON::XS maintainer:

否,因为您使用的是 JSON 和 不是 Perl . Perl显然有能力 代表任何Perl数据 结构,但不是JSON,因此您 需要告诉它该怎么做.许多 人们天真地要求类似 只是转储数据结构",但是 JSON甚至无法代表所有Perl 非对象的数据结构.

No, because you are using JSON and not Perl. Perl is obviously capable of representing any Perl data structure, but JSON is not, so you need to tell it what to do. Many people naively request something like "just dump the data structure", but JSON can't even represent all perl data structures that are non-objects.

...

您的问题是:为什么我不能 将此1MB jpeg图像作为ASCII文件发送 -仅仅因为ascii无法代表八位位组.

Your question is like: why can't I send this 1MB jpeg image as ascii file - simply because ascii cannot represent octets.

...

或者也许你只是想扔 一些垃圾,并期望JSON :: XS 做对您有利的事情(但错了 对于其他人)-根本没有 JSON :: XS神奇猜测的方法 想要什么.

Or maybe you justw ant to to throw some garbage at it and expect JSON::XS to do what is good for you (but wrong for other people) - there is simply no way for JSON::XS to magically guess what you want.

...

是TO_JSON.别的就是 疯狂的猜测.

TO_JSON is it. Anything else is just wild guessing.

...

那么也许你应该尝试一些 JSON实际上可以代表.投掷 一些对象层次结构,并希望 它会做正确的事情"是 显然是不良的软件工程-尝试 当您的协议时,不要诉诸黑客 需要JSON,然后发送JSON,不要 期望json库修复您的 首先是无效消息.

Then maybe you should try stuff that JSON can actually represent. Throwing some object hierarchy at it and hoping it would do "the right thing" is clearly bad software engineering - try not to resort hacks, when your protocl requires JSON, then send JSON, don't expect the json library to fix up your invalid message in the first place.

除了我不在乎之外,我不知道该如何应对.只是工作!像:: SYCK .我不希望对象完美地转换为JSON.但是,我认为只有80%的人希望它能正常工作.我在JSON导入中使用JSON,但在脑部手术期间未执行银行交易.最终,我不想使用特殊角色...我希望发送给它的任何东西对我来说都是不可思议的,使其转换为对响应jQuery JSON请求有用的级别.

I don't know how to respond to that, other than I DON'T CARE. JUST WORK! LIKE ::SYCK. I don't expect objects to transform to JSON perfectly. But, I think I fall in the 80% that just want it to work. I'm using JSON for jQuery import not performing banking transactions during brain surgery. Ultimately, I don't want to use a special role... I want anything sent to it to magically be transformed for me to a level that makes it useful in a response to a jQuery JSON request.

对不起,我错过了这些答复,直到有人说我正在使用无关的媒体进行拖钓. MooseX :: Storage对于非Moose类不起作用,我想要一种以JSON格式表示隐藏的通用方法.不幸的是,其中一些隐藏成员是Moose对象. XML :: Simple可以做到这一点,Data::Dumper可以做到这一点,JSON :: Syck可以做到这一点,列表不胜枚举-我只是想做到这一点.与Perl不一定必须是1:1,老实说,我希望它与JSON::Syck的默认方式相当接近.那么我在这里的论据是:如何使JSON::XS像现在的JSON::Syck一样工作"?而且,你的答案是你做不到.因此,我没有采用其他解决方案.编写代码要花钱,如果Syck已经做对了,我为什么还要写to_JSON ...我想说的是负担沉重的负担,以表明Syck正在序列化一种不适合的时尚理想的.另外,请记住JSON::Syck Audry 的种子,他绝不是巨魔,白痴或脑残";或其他任何被我抛弃的术语.我将以此结尾:缺少JSON::Syck的不良序列化路径以及所需的输出已经 just-working 使我相信这对我来说是一个不错的选择.而且,MooseX :: *:JSON对任意Moose对象有何不同之处?您为什么认为不能编写代码来接受Moose对象而不是对象上的方法?如果您有其他意见,请实质性地回答-我希望看到更好的答复.谢谢. (指向@jrockway和@Ether)

Sorry, I missed these replies until someone said I was trolling in an unrelated medium. MooseX::Storage doesn't work for non-Moose classes, I want a general way to represent the stash in a JSON format. Unfortunately, some of those members of the stash are Moose objects. XML::Simple can do this, Data::Dumper can do this, JSON::Syck can do this, the list goes on -- I just want it done. It doesn't have to be 1:1 with Perl, and to be honest I want it done fairly close to the way JSON::Syck does it by default. My argument here then, is, "How do I get JSON::XS to work exactly like JSON::Syck does currently"? And, your answer is you can't. So I've not employed a different solution. Writing code costs money, why would I want to write to_JSON if Syck is already doing it right... I'd like to say the burden is on the nay-sayers to show what Syck is serializing a fashion that isn't desirable. Also, do keep in mind JSON::Syck was seeded by Audry, who is by no means a troll, idiot, or "brain damaged"; or, any of the other terms that are being thrown my way. I will close with this: the lack of a bad serialization route of JSON::Syck and the desired output already just-working leads me to believe this is a good choice for me. And, what could MooseX::*:JSON be doing differently with an arbitrary Moose object? Why do you think that code can't be written to accept a Moose object rather than a method on it? If you think otherwise, answer with something of substance -- I'd like to see a better response. Thanks. (directed toward @jrockway, and @Ether)

这篇关于如何将Moose对象转换为JSON以在Catalyst中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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