Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serde support #266

Closed
2 of 3 tasks
hicqu opened this issue Feb 7, 2018 · 7 comments
Closed
2 of 3 tasks

serde support #266

hicqu opened this issue Feb 7, 2018 · 7 comments

Comments

@hicqu
Copy link
Contributor

hicqu commented Feb 7, 2018

I think serde_json is a useful feature. If it's opened, the generated code should be like:

#[derive(Serialize, Deserialize)]
struct S {
    a: i64,
}

So that we can parse {"a": 100} to the struct directly.

Current status:

  • Basic implementation
  • Repeated message fields
  • Bytes
@stepancheg
Copy link
Owner

I think two thoughts:

  • I'm opposed to adding #[derive] unconditionally: currently rust-protobuf has no mandatory dependencies, and I'd like to keep it that way. However, I think it would probably be useful to have codegen option to add those derives (FYI, currently rust-protobuf has an option to use bytes crate).
  • I think rust-protobuf should suport JSON encoding and decoding natively. That encoding should follow official specification

@bachp
Copy link

bachp commented Apr 19, 2018

I think deriving Serialize would be useful to use the generated types in Template generators like or Tera. They can automatically render types that implement Serialize.

Hiding this behind a feature would be fine tough.

@repi
Copy link

repi commented May 25, 2018

This would indeed be nice as an optional feature!

@stepancheg
Copy link
Owner

Actually now I think that probably it's possible to have it always on.

E. g. generated files could be like this:

#[cfg_attr(protobuf_serde, derive(Serialize, Deserialize))]
struct GeneratedMessage {
   ...
}

So users who need serde could simply enable flag, and codegen flag won't be needed.

However! I believe JSON serialization and parsing should be done without serde (because it can be done better/more protobuf standard conforming than serde).

I've almost finished reflection in protobuf (now protobuf reflection can mutate messages, not just read), and I plan to implement JSON parsing/serialization in protobuf soon.

@stepancheg stepancheg changed the title How about add a feature serde_json? serde support Jun 3, 2018
stepancheg added a commit that referenced this issue Jun 16, 2018
@stepancheg
Copy link
Owner

stepancheg commented Sep 22, 2018

Three additions.

  • protobuf now natively supports JSON serialization, serde is probably not that important
  • other formats can be implemented with protobuf runtime reflection
  • I've created an issue for code generation callbacks, so serde annotations could be inserted in these callbacks

@stepancheg
Copy link
Owner

The current state of serde support:

  • protobuf crate has with-serde feature
  • no serde annotations are generated by default
  • annotations are generated if serde_derive codegen option is specified
  • annotations are guarded with #[cfg_attr(smth, ...)] if serde_derive_cfg=smth option is specified (this part is done mostly for implementation of with-serde feature, not sure it is needed for users)

This API design is not final. Feedback is welcome!

@stepancheg
Copy link
Owner

Published version 2.1.0.

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

No branches or pull requests

4 participants