有没有这样的数据编解码方案:

  1. 不需要额外的schema文件,有数据就能正确解析。
  2. 编码后的数据小,而且编解码性能足够好。
  3. 规则简单,容易实现优秀的编解码库。

cdo编码

image-20240514102141170

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// cdo (Compact data of object)
// All type encoded format
const (
	// Type: 0, Format: 000|XXXXXX
	// some fixed type
	TypeFixed byte = 0b00000000 // 0
	// Type0 subtypes
	FixNil         byte = 0b00000000 // 0
	FixTrue        byte = 0b00000001 // 1
	FixFalse       byte = 0b00000010 // 2
	FixFloat32     byte = 0b00000011 // 3
	FixFloat64     byte = 0b00000100 // 4
	FixDateTime    byte = 0b00000101 // 5
	FixDate        byte = 0b00000110 // 6
	FixDuration    byte = 0b00000111 // 7
	FixArrSameType byte = 0b00001000 // 8
	FixMax         byte = 0b00011111 // 31

	// Type: 1, Format: 001|XXXXXX
	// all int numbers  which >= 0
	TypePosInt byte = 0b00100000

	// Type: 2, Format: 010|XXXXXX
	// all int numbers  which < 0
	TypeNegInt byte = 0b01000000

	// Type: 3, Format: 011|XXXXXX
	// all bytes array such as string/bytes
	TypeBytes byte = 0b01100000

	// Type: 4, Format: 100|XXXXXX
	// array data
	TypeArray byte = 0b10000000

	// Type: 5, Format: 101|XXXXXX
	// just kvs
	TypeMap byte = 0b10100000

	// Type: 6, Format: 110|XXXXXX
	//
	TypeObject byte = 0b11000000

	// Type: 7, Format: 111|XXXXXX
	//
	TypeObjIndex byte = 0b11100000

	// ++++++++++++++++++++++++++++++++++++++
	TypeSizeOffset2 uint8 = 2
	TypeSizeOffset4 uint8 = 4
	TypeSizeOffset8 uint8 = 8
)