toproto3json.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. // Copyright 2021 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.toProto3JSON = void 0;
  17. const any_1 = require("./any");
  18. const bytes_1 = require("./bytes");
  19. const util_1 = require("./util");
  20. const enum_1 = require("./enum");
  21. const value_1 = require("./value");
  22. const duration_1 = require("./duration");
  23. const timestamp_1 = require("./timestamp");
  24. const wrappers_1 = require("./wrappers");
  25. const fieldmask_1 = require("./fieldmask");
  26. // Convert a single value, which might happen to be an instance of Long, to JSONValue
  27. function convertSingleValue(value) {
  28. var _a;
  29. if (typeof value === 'object') {
  30. if (((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Long') {
  31. return value.toString();
  32. }
  33. throw new Error(`toProto3JSON: don't know how to convert value ${value}`);
  34. }
  35. return value;
  36. }
  37. function toProto3JSON(obj, options) {
  38. const objType = obj.$type;
  39. if (!objType) {
  40. throw new Error('Cannot serialize object to proto3 JSON since its .$type is unknown. Use Type.fromObject(obj) before calling toProto3JSON.');
  41. }
  42. objType.resolveAll();
  43. const typeName = (0, util_1.getFullyQualifiedTypeName)(objType);
  44. // Types that require special handling according to
  45. // https://developers.google.com/protocol-buffers/docs/proto3#json
  46. if (typeName === '.google.protobuf.Any') {
  47. return (0, any_1.googleProtobufAnyToProto3JSON)(obj, options);
  48. }
  49. if (typeName === '.google.protobuf.Value') {
  50. return (0, value_1.googleProtobufValueToProto3JSON)(obj);
  51. }
  52. if (typeName === '.google.protobuf.Struct') {
  53. return (0, value_1.googleProtobufStructToProto3JSON)(obj);
  54. }
  55. if (typeName === '.google.protobuf.ListValue') {
  56. return (0, value_1.googleProtobufListValueToProto3JSON)(obj);
  57. }
  58. if (typeName === '.google.protobuf.Duration') {
  59. return (0, duration_1.googleProtobufDurationToProto3JSON)(obj);
  60. }
  61. if (typeName === '.google.protobuf.Timestamp') {
  62. return (0, timestamp_1.googleProtobufTimestampToProto3JSON)(obj);
  63. }
  64. if (typeName === '.google.protobuf.FieldMask') {
  65. return (0, fieldmask_1.googleProtobufFieldMaskToProto3JSON)(obj);
  66. }
  67. if (util_1.wrapperTypes.has(typeName)) {
  68. return (0, wrappers_1.wrapperToProto3JSON)(obj);
  69. }
  70. const result = {};
  71. for (const [key, value] of Object.entries(obj)) {
  72. const field = objType.fields[key];
  73. const fieldResolvedType = field.resolvedType;
  74. const fieldFullyQualifiedTypeName = fieldResolvedType
  75. ? (0, util_1.getFullyQualifiedTypeName)(fieldResolvedType)
  76. : null;
  77. if (value === null) {
  78. result[key] = null;
  79. continue;
  80. }
  81. if (Array.isArray(value)) {
  82. if (value.length === 0) {
  83. // ignore repeated fields with no values
  84. continue;
  85. }
  86. // if the repeated value has a complex type, convert it to proto3 JSON, otherwise use as is
  87. result[key] = value.map(fieldResolvedType
  88. ? element => {
  89. return toProto3JSON(element, options);
  90. }
  91. : convertSingleValue);
  92. continue;
  93. }
  94. if (field.map) {
  95. const map = {};
  96. for (const [mapKey, mapValue] of Object.entries(value)) {
  97. // if the map value has a complex type, convert it to proto3 JSON, otherwise use as is
  98. map[mapKey] = fieldResolvedType
  99. ? toProto3JSON(mapValue, options)
  100. : convertSingleValue(mapValue);
  101. }
  102. result[key] = map;
  103. continue;
  104. }
  105. if (fieldFullyQualifiedTypeName === '.google.protobuf.NullValue') {
  106. result[key] = null;
  107. continue;
  108. }
  109. if (fieldResolvedType && 'values' in fieldResolvedType && value !== null) {
  110. if (options === null || options === void 0 ? void 0 : options.numericEnums) {
  111. result[key] = (0, enum_1.resolveEnumValueToNumber)(fieldResolvedType, value);
  112. }
  113. else {
  114. result[key] = (0, enum_1.resolveEnumValueToString)(fieldResolvedType, value);
  115. }
  116. continue;
  117. }
  118. if (fieldResolvedType) {
  119. result[key] = toProto3JSON(value, options);
  120. continue;
  121. }
  122. if (typeof value === 'string' ||
  123. typeof value === 'number' ||
  124. typeof value === 'boolean' ||
  125. value === null) {
  126. if (typeof value === 'number' && !Number.isFinite(value)) {
  127. result[key] = value.toString();
  128. continue;
  129. }
  130. result[key] = value;
  131. continue;
  132. }
  133. if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
  134. result[key] = (0, bytes_1.bytesToProto3JSON)(value);
  135. continue;
  136. }
  137. result[key] = convertSingleValue(value);
  138. continue;
  139. }
  140. return result;
  141. }
  142. exports.toProto3JSON = toProto3JSON;
  143. //# sourceMappingURL=toproto3json.js.map