| 1 | package Langertha::Role::JSON; | ||||||||||
| 2 | # ABSTRACT: Role for JSON | ||||||||||
| 3 | 178 | our $VERSION = '0.503'; | |||||||||
| 4 | 178 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 178 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 178 | ▶ | use Encode qw( encode_utf8 decode_utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6601 | ▶ | sub json { shift->_json } | ||||||||
|
Subroutine
json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | sub decode_json_text { | ||||||||
|
Subroutine
decode_json_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 11 | 2 | my ( $self, $text ) = @_; | |||||||||
| 12 | 2 | ▶ | return undef unless defined $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 1 | return $self->_json->decode(encode_utf8($text)); | |||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | 212 | ▶ | sub encode_json_text { | ||||||||
|
Subroutine
encode_json_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 18 | 212 | my ( $self, $data ) = @_; | |||||||||
| 19 | 212 | return decode_utf8( $self->_json->encode($data) ); | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | 178 | has _json => ( | |||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | lazy_build => 1, | ||||||||||
| 27 | ); | ||||||||||
| 28 | # convert_blessed => 1 lets the encoder serialize the distribution's value | ||||||||||
| 29 | # objects (Usage, Cost, Tool, ToolCall, ToolChoice, UsageRecord, ...) through | ||||||||||
| 30 | # their TO_JSON, matching what those classes' POD calls "the house default". | ||||||||||
| 31 | # Plain request bodies carry no blessed values, so this changes nothing for | ||||||||||
| 32 | # them; it only turns a croak into a serialization when a caller hands a value | ||||||||||
| 33 | # object to $engine->json. -- karr k120 | ||||||||||
| 34 | 2556 | ▶ | sub _build__json { JSON::MaybeXS->new( utf8 => 1, canonical => 1, convert_blessed => 1 ) } | ||||||||
|
Subroutine
_build__json: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | 178 | 1; | |||||||||
| 38 | |||||||||||
| 39 | __END__ | ||||||||||
| 40 | |||||||||||
| 41 | =pod | ||||||||||
| 42 | |||||||||||
| 43 | =encoding UTF-8 | ||||||||||
| 44 | |||||||||||
| 45 | =head1 NAME | ||||||||||
| 46 | |||||||||||
| 47 | Langertha::Role::JSON - Role for JSON | ||||||||||
| 48 | |||||||||||
| 49 | =head1 VERSION | ||||||||||
| 50 | |||||||||||
| 51 | version 0.503 | ||||||||||
| 52 | |||||||||||
| 53 | =head2 decode_json_text | ||||||||||
| 54 | |||||||||||
| 55 | my $data = $engine->decode_json_text($perl_string); | ||||||||||
| 56 | |||||||||||
| 57 | Decodes a JSON string that is already a Perl-Unicode string (e.g. a value | ||||||||||
| 58 | pulled out of an already-decoded response tree, or a substring extracted | ||||||||||
| 59 | from assistant-produced text). The shared L<JSON::MaybeXS> instance is | ||||||||||
| 60 | configured with C<utf8 =E<gt> 1> and expects raw bytes, so this helper | ||||||||||
| 61 | UTF-8-encodes the text before delegating to it. Use this instead of | ||||||||||
| 62 | C<< $self->json->decode >> whenever the source is Perl-Unicode rather | ||||||||||
| 63 | than the raw HTTP body. | ||||||||||
| 64 | |||||||||||
| 65 | =head2 encode_json_text | ||||||||||
| 66 | |||||||||||
| 67 | my $text = $engine->encode_json_text($data); | ||||||||||
| 68 | |||||||||||
| 69 | The mirror of L</decode_json_text>: encodes C<$data> to a JSON I<character> | ||||||||||
| 70 | string, for JSON that travels as a string value inside another JSON document | ||||||||||
| 71 | or inside prompt text (a hermes tool prompt, AKI's C<chat_context>, a lifted | ||||||||||
| 72 | structured-output C<content>). The request body is encoded to UTF-8 bytes | ||||||||||
| 73 | exactly once, by L</json> at the transport; a nested value encoded to bytes | ||||||||||
| 74 | here would be encoded a second time there. Same settings as L</json> | ||||||||||
| 75 | (C<canonical>, C<convert_blessed>). | ||||||||||
| 76 | |||||||||||
| 77 | =head2 json | ||||||||||
| 78 | |||||||||||
| 79 | my $data = $engine->json->decode($json_string); | ||||||||||
| 80 | my $json_string = $engine->json->encode($data); | ||||||||||
| 81 | |||||||||||
| 82 | Returns the shared L<JSON::MaybeXS> instance configured with C<utf8>, | ||||||||||
| 83 | C<canonical>, and C<convert_blessed> encoding. Used internally by | ||||||||||
| 84 | L<Langertha::Role::HTTP> and L<Langertha::Role::Streaming> for all JSON | ||||||||||
| 85 | serialization. C<convert_blessed> means the distribution's value objects | ||||||||||
| 86 | (with their C<TO_JSON> methods) serialize through this encoder instead of | ||||||||||
| 87 | croaking. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 SEE ALSO | ||||||||||
| 90 | |||||||||||
| 91 | =over | ||||||||||
| 92 | |||||||||||
| 93 | =item * L<Langertha::Role::HTTP> - HTTP role that requires this | ||||||||||
| 94 | |||||||||||
| 95 | =item * L<JSON::MaybeXS> - The JSON backend used | ||||||||||
| 96 | |||||||||||
| 97 | =back | ||||||||||
| 98 | |||||||||||
| 99 | =head1 SUPPORT | ||||||||||
| 100 | |||||||||||
| 101 | =head2 Issues | ||||||||||
| 102 | |||||||||||
| 103 | Please report bugs and feature requests on GitHub at | ||||||||||
| 104 | L<https://github.com/Getty/langertha/issues>. | ||||||||||
| 105 | |||||||||||
| 106 | =head2 IRC | ||||||||||
| 107 | |||||||||||
| 108 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 109 | |||||||||||
| 110 | =head1 CONTRIBUTING | ||||||||||
| 111 | |||||||||||
| 112 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 113 | |||||||||||
| 114 | =head1 AUTHOR | ||||||||||
| 115 | |||||||||||
| 116 | Torsten Raudssus <getty@cpan.org> | ||||||||||
| 117 | |||||||||||
| 118 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 119 | |||||||||||
| 120 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||
| 121 | |||||||||||
| 122 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 123 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||