Skip to content

Commit 3939667

Browse files
committed
New: Added 'json' conversion option for proto3 JSON mapping compatibility of NaN and Infinity + additional documentation of util.toJSONOptions, see #351
1 parent 0425b58 commit 3939667

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ Supported decorators are:
570570
* **OneOf.d&lt;T extends string>(...fieldNames: `string[]`)**<br />
571571
annotates a property as a protobuf oneof covering the specified fields.
572572

573-
Decorated types reside in `protobuf.roots.decorators` using a flat structure (no duplicate names).
573+
Decorated types reside in `protobuf.roots["decorators"]` using a flat structure (no duplicate names).
574574

575575
Command line
576576
------------

src/converter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
4040
var isUnsigned = false;
4141
switch (field.type) {
4242
case "double":
43-
case "float":gen
44-
("m%s=Number(d%s)", prop, prop);
43+
case "float": gen
44+
("m%s=Number(d%s)", prop, prop); // also catches "NaN", "Infinity"
4545
break;
4646
case "uint32":
4747
case "fixed32": gen
@@ -162,6 +162,10 @@ function genValuePartial_toObject(gen, field, fieldIndex, prop) {
162162
} else {
163163
var isUnsigned = false;
164164
switch (field.type) {
165+
case "double":
166+
case "float": gen
167+
("d%s=o.json&&!isFinite(m%s)?String(m%s):m%s", prop, prop, prop, prop);
168+
break;
165169
case "uint64":
166170
isUnsigned = true;
167171
// eslint-disable-line no-fallthrough

src/type.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ Type.prototype.fromObject = function fromObject(object) {
537537
* @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false`
538538
* @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false`
539539
* @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any
540+
* @property {boolean} [json=false] Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings
540541
*/
541542

542543
/**

src/util/minimal.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,26 @@ util.oneOfSetter = function setOneOf(fieldNames) {
375375
};
376376

377377
/**
378-
* Default conversion options used for {@link Message#toJSON} implementations. Longs, enums and bytes are converted to strings by default.
378+
* Default conversion options used for {@link Message#toJSON} implementations.
379+
*
380+
* These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
381+
*
382+
* - Longs become strings
383+
* - Enums become string keys
384+
* - Bytes become base64 encoded strings
385+
* - (Sub-)Messages become plain objects
386+
* - Maps become plain objects with all string keys
387+
* - Repeated fields become arrays
388+
* - NaN and Infinity for float and double fields become strings
389+
*
379390
* @type {ConversionOptions}
391+
* @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
380392
*/
381393
util.toJSONOptions = {
382394
longs: String,
383395
enums: String,
384-
bytes: String
396+
bytes: String,
397+
json: true
385398
};
386399

387400
util._configure = function() {

0 commit comments

Comments
 (0)