proto3-json-serializer
Advanced tools
Comparing version
@@ -36,2 +36,21 @@ "use strict"; | ||
} | ||
// Convert a value within a repeated or map field | ||
function convertRepeatedOrMapValue(type, value, options) { | ||
if (type && 'values' in type) { | ||
return convertEnum(type, value, options); | ||
} | ||
if (type) { | ||
return toProto3JSON(value, options); | ||
} | ||
return convertSingleValue(value); | ||
} | ||
// Convert an enum type to its value | ||
function convertEnum(type, value, options) { | ||
if (options?.numericEnums) { | ||
return (0, enum_1.resolveEnumValueToNumber)(type, value); | ||
} | ||
else { | ||
return (0, enum_1.resolveEnumValueToString)(type, value); | ||
} | ||
} | ||
function toProto3JSON(obj, options) { | ||
@@ -86,8 +105,5 @@ const objType = obj.$type; | ||
} | ||
// 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); | ||
result[key] = value.map(element => { | ||
return convertRepeatedOrMapValue(fieldResolvedType, element, options); | ||
}); | ||
continue; | ||
@@ -98,6 +114,3 @@ } | ||
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, options) | ||
: convertSingleValue(mapValue); | ||
map[mapKey] = convertRepeatedOrMapValue(fieldResolvedType, mapValue, options); | ||
} | ||
@@ -112,8 +125,3 @@ result[key] = map; | ||
if (fieldResolvedType && 'values' in fieldResolvedType && value !== null) { | ||
if (options?.numericEnums) { | ||
result[key] = (0, enum_1.resolveEnumValueToNumber)(fieldResolvedType, value); | ||
} | ||
else { | ||
result[key] = (0, enum_1.resolveEnumValueToString)(fieldResolvedType, value); | ||
} | ||
result[key] = convertEnum(fieldResolvedType, value, options); | ||
continue; | ||
@@ -120,0 +128,0 @@ } |
{ | ||
"name": "proto3-json-serializer", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"repository": "googleapis/proto3-json-serializer-nodejs", | ||
@@ -45,10 +45,10 @@ "description": "Support for proto3 JSON serialiazation/deserialization for protobuf.js", | ||
"c8": "^10.1.3", | ||
"google-proto-files": "^4.2.0", | ||
"google-proto-files": "^5.0.0", | ||
"gts": "^6.0.2", | ||
"jsdoc": "^4.0.4", | ||
"jsdoc-fresh": "^3.0.0", | ||
"jsdoc-fresh": "^4.0.0", | ||
"jsdoc-region-tag": "^3.0.0", | ||
"linkinator": "^6.1.2", | ||
"mocha": "^11.1.0", | ||
"pack-n-play": "^2.1.0", | ||
"pack-n-play": "^3.0.0", | ||
"protobufjs-cli": "^1.1.3", | ||
@@ -55,0 +55,0 @@ "typescript": "^5.7.3" |
Sorry, the diff of this file is not supported yet
966
0.84%1
Infinity%80217
-9%42
-2.33%