Polymorph Data Encoding

Jakob Jenkov
Last update: 2022-04-28

The Polymorph Data format uses a compact, fast to read and write, versatile binary encoding. This text specifies how that encoding looks. As mentioned, Polymorph Data uses a binary encoding - also sometimes referred to as Polymorph Binary - or Pinary for short.

Polymorph also contains a textual data format which can be converted to Polymorph binary easily. That way you can more easily create Pinary files. This textual-to-binary data format is also referred to as Textual Binary or Tinary.

Polymorph Data Encoding

The Polymorph Data Encoding uses a simple Type-Length-Value format. All data is encoded into fields. A field has a type. The very first byte of a field tells the type. Depending on the field type (the first byte) the rest of the field's bytes are interpreted differently. Polymorph Data Encoding has the following archetype field encodings:

  1. Type => Value
  2. Type => Length Bytes => Value
  3. Type => Subtype => Length Bytes => Value

Type => Value

Fields that follow the [Type => Value] encoding will have:

  • 1 Type byte
  • N Value bytes

The number of value bytes depends on the type of the field. For instance, the type INT_POS_1_BYTES means a "positive integer of 1 bytes in length". Thus, the length of the field value is implicitly derived from the field type.

Type => Length => Value

Fields that follow the [Type => Length => Value] encoding will have:

  • 1 Type byte
  • 0 to 16 Length bytes
  • N Value bytes

The number of length bytes depends on the type of the field. For instance, the type BYTES_2_LENGTH_BYTES means a "byte field with 2 length bytes - meaning a possible field value length of 0 to 65535 bytes".

Type => Subtype => Length => Value

Fields that follow the [Type => Subtype => Length => Value] encoding will have:

  • 1 Type byte
  • 1 Subtype byte
  • 0 to 16 Length bytes
  • N Value bytes

Polymorph Data Field Types

Polymorph contains the following field types:

Field TypeType CodeDescription
BOOLEAN_NULL0A single byte with the value 0 - representing a boolean field with a null value.
BOOLEAN_TRUE1A single byte with the value 1 - representing a boolean field with the value True.
BOOLEAN_TRUE2A single byte with the value 2 - representing a boolean field with the value False.
INT_NULL3A single byte with the value 3 - representing an null value of type integer.
INT_POS_1_BYTES41 type byte + 1 value byte - containing a positive integer value of 1 byte (0 to 255)
INT_POS_2_BYTES51 type byte + 2 value bytes - containing a positive integer value of 2 bytes (0 to 65535)
INT_POS_3_BYTES61 type byte + 3 value bytes - containing a positive integer value of 3 bytes (0 to 2^24-1)
INT_POS_4_BYTES71 type byte + 4 value bytes - containing a positive integer value of 4 bytes (0 to 2^32-1)
INT_POS_5_BYTES81 type byte + 5 value bytes - containing a positive integer value of 5 bytes (0 to 2^40-1)
INT_POS_6_BYTES91 type byte + 6 value bytes - containing a positive integer value of 6 bytes (0 to 2^48-1)
INT_POS_7_BYTES101 type byte + 7 value bytes - containing a positive integer value of 7 bytes (0 to 2^56-1)
INT_POS_8_BYTES111 type byte + 8 value bytes - containing a positive integer value of 8 bytes (0 to 2^64-1)
INT_NEG_1_BYTES121 type byte + 1 value byte - containing a positive integer representing a negative integer value of 1 byte (0 to 255 meaning -1 to -256)
INT_NEG_2_BYTES131 type byte + 2 value bytes - containing a positive integer representing a negative integer value of 2 bytes (0 to 2^16-1 meaning -1 to -2^16)
INT_NEG_3_BYTES141 type byte + 3 value bytes - containing a positive integer representing a negative integer value of 3 bytes (0 to 2^24-1 meaning -1 to -2^24)
INT_NEG_4_BYTES151 type byte + 4 value bytes - containing a positive integer representing a negative integer value of 4 bytes (0 to 2^32-1 meaning -1 to -2^32)
INT_NEG_5_BYTES161 type byte + 5 value bytes - containing a positive integer representing a negative integer value of 5 bytes (0 to 2^40-1 meaning -1 to -2^40)
INT_NEG_6_BYTES171 type byte + 6 value bytes - containing a positive integer representing a negative integer value of 6 bytes (0 to 2^48-1 meaning -1 to -2^48)
INT_NEG_7_BYTES181 type byte + 7 value bytes - containing a positive integer representing a negative integer value of 7 bytes (0 to 2^56-1 meaning -1 to -2^56)
INT_NEG_8_BYTES191 type byte + 8 value bytes - containing a positive integer representing a negative integer value of 8 bytes (0 to 2^64-1 meaning -1 to -2^64)

Jakob Jenkov

Featured Videos



Core Software Performance Optimization Principles

Thread Congestion in Java - Video Tutorial













Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next