google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
22.56k stars 3.19k forks source link

[Python] Use 'T' suffix object for serialization #8230

Open mhs4670go opened 5 months ago

mhs4670go commented 5 months ago

Hello. I'm a newbie on flatbuffers for python language.

I've got two questions about serialization with T-suffix object.

Seems that, when it comes to the serialization, StartXXX and EndXXX api are used like below.

builder = flatbuffers.Builder(1024)

# Create the first `Weapon` ('Sword').
MyGame.Sample.Weapon.Start(builder)
MyGame.Sample.Weapon.AddName(builder, weapon_one)
MyGame.Sample.Weapon.AddDamage(builder, 3)
sword = MyGame.Sample.Weapon.End(builder)

But, following method can do the same thing.

  1. Create Object
  2. Fill some contents
  3. Finish the object with packing the Object
builder = flatbuffers.Builder(1024)

sword = MyGame.Sample.Weapon.WeaponT() # Create T-suffix object
sword.name = weapon_one
sword.damage = 3

builder.Finish(sowrd.Pack(builder))
buf = builder.Output()

Q1) Is it safe to do a serialization with above second way? Q2) When I fill in the contents into the created T-suffix object, it is allowed to set an attribute that is not included in the schema like below.

sword = MyGame.Sample.Weapon.WeaponT()
sword.NonExistsAttribute = True # it causes no error

And, the serialization is done successfully. But, I think it is error-prone. Is there any verification api for this kind of check?