https://google.github.io/flatbuffers/flatbuffers_guide_writing_schema.html
// example IDL filenamespace MyGame;attribute "priority";enum Color : byte { Red = 1, Green, Blue }union Any { Monster, Weapon, Pickup }struct Vec3 {x:float;y:float;z:float;}table Monster {pos:Vec3;mana:short = 150;hp:short = 100;name:string;friendly:bool = false (deprecated, priority: 1);inventory:[ubyte];color:Color = Blue;test:Any;}root_type Monster;
Table
name和一些fields。每个field包含name、type,默认值可选。不写默认值,值类型赋值为0,其他类型赋值为null。
可是使用deprecated。
在最后添加新的字段。
Struct
所有field必须有默认值。
只能包含值类型和其他struct。
不能添加字段、使用deprecated。
比table占内存更小,速度更快。
值类型
- 8 bit: byte (int8), ubyte (uint8), bool
- 16 bit: short (int16), ushort (uint16)
- 32 bit: int (int32), uint (uint32), float (float32)
64 bit: long (int64), ulong (uint64), double (float64)
引用类型
vector:语法是[ ]
- string:
Arrays
只在struct中支持。语法是[float : 3 ]struct Vec3 {x:float;y:float;z:float;}//可以更改为struct Vec3 {v:[float:3];}
Enums
只有整型被支持:byte, ubyte, short ushort, int, uint, long and ulong.Unions
和enum形式上一样,但是字段的名字使用的是table名。相当于引用了一下table。table PointPosition { x:uint; y:uint; }table MarkerPosition {}union Position {Start:MarkerPosition,Point:PointPosition,Finish:MarkerPosition}
Namespaces
生成C++的命名空间或Java的Package。Includes
包含其他schema文件。可以保证每个文件只被解析一次。Root Type
表示是root的table,对于json来说很重要。File identification和extension
file_identifier "MYFI"; //默认生成标识?file_extension "ext"; //默认生成.bin文件,这个可以修改
注释和文档
///Attributes
关于性能的建议
- 尽量使用enum代替string
- 选择最小的整型
- 没有继承,可以使用union来表示数据结构关系
- 共享数据,比如使用相同的string、table
