Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the JSON CPAN distribution.

Report information
The Basics
Id:
54321
Status:
open
Priority:
Low/Low
Queue:

People
Owner:
Nobody in particular
Requestors:
rjbs [...] cpan.org
Cc:
AdminCc:

BugTracker
Severity:
Wishlist
Broken in:
(no value)
Fixed in:
(no value)



Subject: allow a per-JSON object to-json converter
I absolutely love JSON.pm. I use it daily, all the time, and it never does anything wrong for me. Thanks! It has only one problem which has bothered me for quite some time, and I would *love* to see it addressed: I would like a way to provide a TO_JSON-like callback to my JSON object. I do not want to create a UNIVERSAL::TO_JSON, as adding methods to UNIVERSAL tends to lead to horrible stupid problems later. Instead, I would like a way to say: ->convert_blessed(1) ->convert_blessed_fallback(sub { ... }); Is this an acceptable proposal? While it would be fantastic if it got coded while I wasn't looking, I would be willing to have a pass at it, if it's something you'd accept. Thanks again for JSON.pm! -- rjbs
RT-Send-CC: makamaka.donzoko@gmail.com
On 2010-2月-04 木 14:50:29, RJBS wrote:
Show quoted text
> I absolutely love JSON.pm. I use it daily, all the time, and it never > does anything wrong for > me. Thanks! > > It has only one problem which has bothered me for quite some time, and > I would *love* to > see it addressed: I would like a way to provide a TO_JSON-like > callback to my JSON object. I > do not want to create a UNIVERSAL::TO_JSON, as adding methods to > UNIVERSAL tends to lead > to horrible stupid problems later. Instead, I would like a way to > say: > > ->convert_blessed(1) > ->convert_blessed_fallback(sub { ... }); > > Is this an acceptable proposal? While it would be fantastic if it got > coded while I wasn't > looking, I would be willing to have a pass at it, if it's something > you'd accept. > > Thanks again for JSON.pm!
I will accept your proposal. However, I send JSON::XS author this report for XS comptibility. Please wait for a while. Regard,
On further reflection, a better interface would be: ->convert_blessed(1) ->convert_blessed_with($method) By default, not supplying the second call would mean $method was TO_JSON. $method could be an alternate method name or a coderef. Things would call $blessed_item->$method. -- rjbs
So, I've waited a good while. Any news? :-) -- rjbs
On 2012-07-13 19:24:52, RJBS wrote:
Show quoted text
> So, I've waited a good while. Any news? :-)
It's been six more months. Would it be productive were I to talk to Marc L about this? -- rjbs
On Fri Feb 01 01:31:48 2013, RJBS wrote:
Show quoted text
> On 2012-07-13 19:24:52, RJBS wrote:
> > So, I've waited a good while. Any news? :-)
> > It's been six more months. Would it be productive were I to talk to > Marc L about this?
Hi. Sorry for the way too long silence. Do you still need this feature? Or rather, do you still think of any clear benefits enough to convince Marc (and maybe Reini) when you can get almost the same result with something like this? my $json = do { no warnings qw/once redefine/; local *UNIVERSAL::TO_JSON = sub { ... }; encode_json( $your_data ); }; I understand the beauty, but I'm not so sure if we really need this now when TO_JSON is so widely accepted (even though makamaka said he would accept this proposal). Might it be enough to improve pod with the above example? (note that we'd need something to make -convert_blessed_universally less harmful, and this would be an option. )
On 2016-09-09 08:31:44, ISHIGAKI wrote:
Show quoted text
> Do you still need this feature?
I can live without it. I just think it is, at the very least, ugly to have part of the configuration for a JSON object stored not in the object, but in UNIVERSAL. -- rjbs
On Mon Sep 12 00:29:24 2016, RJBS wrote:
Show quoted text
> On 2016-09-09 08:31:44, ISHIGAKI wrote:
> > Do you still need this feature?
> > I can live without it. I just think it is, at the very least, ugly to > have part of the configuration for a JSON object stored not in the > object, but in UNIVERSAL.
Hmmm. Further investigation revealed a (Cpanel::)JSON::XS object is a blessed scalar reference, though a JSON::PP object is a blessed hash reference. So storing a callback into a JSON.pm object may not be that easy...


This service runs on Request Tracker, is sponsored by The Perl Foundation, and maintained by Best Practical Solutions.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.