Skip to content

An option is a setting attached to a schema element: a file, a message, a field, an enum, or a service. The options on this page change what the compiler generates and how data maps to other formats, and they never change the wire format. (Editions features use the same syntax and are an exception. Field presence, for example, changes what gets written.) A file with go_package set and the same file without it encode the same bytes.

The most common ones:

  • go_package and java_package: where the generated Go and Java code goes.
  • optimize_for: trades generated-code size for speed in C++ and Java. SPEED, CODE_SIZE, or LITE_RUNTIME.
  • [deprecated = true]: marks a field so generated code warns when it is used.
  • [json_name = "uid"]: changes the key the JSON mapping uses, as the example shows.
  • [(pb.enumvalue.json).string = "kg"]: changes the name the JSON mapping uses for an enum value. It is new in edition 2026 and has its own example below.

The full set lives in descriptor.proto, and the Custom Options page shows how to define your own.

edition = "2024";

option go_package = "github.com/example/v1";
option java_package = "com.example.v1";
option optimize_for = SPEED;

message User {
  string user_id = 1 [json_name = "uid"];
  string old_field = 2 [deprecated = true];
}
JSON
{
  "uid": "01H8XGJWBWBAQ4",
  "oldField": "still serialized"
}

JSON names for enum values

json_name renames a field's key. Until edition 2026 nothing did the same for an enum value, which always went into JSON under the name in the schema, prefix and all. The (pb.enumvalue.json).string option sets the string to use instead.

The option lives in google/protobuf/json_options.proto. import option is enough to bring it in, because the file is only needed for the option it defines. A runtime that supports the option writes the custom name, and when it parses it takes the custom name, the original name, or the number. The compiler rejects a custom name that another value of the enum already answers to.

The option only works in a file on edition 2026 or later. It declares the edition that introduced it, and the compiler rejects it anywhere earlier. Renaming a value that is already in use breaks JSON clients, the same as changing a json_name does. A client built before the change doesn't recognize the new string.

parcel.proto
edition = "2026";

import option "google/protobuf/json_options.proto";

enum Unit {
  UNIT_UNSPECIFIED = 0;
  UNIT_KG = 1 [(pb.enumvalue.json).string = "kg"];
  UNIT_LB = 2 [(pb.enumvalue.json).string = "lb"];
}

message Parcel {
  string id = 1;
  double weight = 2;
  Unit unit = 3;
}
JSON
{
  "id": "pcl_123",
  "weight": 2.5,
  "unit": "kg"
}

Next

Unknown Fields

How Protobuf preserves fields a decoder doesn't recognize, why that keeps old clients compatible with new schemas, and where unknown fields can bite.