簡介

LiteByte是一種輕量級的二進制資料交換格式,
體積小巧、簡單易用是設計目標,主要用于解決前后臺資料傳輸量的問題,
作者:冰封百度(ZhangYu)
設計的靈感來源于C# struct記憶體對齊后的緊湊格式,暫時只實作了C#版本,
特點
1.緊湊的二進制資料格式,支持變長整型,資料量小,
2.用近似代碼定義類的方式定義物件結構,使用方便,
實作思路
把一個物件分為兩個部分:結構和值,
結構用組態檔定義,越方便越好,
值用于網路傳輸,越小越好,
前后臺依賴相同的結構組態檔,轉換時把物件的值拆出來傳輸,決議時把值還原成物件,
使用方法
1.創建自定義結構檔案CustomType.lbs (LiteByte Schema 文本檔案),
2.定義物件欄位(像寫類一樣寫結構、型別和名稱),
3.呼叫LBUtil.Serialize(object) 把物件序列化成二進制資料,
4.呼叫LBUtil.Deserilize(bytes) 把二進制資料反序列化成物件,
代碼樣例:
// 自定義物件結構: // 寫結構配置和C#中寫struct一樣, 訪問修飾符可以不寫,讀組態檔的時候會被忽略 /// <summary> 玩家資訊測驗 | PlayerInfo test </summary> public struct PlayerInfo { public uint id; public string nickname; public byte gender; public bool isVip; public int lv; public int hp; public int mp; public int exp; } // 命名空間 using LiteByte; // 創建物件 PlayerInfo player = new PlayerInfo(); player.id = 100001; player.nickname = "冰封百度"; player.gender = 1; player.isVip = true; player.lv = 999; player.hp = 999999; player.mp = 999999; player.exp = 9999999; // 序列化: string typeName = "PlayerInfo"; byte[] bytes = LBUtil.Serialize(typeName, player); // 長度:31位元組 // 反序列化: PlayerInfo info = LBUtil.Deserialize<PlayerInfo>(typeName, bytes);
轉換結果:

