Skip to content

Add serde feature gated support for (de)serialization of packed protos.#628

Open
spwilson27 wants to merge 4 commits into
capnproto:masterfrom
spwilson27:master
Open

Add serde feature gated support for (de)serialization of packed protos.#628
spwilson27 wants to merge 4 commits into
capnproto:masterfrom
spwilson27:master

Conversation

@spwilson27

Copy link
Copy Markdown

Add support for serde to enable deserialization inter-op with JSON.

Use Case

I'd like to define a typed config specification using capnp definitions, both to support strong typing and because the config will be sent across IPC boundaries. I'd like to write the actual config file in JSON since it's easily human readable.

Testing

Verified all existing tests pass with cargo nextest run on M4 MAX Mac. Verified example builds and prints expected output:

cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.26s
     Running `<redacted>/json_config`
{
  "appName": "hive-worker",
  "port": 7000,
  "verbose": true
}

@spwilson27

Copy link
Copy Markdown
Author

Note, code was mostly AI generated. I've manually reviewed it all for anything egregious.

Potential Optimizations:

  • We could switch to cfg gating for compilation of functions if that's preferred over runtime checks
  • It might be reasonable to split into two separate features, one for serialize, one for deserialize

I'm willing to make changes as requested, sharing this patchset as I've wanted this ability across multiple projects.

@stepancheg

stepancheg commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

I think this PR needs some tests. (I am not a maintainer of this project.)

spwilson27 and others added 2 commits April 4, 2026 21:38
Previously, the serde Serialize/Deserialize code generation skipped
structs with union (discriminant) fields. This extends it to handle
them:

- Serialize: non-union fields serialized normally, active union variant
  serialized via match on self.which()
- Deserialize: union field keys handled same as non-union (init sets
  the discriminant)
- Recursive types: cycle detection now returns true instead of false,
  since serde handles recursion at runtime
- Fix list deserialization borrow: use reborrow() before init and
  mut self on visit_seq
- Guard deserialize_serde method on supports_serde_struct check

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants