Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
"keyType": "string",
"type": "Enum",
"id": 6
},
"bytesMapField": {
"keyType": "string",
"type": "bytes",
"id": 7
}
}
},
Expand Down Expand Up @@ -128,6 +133,11 @@
"rule": "repeated",
"type": "Enum",
"id": 7
},
"repeatedBytes": {
"rule": "repeated",
"type": "bytes",
"id": 8
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ message MessageWithMap {
map<string, string> string_map_field = 4;
map<string, int64> long_map_field = 5;
map<string, Enum> enum_map_field = 6;
map<string, bytes> bytes_map_field = 7;
}

message MapValue {
Expand All @@ -68,6 +69,7 @@ message MessageWithRepeated {
repeated string one_more_repeated_string = 5;
repeated int64 repeated_long = 6;
repeated Enum repeated_enum = 7;
repeated bytes repeated_bytes = 8;
}

message RepeatedValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function convertSingleValue(value: JSONValue | object): JSONValue {
if (value?.constructor?.name === 'Long') {
return (value as LongStub).toString();
}
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
return bytesToProto3JSON(value);
}
throw new Error(`toProto3JSON: don't know how to convert value ${value}`);
}
return value;
Expand Down Expand Up @@ -200,10 +203,6 @@ export function toProto3JSON(
result[key] = value;
continue;
}
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
result[key] = bytesToProto3JSON(value);
continue;
}
result[key] = convertSingleValue(value);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function testMap(root: protobuf.Root) {
'minus one': '-1',
zero: '0',
},
bytesMapField: {
key1: 'dmFsdWUgMQo=',
key2: 'dmFsdWUgMgo=',
},
});
const json = {
mapField: {
Expand All @@ -61,6 +65,10 @@ function testMap(root: protobuf.Root) {
zero: '0',
},
enumMapField: {},
bytesMapField: {
key1: 'dmFsdWUgMQo=',
key2: 'dmFsdWUgMgo=',
},
};

it('serializes to proto3 JSON', () => {
Expand Down Expand Up @@ -90,6 +98,7 @@ function testEnumMap(root: protobuf.Root) {
key1: 'UNKNOWN',
key2: 'KNOWN',
},
bytesMapField: {},
};
const jsonWithNumericEnums = {
mapField: {},
Expand All @@ -99,6 +108,7 @@ function testEnumMap(root: protobuf.Root) {
key1: 0,
key2: 1,
},
bytesMapField: {},
};

it('serializes to proto3 JSON with string enums', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,27 @@ function testRepeatedEnum(root: protobuf.Root) {
});
}

function testRepeatedBytes(root: protobuf.Root) {
const MessageWithRepeated = root.lookupType('test.MessageWithRepeated');
const message = MessageWithRepeated.fromObject({
repeatedBytes: ['dmFsdWUgMQo=', 'dmFsdWUgMgo='],
});
const json = {
repeatedBytes: ['dmFsdWUgMQo=', 'dmFsdWUgMgo='],
};

it('serializes to proto3 JSON', () => {
const serialized = toProto3JSON(message);
assert.deepStrictEqual(serialized, json);
});

it('deserializes from proto3 JSON', () => {
const deserialized = fromProto3JSON(MessageWithRepeated, json);
assert.deepStrictEqual(deserialized, message);
});
}

testTwoTypesOfLoad('repeated fields', testRepeated);
testTwoTypesOfLoad('empty repeated fields', testEmptyRepeated);
testTwoTypesOfLoad('repeated enums', testRepeatedEnum);
testTwoTypesOfLoad('repeated bytes', testRepeatedBytes);
Loading