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

V2: Rename JSON serialization option emitDefaultValues #863

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/runtime_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,11 @@ Options for `Message.fromJson` and `Message.fromJsonString`:

Options for `Message.toJson` and `Message.toJsonString`:

- `emitDefaultValues?: boolean`<br/>
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`<br/>
By default, fields with implicit presence are not serialized if they are
unset. For example, an empty list field or a proto3 in32 field with 0 is
timostamm marked this conversation as resolved.
Show resolved Hide resolved
not serialized. With this option enabled, such fields are included in the
output.
- `enumAsInteger?: boolean`<br/>
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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/protobuf-test/src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -716,7 +716,7 @@ describe("JsonWriteOptions", () => {
}
`);
const json = toJson(descMessage, create(descMessage), {
emitDefaultValues: true,
alwaysEmitImplicit: true,
});
expect(json).toStrictEqual({
int32Field: 0,
Expand All @@ -736,7 +736,7 @@ describe("JsonWriteOptions", () => {
}
`);
const json = toJson(descMessage, create(descMessage), {
emitDefaultValues: true,
alwaysEmitImplicit: true,
});
expect(json).toStrictEqual({});
});
Expand All @@ -750,7 +750,7 @@ describe("JsonWriteOptions", () => {
}
`);
const json = toJson(descMessage, create(descMessage), {
emitDefaultValues: true,
alwaysEmitImplicit: true,
});
expect(json).toStrictEqual({
listField: [],
Expand Down
17 changes: 9 additions & 8 deletions packages/protobuf/src/to-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 in32 field with 0 is
timostamm marked this conversation as resolved.
Show resolved Hide resolved
* 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
Expand Down Expand Up @@ -94,7 +95,7 @@ export interface JsonWriteStringOptions extends JsonWriteOptions {

// Default options for serializing to JSON.
const jsonWriteDefaults: Readonly<JsonWriteOptions> = {
emitDefaultValues: false,
alwaysEmitImplicit: false,
enumAsInteger: false,
useProtoFieldName: false,
};
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand Down