-
Notifications
You must be signed in to change notification settings - Fork 76
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
V2: JSON Types #866
V2: JSON Types #866
Conversation
Signed-off-by: Timo Stamm <[email protected]>
Generate the well-known types with JSON typings. Signed-off-by: Timo Stamm <[email protected]>
Re-generate all, adding JSON typings. Signed-off-by: Timo Stamm <[email protected]>
Signed-off-by: Timo Stamm <[email protected]>
/** | ||
* JSON type for the message docs.User. | ||
*/ | ||
export type UserJson = { | ||
/** | ||
* @generated from field: string first_name = 1; | ||
*/ | ||
firstName?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simple example for a JSON type for a message.
Will there be something similar for proto3 as well?, or is the v2 meaning for the future version of this package? |
@mikeki, V2 refers to version 2 of this project. You can try it out here: https://github.com/bufbuild/protobuf-es/releases/tag/v2.0.0-alpha.1 |
Got it! Yes these interfaces would be great. One thing I noticed is that the interface between |
c025d07
to
766326a
Compare
The JsonType parameter must remain optional, because JSON types are optional.
This PR adds an advanced feature with the plugin option
json_types=true
. If enabled,protoc-gen-es
generates a JSON type for every Protobuf message.Given this definition:
The following additional export is generated:
The JSON type matches exactly what
toJson()
will emit with standard serialization options, andtoJson()
will actually automatically return the JSON type if available:For enumerations, a similar mechanism applies. We generate a union type with all JSON string values for the enum:
With the functions
enumToJson()
andenumFromJson()
, values can be converted between both representations. WithisEnumJson()
, unknown input can be narrowed down to known values. If JSON types are available, the functions are type safe: