diff --git a/docs/runtime_api.md b/docs/runtime_api.md index b22b41cc9..99ea1db80 100644 --- a/docs/runtime_api.md +++ b/docs/runtime_api.md @@ -985,10 +985,11 @@ Options for `Message.fromJson` and `Message.fromJsonString`: Options for `Message.toJson` and `Message.toJsonString`: -- `emitDefaultValues?: boolean`
- Fields with default values are omitted by default in JSON output. - This option overrides this behavior and outputs fields with - their default values. +- `alwaysEmitImplicit?: boolean`
+ By default, fields with implicit presence are not serialized if they are + unset. For example, an empty list field or a proto3 int32 field with 0 is + not serialized. With this option enabled, such fields are included in the + output. - `enumAsInteger?: boolean`
The name of an enum value is used by default in JSON output. This option overrides the behavior to use the numeric value of the enum value instead. @@ -1020,7 +1021,7 @@ this function directly. Serializing a message with `JSON.stringify()` is equivalent to calling `toJsonString` on the message, with the [serialization option](#json-serialization-options) -`emitDefaultValues: true`. +`alwaysEmitImplicit: true`. ### Unknown fields diff --git a/packages/protobuf-test/src/json.test.ts b/packages/protobuf-test/src/json.test.ts index dd8285a14..5a68b9df0 100644 --- a/packages/protobuf-test/src/json.test.ts +++ b/packages/protobuf-test/src/json.test.ts @@ -704,7 +704,7 @@ describe("extensions in JSON", () => { }); describe("JsonWriteOptions", () => { - describe("emitDefaultValues", () => { + describe("alwaysEmitImplicit", () => { test("emits proto3 implicit fields", async () => { const descMessage = await compileMessage(` syntax="proto3"; @@ -716,7 +716,7 @@ describe("JsonWriteOptions", () => { } `); const json = toJson(descMessage, create(descMessage), { - emitDefaultValues: true, + alwaysEmitImplicit: true, }); expect(json).toStrictEqual({ int32Field: 0, @@ -736,7 +736,7 @@ describe("JsonWriteOptions", () => { } `); const json = toJson(descMessage, create(descMessage), { - emitDefaultValues: true, + alwaysEmitImplicit: true, }); expect(json).toStrictEqual({}); }); @@ -750,7 +750,7 @@ describe("JsonWriteOptions", () => { } `); const json = toJson(descMessage, create(descMessage), { - emitDefaultValues: true, + alwaysEmitImplicit: true, }); expect(json).toStrictEqual({ listField: [], diff --git a/packages/protobuf/src/to-json.ts b/packages/protobuf/src/to-json.ts index b78979460..fe3ff0e27 100644 --- a/packages/protobuf/src/to-json.ts +++ b/packages/protobuf/src/to-json.ts @@ -55,11 +55,12 @@ const IMPLICIT: FeatureSet_FieldPresence.IMPLICIT = 2; */ export interface JsonWriteOptions { /** - * Emit fields with default values: Fields with default values are omitted - * by default in proto3 JSON output. This option overrides this behavior - * and outputs fields with their default values. + * By default, fields with implicit presence are not serialized if they are + * unset. For example, an empty list field or a proto3 int32 field with 0 is + * not serialized. With this option enabled, such fields are included in the + * output. */ - emitDefaultValues: boolean; + alwaysEmitImplicit: boolean; /** * Emit enum values as integers instead of strings: The name of an enum @@ -94,7 +95,7 @@ export interface JsonWriteStringOptions extends JsonWriteOptions { // Default options for serializing to JSON. const jsonWriteDefaults: Readonly = { - emitDefaultValues: false, + alwaysEmitImplicit: false, enumAsInteger: false, useProtoFieldName: false, }; @@ -143,7 +144,7 @@ function reflectToJson(msg: ReflectMessage, opts: JsonWriteOptions): JsonValue { `cannot encode field ${msg.desc.typeName}.${f.name} to JSON: required field not set`, ); } - if (!opts.emitDefaultValues || f.presence !== IMPLICIT) { + if (!opts.alwaysEmitImplicit || f.presence !== IMPLICIT) { // Fields with implicit presence omit zero values (e.g. empty string) by default continue; } @@ -220,7 +221,7 @@ function mapToJson(map: ReflectMap, opts: JsonWriteOptions) { } break; } - return opts.emitDefaultValues || map.size > 0 ? jsonObj : undefined; + return opts.alwaysEmitImplicit || map.size > 0 ? jsonObj : undefined; } function listToJson(list: ReflectList, opts: JsonWriteOptions) { @@ -243,7 +244,7 @@ function listToJson(list: ReflectList, opts: JsonWriteOptions) { } break; } - return opts.emitDefaultValues || jsonArr.length > 0 ? jsonArr : undefined; + return opts.alwaysEmitImplicit || jsonArr.length > 0 ? jsonArr : undefined; } function enumToJson(