Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use structure in TO_JSON method when available #38

Open
dannymk opened this issue Nov 2, 2018 · 8 comments
Open

Use structure in TO_JSON method when available #38

dannymk opened this issue Nov 2, 2018 · 8 comments

Comments

@dannymk
Copy link

dannymk commented Nov 2, 2018

Perhaphs add a sub that takes an instance that can actually return a structure that only includes items that we want in the output:

multi sub to-json ( $_ where *.can('TO_JSON') ) {
    to-json .TO_JSON
}

see: https://stackoverflow.com/questions/53110521/perl6-class-to-json-exclude-properties/53120677#53120677

@moritz
Copy link
Owner

moritz commented Nov 3, 2018

Is there any consensus among the various Perl 6 JSON modules as to calling such a method, and if yes, how to call it?

Personally, I'd prefer AS-JSON, as TO implies a mutation of the invocant.

@dannymk
Copy link
Author

dannymk commented Nov 3, 2018

In some of the Perl5 modules the documentation reads: "If $enable is true (or missing), then encode, upon encountering a blessed object, will check for the availability of the TO_JSON method on the object's class. If found, it will be called in scalar context and the resulting scalar will be encoded instead of the object."

@dannymk
Copy link
Author

dannymk commented Dec 7, 2018

@moritz are you planning on incorporating the above patch into the module?

@moritz
Copy link
Owner

moritz commented Dec 11, 2018

@dannymk not before there is any consensus with, or at least data from, other Perl 6 JSON modules

@dannymk
Copy link
Author

dannymk commented Dec 21, 2018

Where are we supposed to get this consensus from?

@moritz
Copy link
Owner

moritz commented Dec 21, 2018

From the authors of the other JSON-related p6 modules

@drforr
Copy link

drforr commented Mar 20, 2019

Not speaking as an author of a JSON module here, but certainly of some other Perl 6 modules. Rather than hard-coding a method name, I'd suggest something like .to-json( $obj, :filter('method-name') ) or using a dynamic variable... $*JSON-FILTER-NAME='method-name'; $foo.to-json( $obj ); Both of those methods leave the choice of the filtering method to the user, and the dynamic variable means that you don't even need a 'multi method' variant.

@drforr
Copy link

drforr commented Mar 20, 2019

Another suggestion that feels even more Perl6ish to me would be something like this:

class MyObject {
  has $.attribute;
  method JSON returns JSONStr { # your package could create this JSONStr subtype, optional
    as-json( self, { #`{ Your filter code here } } );
  }
}
# code passes...
my $obj = MyObject.new( :attribute("hello") );
status_ok( $obj.JSON ); # Return JSONified output, just like you'd say $obj.Str to stringify it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants