@@ -80,6 +80,9 @@ import {
80
80
describe ( 'Serializer' , ( ) => {
81
81
const partition = new DatabaseId ( 'p' , 'd' ) ;
82
82
const s = new JsonProtoSerializer ( partition , { useProto3Json : false } ) ;
83
+ const proto3JsonSerializer = new JsonProtoSerializer ( partition , {
84
+ useProto3Json : true
85
+ } ) ;
83
86
const emptyResumeToken = new Uint8Array ( 0 ) ;
84
87
const protos = loadRawProtos ( ) ;
85
88
@@ -124,36 +127,47 @@ describe('Serializer', () => {
124
127
/** The expected JSON value for the field (e.g. 'NULL_VALUE') */
125
128
jsonValue : unknown ;
126
129
/**
127
- * The expected protobufJs value for the field (e.g. `0`).
128
- * If omitted, it's expected to be the same as jsonValue.
130
+ * The expected protobufJs value for the field (e.g. `0`). This is
131
+ * largely inconsequential (we only rely on the JSON representation), but
132
+ * it can be useful for debugging issues. If omitted, it's assumed to be
133
+ * the same as jsonValue.
129
134
*/
130
135
protobufJsValue ?: unknown ;
136
+ /**
137
+ * If true, uses the proto3Json serializer (and skips the round-trip
138
+ * through protobufJs).
139
+ */
140
+ useProto3Json ?: boolean ;
131
141
} ) : void {
132
142
const { value, valueType, jsonValue } = opts ;
133
143
const protobufJsValue =
134
144
opts . protobufJsValue !== undefined
135
145
? opts . protobufJsValue
136
146
: opts . jsonValue ;
147
+ const serializer = opts . useProto3Json ? proto3JsonSerializer : s ;
137
148
138
149
// Convert FieldValue to JSON and verify.
139
- const actualJsonProto = s . toValue ( value ) ;
150
+ const actualJsonProto = serializer . toValue ( value ) ;
140
151
expect ( actualJsonProto ) . to . deep . equal ( { [ valueType ] : jsonValue } ) ;
141
152
142
- // Convert JSON to protobufjs and verify value.
143
- const actualProtobufjsProto : ProtobufJS . Message = ValueMessage . fromObject (
144
- actualJsonProto
145
- ) ;
146
- expect ( actualProtobufjsProto [ valueType ] ) . to . deep . equal ( protobufJsValue ) ;
153
+ // If we're using protobufJs JSON (not Proto3Json), then round-trip through protobufjs.
154
+ if ( ! opts . useProto3Json ) {
155
+ // Convert JSON to protobufjs and verify value.
156
+ const actualProtobufjsProto : ProtobufJS . Message = ValueMessage . fromObject (
157
+ actualJsonProto
158
+ ) ;
159
+ expect ( actualProtobufjsProto [ valueType ] ) . to . deep . equal ( protobufJsValue ) ;
147
160
148
- // Convert protobufjs back to JSON.
149
- const returnJsonProto = ValueMessage . toObject (
150
- actualProtobufjsProto ,
151
- protoLoaderOptions
152
- ) ;
153
- expect ( returnJsonProto ) . to . deep . equal ( { [ valueType ] : jsonValue } ) ;
161
+ // Convert protobufjs back to JSON.
162
+ const returnJsonProto = ValueMessage . toObject (
163
+ actualProtobufjsProto ,
164
+ protoLoaderOptions
165
+ ) ;
166
+ expect ( returnJsonProto ) . to . deep . equal ( actualJsonProto ) ;
167
+ }
154
168
155
169
// Convert JSON back to FieldValue.
156
- const actualReturnFieldValue = s . fromValue ( returnJsonProto ) ;
170
+ const actualReturnFieldValue = serializer . fromValue ( actualJsonProto ) ;
157
171
expect ( actualReturnFieldValue . isEqual ( value ) ) . to . be . true ;
158
172
}
159
173
@@ -316,16 +330,12 @@ describe('Serializer', () => {
316
330
317
331
it ( 'converts BlobValue to Base64 string (useProto3Json=true)' , ( ) => {
318
332
const base64 = 'AAECAwQF' ;
319
- const bytes = [ 0 , 1 , 2 , 3 , 4 , 5 ] ;
320
- const example = Blob . fromUint8Array ( new Uint8Array ( bytes ) ) ;
321
-
322
- const expected = { bytesValue : base64 } ;
323
-
324
- const serializer = new JsonProtoSerializer ( partition , {
333
+ verifyFieldValueRoundTrip ( {
334
+ value : new fieldValue . BlobValue ( Blob . fromBase64String ( base64 ) ) ,
335
+ valueType : 'bytesValue' ,
336
+ jsonValue : base64 ,
325
337
useProto3Json : true
326
338
} ) ;
327
- const obj = serializer . toValue ( new fieldValue . BlobValue ( example ) ) ;
328
- expect ( obj ) . to . deep . equal ( expected ) ;
329
339
} ) ;
330
340
331
341
it ( 'converts ArrayValue' , ( ) => {
0 commit comments