aspnetcore/grpc/json-transcoding-binding.md
gRPC JSON transcoding creates RESTful JSON web APIs from gRPC methods. It uses annotations and options for customizing how a RESTful API maps to the gRPC methods.
gRPC methods must be annotated with an HTTP rule before they support transcoding. The HTTP rule includes information about calling the gRPC method as a RESTful API, such as the HTTP method and route.
An HTTP rule is:
google.api.http.google/api/annotations.proto file. The google/api/http.proto and google/api/annotations.proto files need to be in the project.The HTTP method is specified by setting the route to the matching HTTP method field name:
getputpostdeletepatchThe custom field allows for other HTTP methods.
In the following example, the CreateAddress method is mapped to POST with the specified route:
gRPC JSON transcoding routes support route parameters. For example, {name} in a route binds to the name field on the request message.
To bind a field on a nested message, specify the path to the field. In the following example, {params.org} binds to the org field on the IssueParams message:
Transcoding routes and ASP.NET Core routes have a similar syntax and feature set. However, some ASP.NET Core routing features aren't supported by transcoding. These include:
Transcoding deserializes the request body JSON to the request message. The body field specifies how the HTTP request body maps to the request message. The value is either the name of the request field whose value is mapped to the HTTP request body or * for mapping all request fields.
In the following example, the HTTP request body is deserialized to the address field:
Any fields in the request message that aren't bound by route parameters or the request body can be set using HTTP query parameters.
In the preceding example:
org and repo fields are bound from route parameters.text and the nested fields from page, can be bound from the query string: ?text=value&page.index=0&page.size=10By default, transcoding serializes the entire response message as JSON. The response_body field allows serialization of a subset of the response message.
In the preceding example, the address field is serialized to the response body as JSON.
For more information about customizing gRPC transcoding, see the HttpRule specification.
Messages are converted to and from JSON using the JSON mapping in the Protobuf specification. Protobuf's JSON mapping is a standardized way to convert between JSON and Protobuf, and all serialization follows these rules.
However, gRPC JSON transcoding offers some limited options for customizing JSON with xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings, as shown in the following table.
| Option | Default Value | Description |
|---|---|---|
| xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.IgnoreDefaultValues | false | If set to true, fields with default values are ignored during serialization. |
| xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.WriteEnumsAsIntegers | false | If set to true, enum values are written as integers instead of strings. |
| xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.WriteInt64sAsStrings | false | If set to true, Int64 and UInt64 values are written as strings instead of numbers. |
| xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.WriteIndented | false | If set to true, JSON is written using pretty printing. This option doesn't affect streaming methods, which write line-delimited JSON messages and can't use pretty printing. |
builder.Services.AddGrpc().AddJsonTranscoding(o =>
{
o.JsonSettings.WriteIndented = true;
});
In the .proto file, the json_name field option customizes a field's name when it's serialized as JSON, as in the following example:
message TestMessage {
string my_field = 1 [json_name="customFieldName"];
}
Transcoding doesn't support advanced JSON customization. Apps requiring precise JSON structure control should consider using ASP.NET Core Web API.