You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CBOR is a binary data serialization format similar to JSON but more compact, efficient, and capable of encoding a
3
+
broader range of data types. It is useful for exchanging structured data between systems, especially when performance
4
+
and size are critical.
5
+
6
+
## Purpose of the CBOR Implementation
7
+
8
+
The CBOR code here allows the custom SurrealDB types (e.g., `GeometryPoint`, `Table`, `Range`, etc.) to be serialized
9
+
into CBOR binary format and deserialized back into Python objects. This is necessary because these types are not natively
10
+
supported by CBOR; thus, custom encoding and decoding logic is implemented.
11
+
12
+
## Key Components
13
+
14
+
### Custom Types
15
+
16
+
`Range` Class: Represents a range with a beginning (`begin`) and end (`end`). These can either be included (`BoundIncluded`) or excluded (`BoundExcluded`).
17
+
`Table`, `RecordID`, `GeometryPoint`, etc.: Custom SurrealDB-specific data types, representing domain-specific constructs like tables, records, and geometrical objects.
18
+
19
+
### CBOR Encoder
20
+
21
+
The function `default_encoder` is used to encode custom Python objects into CBOR's binary format. This is done by associating a specific CBOR tag (a numeric identifier) with each data type.
22
+
23
+
For example:
24
+
25
+
`GeometryPoint` objects are encoded using the tag `TAG_GEOMETRY_POINT` with its coordinates as the value.
26
+
`Range` objects are encoded using the tag `TAG_BOUND_EXCLUDED` with a list [begin, end] as its value.
27
+
The `CBORTag` class is used to represent tagged data in `CBOR`.
28
+
29
+
### CBOR Decoder
30
+
31
+
The function `tag_decoder` is the inverse of `default_encoder`. It takes tagged CBOR data and reconstructs the corresponding Python objects.
32
+
33
+
For example:
34
+
35
+
When encountering the `TAG_GEOMETRY_POINT` tag, it creates a `GeometryPoint` object using the tag's value (coordinates).
36
+
When encountering the `TAG_RANGE` tag, it creates a `Range` object using the tag's value (begin and end).
37
+
38
+
### encode and decode Functions
39
+
40
+
These are high-level functions for serializing and deserializing data:
41
+
42
+
`encode(obj)`: Converts a Python object into CBOR binary format.
43
+
`decode(data)`: Converts CBOR binary data back into a Python object using the custom decoding logic.
0 commit comments