代碼說明:
1.序列化物件時用LBUtil.Serialize("name", obj)
2.反序列化物件時候用LBUtil.Deserilize("name", obj)
3.可以轉換自定義的struct和class 欄位需要是public的
4.提供了通用的轉換物件LBObject,可以Set和Get值,用于動態創建全新的自定義結構,由于序列化和反序列化自定義struct和class時用了反射,效率不高,用LBObject轉換效率會更高一些,但用起來會麻煩一些,按自己喜歡的方式使用就好,
支持的資料型別
資料型別介紹
1.基本的值型別:bool、byte、short、int、long
2.字串 string (支持UTF8、Unicode、ASCII三種編碼方式)
3.陣列 (型別+"[]"會被識別成陣列)
4.自定義型別 (復雜物件)
基本資料型別
位元型(7種)
| 型別 | 長度 | 值范圍 |
| Bit1(Boolean) | 1位 | 0 ~ 1 |
| Bit2(Byte) | 2位 | 0 ~ 3 |
| Bit3(Byte) | 3位 | 0 ~ 7 |
| Bit4(Byte) | 4位 | 0 ~ 15 |
| Bit5(Byte) | 5位 | 0 ~ 31 |
| Bit6(Byte) | 6位 | 0 ~ 63 |
| Bit7(Byte) | 7位 | 0 ~ 127 |
整型(16種)
| 型別 | 長度 | 值范圍 |
| Int8(sbyte) | 1位元組 | -128 ~ 127 |
| Int16(short) | 2位元組 | -32768 ~ -32767 |
| Int24(int) | 3位元組 | -8388608 ~ 8388607 |
| Int32(int) | 4位元組 | -2147483648 ~ 2147483647 |
| Int40(long) | 5位元組 | -549755813888 ~ 549755813887 |
| Int40(long) | 6位元組 | -140737488355328 ~ 140737488355327 |
| Int40(long) | 7位元組 | -36028797018963968 ~ 36028797018963967 |
| Int64(long) | 8位元組 | -9223372036854775808 ~ 9223372036854775807 |
| UInt8(byte) | 1位元組 | 0 ~ 255 |
| UInt16(ushort) | 1位元組 | 0 ~ 65535 |
| UInt24(uint) | 1位元組 | 0 ~ 16777215 |
| UInt32(uint) | 1位元組 | 0 ~ 4294967295 |
| UInt40(ulong) | 1位元組 | 0 ~ 1099511627775 |
| UInt48(ulong) | 1位元組 | 0 ~ 281474976710655 |
| UInt56(ulong) | 1位元組 | 0 ~ 72057594037927935 |
| UInt64(ulong) | 1位元組 | 0 ~ 18446744073709551615 |
浮點型(5種)
| 型別 | 長度 | 有效數字 | 值范圍 |
| Float8(float) | 1位元組 | 7位 | 0/255 ~ 255/255 |
| Float16(float) | 2位元組 | 3位 | ±6.55E +4 |
| Float24(float) | 3位元組 | 5位 | ±1.8447E +19 |
| Float32(float) | 4位元組 | 7位 | ±3.402823E +38 |
| Float64(double) | 8位元組 | 15位 | ±1.7976931348623157E +308 |
變長整型(7種)
| 型別 | 長度 | 值范圍 |
| VarInt16(short) | 1位 + 1~2位元組 | 同Int16 |
| VarInt32(int) | 2位 + 1~4位元組 | 同Int32 |
| VarInt64(long) | 3位 + 1~8位元組 | 同Int64 |
| VarUInt16(ushort) | 1位 + 1~2位元組 | 同UInt16 |
| VarUInt32(uint) | 2位 + 1~4位元組 | 同UInt32 |
| VarUInt64(ulong) | 3位 + 1~8位元組 | 同UInt64 |
| VarLength(int) | 3位 + 1~8位元組 | -1 ~ (Int32.MaxValue/2 - 1) |
字串(3種編碼)
| 型別 | 單個字符長度 | 總長度范圍 |
| UTF8(string) | 1~4位元組 | 頭(1~4)位元組+體(0 ~ 1073741822)位元組 |
| Unicode(string) | 2位元組 | 頭(1~4)位元組+體(0 ~ 1073741822)x2位元組 |
| ASCII(string) | 1位元組 | 頭(1~4)位元組+體(0 ~ 1073741822)位元組 |
復雜資料型別(2種)
| 型別 | 運算式 |
| 陣列(Array) | 型別名稱[] |
| 字典(未實作) | Dictionary<基本型別, 型別名稱> |
| 自定義型別 | 只要不和基本型別和陣列重名 即被當作自定義型別 |
自定義型別結構配置(LiteByte Schema)樣例
以下樣例中 基本型別默認應用以下簡稱配置
Bit1 = bool
Int8 = sbyte
UInt8 = byte
VarInt32 = int
VarUnt32 = uint
VarInt64 = long
VarUInt64 = ulong
UTF8 = string
基本資料型別 結構:
1 struct BaseTypeST { 2 3 // 位元型 4 bool boolValue; 5 6 // 有符號整型 7 sbyte sbyteValue; 8 short shortValue; 9 int intValue; 10 long longValue; 11 12 // 無符號整型 13 byte byteValue; 14 ushort ushortValue; 15 uint uintValue; 16 ulong ulongValue; 17 18 // 有符號浮點型 19 float floatValue; 20 double doubleValue; 21 22 // 字符型(UTF8) 23 string stringValue; 24 25 }
陣列 結構:
1 struct ArrayST { 2 int[] ids; 3 string[] names; 4 }
用戶資訊 結構:
1 struct UserInfoST { 2 3 uint id; 4 string username; 5 string nickname; 6 int hp; 7 int mp; 8 long exp; 9 long gold; 10 byte age; 11 bool isVip; 12 13 }
各語言型別對照表
| 型別 | 長度 | C# | Java | C++ | Go |
| Bit1 | 1位 | bool | boolean | char | bool |
| Bit2 | 2位 | byte | byte | char | uint8 |
| Bit3 | 3位 | byte | byte | char | uint8 |
| Bit4 | 4位 | byte | byte | char | uint8 |
| Bit5 | 5位 | byte | byte | char | uint8 |
| Bit6 | 6位 | byte | byte | char | uint8 |
| Bit7 | 7位 | byte | byte | char | uint8 |
| Int8 | 1位元組 | sbyte | sbyte | char | int8 |
| Int16 | 2位元組 | short | short | short | int16 |
| Int24 | 3位元組 | int | int | int | int32 |
| Int32 | 4位元組 | int | int | int | int32 |
| Int40 | 5位元組 | long | long | long long | int64 |
| Int48 | 6位元組 | long | long | long long | int64 |
| Int56 | 7位元組 | long | long | long long | int64 |
| Int64 | 8位元組 | long | long | long long | int64 |
| UInt8 | 1位元組 | byte | byte | unsigned char | uint8 |
| UInt16 | 2位元組 | ushort | ushort | unsigned short | uint16 |
| UInt24 | 3位元組 | uint | uint | unsigned int | uint32 |
| UInt32 | 4位元組 | uint | uint | unsigned int | uint32 |
| UInt40 | 5位元組 | ulong | ulong | unsigned long long | uint64 |
| UInt48 | 6位元組 | ulong | ulong | unsigned long long | uint64 |
| UInt56 | 7位元組 | ulong | ulong | unsigned long long | uint64 |
| UInt64 | 8位元組 | ulong | ulong | unsigned long long | uint64 |
| Float8 | 1位元組 | float | float | float | float32 |
| Float16 | 2位元組 | float | float | float | float32 |
| Float24 | 3位元組 | float | float | float | float32 |
| Float32 | 4位元組 | float | float | float | float32 |
| Float64 | 8位元組 | double | double | double | float64 |
| VarInt16 | 1位+1~2位元組 | short | short | short | int16 |
| VarInt32 | 2位+1~4位元組 | int | int | int | int32 |
| VarInt64 | 3位+1~8位元組 | long | long | long long | int64 |
| VarUInt16 | 1位+1~2位元組 | ushort | ushort | unsigned short | uint16 |
| VarUInt32 | 2位+1~4位元組 | uint | uint | unsigned int | uint32 |
| VarUInt64 | 3位+1~8位元組 | ulong | ulong | unsigned long long | uint64 |
| VarLength | 2位+1~4位元組 | int | int | int | int32 |
| UTF8 | 1~4位元組 | string | string | string | string |
| Unicode | 2位元組 | string | string | string | string |
| ASCII | 1位元組 | string | string | string | string |
共計38種
資料型別說明:
1.對bool型的支持最好,一個bool型只占1位(1/8個位元組),
2.支持變長整數(short、int、long、ushort、uint、ulong)
3.支持null值 (能空的型別string, array, object,都支持它們為空的情況)
4.建議在定義資料格式時,用盡量小的型別定義欄位,這樣序列化的資料體積會更小,如果懶得寫,可以考慮使用變長資料,
5.支持自定義資料型別名稱,因為相同的資料型別在不同的編程語言中名字不一樣,為了方便使用,我添加了一套內置資料型別名稱并添加了一個型別名稱映射的功能,可以自定義基本值型別的名稱,按照自己喜歡的風格命名就好,
6.因為在撰寫變長資料型別的程序中用到了一些不常見的資料格式,為了重用型別,索性就一起支持了,在對資料大小很嚴格的環境會有幫助,這些非常規的資料型別有:
Bit2~Bit7 占2~7位
Int24、Int40、Int48、Int56 占3、5、6、7位元組
UInt24、UInt40、UInt48、UInt56 占3、5、6、7位元組
VarLength 用于表示string和array的長度 值范圍-1~(int.MaxValue/2 - 1)
其他說明:
由于能力有限,暫時只實作了C#版本(在Unity中實作的,算半個.Net吧)
其他語言后續有時間再寫,雖然造了個輪子 不過感覺造輪子的程序中識訓遠大于付出,挺開心的,
建了個群,有需求的可加,
QQ群:715800513
專案GitHub:https://github.com/zhangyukof/litebyte
測驗Demo:
鏈接:https://pan.baidu.com/s/1yQVn6f4YAkNBDnD0g86xow
提取碼:lio4
轉載請標明原文地址:https://www.cnblogs.com/zhangyukof/p/12073041.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/47515.html
標籤:其他
上一篇:【福利】下載白皮書,贏多重好禮!
