minato_codec_scalar (minato v0.18.6)
View SourceCodecs for the scalar types that are not temporal and not numeric:
bool, bytea, int2, int4, int8, float4, float8, text, varchar,
json, jsonb and uuid.
Internal to minato. The supported surface is minato_codec.
Wire formats
boolis one byte, 0 or 1, in binary, andtorfin text.byteais the raw bytes in binary. In text it is thehexoutput format, a\xprefix followed by hex digits; the olderescapeformat is accepted when decoding but never produced.- The integers are big-endian two's complement of 2, 4 and 8 bytes.
float4andfloat8are big-endian IEEE 754.text,varcharandjsonare the raw encoded bytes in both formats, which for a connection usingclient_encoding = UTF8means UTF-8. minato does not validate the encoding; the server rejects what it will not accept.jsonbin binary is a single version byte, currently 1, followed by the JSON text. In text it is the JSON text alone.uuidis the 16 raw bytes in binary and the 36 character hyphenated form in text.
Erlang representation
Strings, json and bytea are binaries when decoded and accept any iodata()
when encoded.
jsonb is not parsed. It decodes to the JSON document as a binary, with the
version byte removed, and encodes from JSON iodata(). Callers that want terms
run json:decode/1 and json:encode/1 themselves, which keeps the choice of
JSON representation with the caller and keeps the codec free of a JSON parser.
float4 and float8 decode to a float, or to nan, infinity or
neg_infinity, because the BEAM has no float value for those three. The same
three atoms are accepted when encoding. nan encodes to the quiet NaN, which
is what PostgreSQL itself emits and what IEEE 754 recommends; a signalling NaN
can trap on hardware that honours the distinction.
A float4 decoded from text is narrowed to float4 precision, so that the
same value read in either wire format gives the same term. PostgreSQL prints a
float4 with only the digits a float4 needs, and reading those digits as a
double gives a number no float4 holds.
In the text format PostgreSQL prints a float in its shortest round-tripping
form, which for many values carries no decimal point at all: 0, 1e+30,
1e-30. binary_to_float/1 rejects every one of those, so the text decoder
supplies the missing point before parsing.
uuid decoding is governed by the uuid_format option: string, the default,
gives the 36 character hyphenated lowercase binary, and binary gives the 16
raw bytes. Encoding always accepts all three input forms: 16 raw bytes, 32 hex
characters, or the 36 character hyphenated form, in either case.
Summary
Functions
-spec decode(minato_oid:name(), binary(), minato_codec:format(), minato_codec:opts()) -> minato_codec:value().
Decode Format wire bytes to a scalar value.
-spec encode(minato_oid:name(), term(), minato_codec:format(), minato_codec:opts()) -> iodata().
Encode a scalar value to Format wire bytes.