Function jsonb::from_slice

source ·
pub fn from_slice(buf: &[u8]) -> Result<Value<'_>, Error>
Expand description

The binary JSONB contains three parts, Header, JEntry and RawData. This structure can be nested. Each group of structures starts with a Header. The upper-level Value will store the Header length or offset of the lower-level Value. Header stores the type of the Value, include Array, Object and Scalar, Scalar has only one Value, and a corresponding JEntry. Array and Object are nested type, they have multiple lower-level Values. So the Header also stores the number of lower-level Values. JEntry stores the types of Scalar Value, including Null, True, False, Number, String and Container. They have three different decode methods.

  1. Null, True and False can be obtained by JEntry, no extra work required.
  2. Number and String has related RawData, JEntry store the length or offset of this data, the Value can be read out and then decoded.
  3. Container is actually a nested Array or Object with the same structure, JEntry store the length or offset of the lower-level Header, from where the same decode process can begin. RawData is the encoded Value. Number is a variable-length Decimal, store both int and float value. String is the original string, can be borrowed directly without extra decode. Array and Object is a lower-level encoded JSONB value. The upper-level doesn’t care about the specific content. Decode can be executed recursively. Decode JSONB Value from binary bytes.