I'll try to describe the issue again. (It applies not only to expand method, but also saving the unicode strings in json(b) type via Mojo::Pg)
In this example I'll use next table with text and jsonb columns:
test=> \d t
Table "public.t"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
t | text |
j | jsonb |
test=>
Following script insert data into this table:
use Mojo::Base -strict;
use Mojo::Pg;
use JSON::XS;
my $db = Mojo::Pg->new('postgresql://and@/test')->db;
my $json = JSON::XS->new->ascii(1);
$db->query('insert into t (id, t, j) values (1, ?, ?)', '♥', {json => {'♥' => '♥'}});
$db->query('insert into t (id, t, j) values (2, ?, ?)', '♥', $json->encode({'♥' => '♥'}));
After executing the script, data in the table look like this:
test=> select * from t;
id | t | j
----+---+--------------------------
1 | ♥ | {"â\u0099¥": "â\u0099¥"}
2 | ♥ | {"♥": "♥"}
(2 rows)
test=>
Upon select the value (that correctly stored in postgres), you get an error while expand:
use Mojo::Base -strict;
use Mojo::Pg;
my $db = Mojo::Pg->new('postgresql://and@/test')->db;
say $db->query('select * from t where id = 2')->expand->hash->{j}{'♥'};
Input is not UTF-8 encoded at /home/and/perl5/perlbrew/perls/perl-5.20.0/lib/site_perl/5.20.0/Mojo/Pg/Results.pm line 51.
$
This is because the scalar already with wide characters and UTF8 flag (converted by DBD::Pg, I guess):
use Mojo::Base -strict;
use Mojo::Pg;
use Devel::Peek;
my $db = Mojo::Pg->new('postgresql://and@/test')->db;
my $a = $db->query('select * from t where id = 2')->hash->{j};
Dump $a;
SV = PV(0x254c3c0) at 0x15b19f8
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK,UTF8)
PV = 0x25796a0 "{\"\342\231\245\": \"\342\231\245\"}"\0 [UTF8 "{"\x{2665}": "\x{2665}"}"]
CUR = 14
LEN = 16
COW_REFCNT = 0
$
So we need encode data in UTF8 and then use decode_json while expand.