proto3-json-serializer
Advanced tools
Comparing version 1.0.3 to 1.1.0
/// <reference types="node" /> | ||
import * as protobuf from 'protobufjs'; | ||
import { ToProto3JSONOptions } from './toproto3json'; | ||
import { JSONObject, JSONValue } from './types'; | ||
@@ -8,3 +9,3 @@ export interface Any { | ||
} | ||
export declare function googleProtobufAnyToProto3JSON(obj: protobuf.Message & Any): JSONObject; | ||
export declare function googleProtobufAnyToProto3JSON(obj: protobuf.Message & Any, options?: ToProto3JSONOptions): JSONObject; | ||
export declare function googleProtobufAnyFromProto3JSON(root: protobuf.Root, json: JSONValue): { | ||
@@ -11,0 +12,0 @@ type_url: string; |
@@ -29,3 +29,3 @@ "use strict"; | ||
]); | ||
function googleProtobufAnyToProto3JSON(obj) { | ||
function googleProtobufAnyToProto3JSON(obj, options) { | ||
// https://developers.google.com/protocol-buffers/docs/proto3#json | ||
@@ -45,3 +45,3 @@ // If the Any contains a value that has a special JSON mapping, it will be converted as follows: | ||
const valueMessage = type.decode(obj.value); | ||
const valueProto3JSON = (0, toproto3json_1.toProto3JSON)(valueMessage); | ||
const valueProto3JSON = (0, toproto3json_1.toProto3JSON)(valueMessage, options); | ||
if (specialJSON.has(typeName)) { | ||
@@ -48,0 +48,0 @@ return { |
import * as protobuf from 'protobufjs'; | ||
import { JSONValue } from './types'; | ||
export declare function resolveEnumValueToString(enumType: protobuf.Enum, enumValue: JSONValue): string | number; | ||
export declare function resolveEnumValueToNumber(enumType: protobuf.Enum, enumValue: JSONValue): string | number; |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resolveEnumValueToString = void 0; | ||
exports.resolveEnumValueToNumber = exports.resolveEnumValueToString = void 0; | ||
function resolveEnumValueToString(enumType, enumValue) { | ||
@@ -37,2 +37,18 @@ // for unknown enum values, do not fail and try to do the best we could. | ||
exports.resolveEnumValueToString = resolveEnumValueToString; | ||
function resolveEnumValueToNumber(enumType, enumValue) { | ||
if (typeof enumValue === 'number') { | ||
// return as is | ||
return enumValue; | ||
} | ||
if (typeof enumValue === 'string') { | ||
const num = enumType.values[enumValue]; | ||
if (typeof num === 'undefined') { | ||
// unknown value, cannot convert to number, returning string as is | ||
return enumValue; | ||
} | ||
return num; | ||
} | ||
throw new Error('resolveEnumValueToNumber: enum value must be a string or a number'); | ||
} | ||
exports.resolveEnumValueToNumber = resolveEnumValueToNumber; | ||
//# sourceMappingURL=enum.js.map |
export { JSONObject, JSONValue } from './types'; | ||
export { toProto3JSON } from './toproto3json'; | ||
export { toProto3JSON, ToProto3JSONOptions } from './toproto3json'; | ||
export { fromProto3JSON } from './fromproto3json'; |
import * as protobuf from 'protobufjs'; | ||
import { JSONValue } from './types'; | ||
export declare function toProto3JSON(obj: protobuf.Message): JSONValue; | ||
export interface ToProto3JSONOptions { | ||
numericEnums: boolean; | ||
} | ||
export declare function toProto3JSON(obj: protobuf.Message, options?: ToProto3JSONOptions): JSONValue; |
@@ -29,3 +29,3 @@ "use strict"; | ||
}; | ||
function toProto3JSON(obj) { | ||
function toProto3JSON(obj, options) { | ||
const objType = obj.$type; | ||
@@ -40,3 +40,3 @@ if (!objType) { | ||
if (typeName === '.google.protobuf.Any') { | ||
return (0, any_1.googleProtobufAnyToProto3JSON)(obj); | ||
return (0, any_1.googleProtobufAnyToProto3JSON)(obj, options); | ||
} | ||
@@ -83,3 +83,3 @@ if (typeName === '.google.protobuf.Value') { | ||
? element => { | ||
return toProto3JSON(element); | ||
return toProto3JSON(element, options); | ||
} | ||
@@ -94,3 +94,3 @@ : id); | ||
map[mapKey] = fieldResolvedType | ||
? toProto3JSON(mapValue) | ||
? toProto3JSON(mapValue, options) | ||
: mapValue; | ||
@@ -106,7 +106,12 @@ } | ||
if (fieldResolvedType && 'values' in fieldResolvedType && value !== null) { | ||
result[key] = (0, enum_1.resolveEnumValueToString)(fieldResolvedType, value); | ||
if (options === null || options === void 0 ? void 0 : options.numericEnums) { | ||
result[key] = (0, enum_1.resolveEnumValueToNumber)(fieldResolvedType, value); | ||
} | ||
else { | ||
result[key] = (0, enum_1.resolveEnumValueToString)(fieldResolvedType, value); | ||
} | ||
continue; | ||
} | ||
if (fieldResolvedType) { | ||
result[key] = toProto3JSON(value); | ||
result[key] = toProto3JSON(value, options); | ||
continue; | ||
@@ -113,0 +118,0 @@ } |
# Changelog | ||
## [1.1.0](https://github.com/googleapis/proto3-json-serializer-nodejs/compare/v1.0.3...v1.1.0) (2022-08-26) | ||
### Features | ||
* option to serialize enum values as numbers ([#60](https://github.com/googleapis/proto3-json-serializer-nodejs/issues/60)) ([456b771](https://github.com/googleapis/proto3-json-serializer-nodejs/commit/456b771d5fef06d914c6e201fd9f17251e55d4d9)) | ||
### Bug Fixes | ||
* remove pip install statements ([#1546](https://github.com/googleapis/proto3-json-serializer-nodejs/issues/1546)) ([#58](https://github.com/googleapis/proto3-json-serializer-nodejs/issues/58)) ([741d070](https://github.com/googleapis/proto3-json-serializer-nodejs/commit/741d0704f49332dd5d66206fcdf2111464fb8759)) | ||
## [1.0.3](https://github.com/googleapis/proto3-json-serializer-nodejs/compare/v1.0.2...v1.0.3) (2022-07-10) | ||
@@ -4,0 +16,0 @@ |
{ | ||
"name": "proto3-json-serializer", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"repository": "googleapis/proto3-json-serializer-nodejs", | ||
@@ -5,0 +5,0 @@ "description": "Support for proto3 JSON serialiazation/deserialization for protobuf.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85549
964