Skip to content

Feat: rust table struct deserialize#8968

Open
RenzoMXD wants to merge 2 commits intogoogle:masterfrom
RenzoMXD:feat/rust-table-struct-deserialize
Open

Feat: rust table struct deserialize#8968
RenzoMXD wants to merge 2 commits intogoogle:masterfrom
RenzoMXD:feat/rust-table-struct-deserialize

Conversation

@RenzoMXD
Copy link
Contributor

Summary

Closes #8841

Adds serde::Deserialize support for the Rust Object API types, enabling full JSON (and any serde format) round-trip serialization/deserialization.

Problem

The Rust FlatBuffers target supported:

  • Reading/writing FlatBuffer binary data (zero-copy)
  • Serializing FlatBuffer reader types to JSON via Serialize (--rust-serialize)
  • Object API with owned *T types via unpack()/pack() (--gen-object-api)

But it was missing the reverse path: deserializing JSON (or any text/binary serde format) back into Rust types. The core challenge identified in #8841 was that serde's Deserialize trait signature doesn't accept a FlatBufferBuilder, making it impossible to deserialize directly into the zero-copy reader types.

Solution

Instead of targeting the zero-copy reader types (Monster<'a>), this PR adds Deserialize to the Object API types (MonsterT, Vec3T, etc.) which own their data (String, Vec, Box) and can be freely constructed without a builder. This gives users a complete round-trip path:

@RenzoMXD RenzoMXD requested a review from dbaileychess as a code owner March 10, 2026 20:40
@github-actions github-actions bot added c++ rust codegen Involving generating code from schema labels Mar 10, 2026
@jtdavis777
Copy link
Collaborator

hey @RenzoMXD thank you for this PR! I really want to get this in but given I have so little experience in rust it may take a bit for me to digest and feel confident in this being a solution. I hope you will have patience with me :D

@fawdlstty would you be able to test this against your original issues?

@fawdlstty
Copy link
Contributor

Thank you very much for your help. The generated object API is something I didn't notice, but it can indeed solve this problem.

@fawdlstty
Copy link
Contributor

@jtdavis777 If possible, I suggest not waiting for my local testing as I may only get involved in this part after a while and not too quickly

@RenzoMXD
Copy link
Contributor Author

Run the deserialize-specific test:

cd tests/rust_serialize_test && cargo run

Expected Output:

OK: Object API JSON round-trip passed
OK: Object API full pipeline (FlatBuffer -> JSON -> FlatBuffer) passed
OK: Object API deserialize from JSON string passed
OK: Bitflags enum deserialization passed
OK: Union type deserialization passed

The new tests live in tests/rust_serialize_test/src/main.rs (lines 97-311). They cover:

  • test_object_api_json_roundtrip — creates a MonsterT with nested structs, vectors, and enums, serializes to JSON, deserializes back, asserts equality
  • test_object_api_full_pipelineMonsterT → pack() → FlatBuffer → unpack() → MonsterT → JSON → MonsterT → pack() → FlatBuffer, then verifies the final FlatBuffer reads correctly
  • test_object_api_deserialize_from_json_string — parses a hand-written JSON string into MonsterT, proving external JSON input works
  • test_bitflags_enum_deserializeColor::Green → JSON → Color::Green (bitflags had no Deserialize before)
  • test_union_deserializeAnyT::NONE and AnyT::Monster(Box<MonsterT>) round-trip through JSON

I think this can help your testing @jtdavis777

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[rust] table/struct deserialization

3 participants