| 1 | package Langertha::Role::JSON; | ||||||||||
| 2 | # ABSTRACT: Role for JSON | ||||||||||
| 3 | 34 | our $VERSION = '0.502'; | |||||||||
| 4 | 34 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 34 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 34 | ▶ | use Encode qw( encode_utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 159 | ▶ | sub json { shift->_json } | ||||||||
|
Subroutine
json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 9 | |||||||||||
| 10 | sub decode_json_text { | ||||||||||
| 11 | 16 | ▶ | my ( $self, $text ) = @_; | ||||||||
|
Subroutine
decode_json_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 12 | 16 | ▶ | return undef unless defined $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 15 | return $self->_json->decode(encode_utf8($text)); | |||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 34 | has _json => ( | |||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | lazy_build => 1, | ||||||||||
| 21 | ); | ||||||||||
| 22 | 123 | ▶ | sub _build__json { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } | ||||||||
|
Subroutine
_build__json: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 34 | 1; | |||||||||
| 26 | |||||||||||
| 27 | __END__ | ||||||||||
| 28 | |||||||||||
| 29 | =pod | ||||||||||
| 30 | |||||||||||
| 31 | =encoding UTF-8 | ||||||||||
| 32 | |||||||||||
| 33 | =head1 NAME | ||||||||||
| 34 | |||||||||||
| 35 | Langertha::Role::JSON - Role for JSON | ||||||||||
| 36 | |||||||||||
| 37 | =head1 VERSION | ||||||||||
| 38 | |||||||||||
| 39 | version 0.502 | ||||||||||
| 40 | |||||||||||
| 41 | =head2 decode_json_text | ||||||||||
| 42 | |||||||||||
| 43 | my $data = $engine->decode_json_text($perl_string); | ||||||||||
| 44 | |||||||||||
| 45 | Decodes a JSON string that is already a Perl-Unicode string (e.g. a value | ||||||||||
| 46 | pulled out of an already-decoded response tree, or a substring extracted | ||||||||||
| 47 | from assistant-produced text). The shared L<JSON::MaybeXS> instance is | ||||||||||
| 48 | configured with C<utf8 =E<gt> 1> and expects raw bytes, so this helper | ||||||||||
| 49 | UTF-8-encodes the text before delegating to it. Use this instead of | ||||||||||
| 50 | C<< $self->json->decode >> whenever the source is Perl-Unicode rather | ||||||||||
| 51 | than the raw HTTP body. | ||||||||||
| 52 | |||||||||||
| 53 | =head2 json | ||||||||||
| 54 | |||||||||||
| 55 | my $data = $engine->json->decode($json_string); | ||||||||||
| 56 | my $json_string = $engine->json->encode($data); | ||||||||||
| 57 | |||||||||||
| 58 | Returns the shared L<JSON::MaybeXS> instance configured with C<utf8> and | ||||||||||
| 59 | C<canonical> encoding. Used internally by L<Langertha::Role::HTTP> and | ||||||||||
| 60 | L<Langertha::Role::Streaming> for all JSON serialization. | ||||||||||
| 61 | |||||||||||
| 62 | =head1 SEE ALSO | ||||||||||
| 63 | |||||||||||
| 64 | =over | ||||||||||
| 65 | |||||||||||
| 66 | =item * L<Langertha::Role::HTTP> - HTTP role that requires this | ||||||||||
| 67 | |||||||||||
| 68 | =item * L<JSON::MaybeXS> - The JSON backend used | ||||||||||
| 69 | |||||||||||
| 70 | =back | ||||||||||
| 71 | |||||||||||
| 72 | =head1 SUPPORT | ||||||||||
| 73 | |||||||||||
| 74 | =head2 Issues | ||||||||||
| 75 | |||||||||||
| 76 | Please report bugs and feature requests on GitHub at | ||||||||||
| 77 | L<https://github.com/Getty/langertha/issues>. | ||||||||||
| 78 | |||||||||||
| 79 | =head2 IRC | ||||||||||
| 80 | |||||||||||
| 81 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 82 | |||||||||||
| 83 | =head1 CONTRIBUTING | ||||||||||
| 84 | |||||||||||
| 85 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 86 | |||||||||||
| 87 | =head1 AUTHOR | ||||||||||
| 88 | |||||||||||
| 89 | Torsten Raudssus <getty@cpan.org> | ||||||||||
| 90 | |||||||||||
| 91 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 92 | |||||||||||
| 93 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||
| 94 | |||||||||||
| 95 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 96 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||