dmitigr/pgfe

jsonb in binary mode

Closed this issue · 2 comments

hello it's me again
👨

when using jsonb type (oid 3802) the retrieved value has an additional first character '<U+0001>'. It does not happen in the in text mode.

example query:

SELECT '{"a":1, "b":2}'::jsonb as "my_little_json"
conn->set_result_format(dmitigr::pgfe::Data_format::binary);

I retrieve value as :

const std::string &string = pgfe::to<std::string>(row[i]);

Hey,

Types like jsonb has it's own internal binary representation defined on the backend side. Binary representations for complex data types might change from release to release, so the text format is usually the more safe and portable choice for them. That's why in Pgfe binary mode is supported only for base types (boolean, int4, etc etc) and text (as std::string).

By doing this:

auto str = pgfe::to<std::string>(row[i]);

after retrieving the data in binary format you just fills the instance of std::string with raw bytes -- internal representation of jsonb value.

Of course, you can implement you own Jsonb type on the client side and integrate this type with Pgfe easily. (Pgfe supports custom types integration via struct template Conversions.)