-
Notifications
You must be signed in to change notification settings - Fork 8
/
toproto3json.ts
191 lines (177 loc) · 5.77 KB
/
toproto3json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as protobuf from 'protobufjs';
import {JSONObject, JSONValue, LongStub} from './types';
import {Any, googleProtobufAnyToProto3JSON} from './any';
import {bytesToProto3JSON} from './bytes';
import {getFullyQualifiedTypeName, wrapperTypes} from './util';
import {resolveEnumValueToNumber, resolveEnumValueToString} from './enum';
import {
googleProtobufListValueToProto3JSON,
googleProtobufStructToProto3JSON,
googleProtobufValueToProto3JSON,
ListValue,
Struct,
Value,
} from './value';
import {Duration, googleProtobufDurationToProto3JSON} from './duration';
import {googleProtobufTimestampToProto3JSON, Timestamp} from './timestamp';
import {
BoolValue,
BytesValue,
NumberValue,
StringValue,
wrapperToProto3JSON,
} from './wrappers';
import {FieldMask, googleProtobufFieldMaskToProto3JSON} from './fieldmask';
export interface ToProto3JSONOptions {
numericEnums: boolean;
}
// Convert a single value, which might happen to be an instance of Long, to JSONValue
function convertSingleValue(value: JSONValue | object): JSONValue {
if (typeof value === 'object') {
if (value?.constructor?.name === 'Long') {
return (value as LongStub).toString();
}
throw new Error(`toProto3JSON: don't know how to convert value ${value}`);
}
return value;
}
export function toProto3JSON(
obj: protobuf.Message,
options?: ToProto3JSONOptions
): JSONValue {
const objType = obj.$type;
if (!objType) {
throw new Error(
'Cannot serialize object to proto3 JSON since its .$type is unknown. Use Type.fromObject(obj) before calling toProto3JSON.'
);
}
objType.resolveAll();
const typeName = getFullyQualifiedTypeName(objType);
// Types that require special handling according to
// https://developers.google.com/protocol-buffers/docs/proto3#json
if (typeName === '.google.protobuf.Any') {
return googleProtobufAnyToProto3JSON(
obj as protobuf.Message & Any,
options
);
}
if (typeName === '.google.protobuf.Value') {
return googleProtobufValueToProto3JSON(obj as protobuf.Message & Value);
}
if (typeName === '.google.protobuf.Struct') {
return googleProtobufStructToProto3JSON(obj as protobuf.Message & Struct);
}
if (typeName === '.google.protobuf.ListValue') {
return googleProtobufListValueToProto3JSON(
obj as protobuf.Message & ListValue
);
}
if (typeName === '.google.protobuf.Duration') {
return googleProtobufDurationToProto3JSON(
obj as protobuf.Message & Duration
);
}
if (typeName === '.google.protobuf.Timestamp') {
return googleProtobufTimestampToProto3JSON(
obj as protobuf.Message & Timestamp
);
}
if (typeName === '.google.protobuf.FieldMask') {
return googleProtobufFieldMaskToProto3JSON(
obj as protobuf.Message & FieldMask
);
}
if (wrapperTypes.has(typeName)) {
return wrapperToProto3JSON(
obj as protobuf.Message &
(NumberValue | StringValue | BoolValue | BytesValue)
);
}
const result: JSONObject = {};
for (const [key, value] of Object.entries(obj)) {
const field = objType.fields[key];
const fieldResolvedType = field.resolvedType;
const fieldFullyQualifiedTypeName = fieldResolvedType
? getFullyQualifiedTypeName(fieldResolvedType)
: null;
if (value === null) {
result[key] = null;
continue;
}
if (Array.isArray(value)) {
if (value.length === 0) {
// ignore repeated fields with no values
continue;
}
// if the repeated value has a complex type, convert it to proto3 JSON, otherwise use as is
result[key] = value.map(
fieldResolvedType
? element => {
return toProto3JSON(element, options);
}
: convertSingleValue
);
continue;
}
if (field.map) {
const map: JSONObject = {};
for (const [mapKey, mapValue] of Object.entries(value)) {
// if the map value has a complex type, convert it to proto3 JSON, otherwise use as is
map[mapKey] = fieldResolvedType
? toProto3JSON(mapValue as protobuf.Message, options)
: convertSingleValue(mapValue as JSONValue);
}
result[key] = map;
continue;
}
if (fieldFullyQualifiedTypeName === '.google.protobuf.NullValue') {
result[key] = null;
continue;
}
if (fieldResolvedType && 'values' in fieldResolvedType && value !== null) {
if (options?.numericEnums) {
result[key] = resolveEnumValueToNumber(fieldResolvedType, value);
} else {
result[key] = resolveEnumValueToString(fieldResolvedType, value);
}
continue;
}
if (fieldResolvedType) {
result[key] = toProto3JSON(value, options);
continue;
}
if (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean' ||
value === null
) {
if (typeof value === 'number' && !Number.isFinite(value)) {
result[key] = value.toString();
continue;
}
result[key] = value;
continue;
}
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
result[key] = bytesToProto3JSON(value);
continue;
}
result[key] = convertSingleValue(value);
continue;
}
return result;
}