Skip to content

Commit

Permalink
New: Added 'json' conversion option for proto3 JSON mapping compatibi…
Browse files Browse the repository at this point in the history
…lity of NaN and Infinity + additional documentation of util.toJSONOptions, see #351
  • Loading branch information
dcodeIO committed Apr 11, 2017
1 parent 0425b58 commit 3939667
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ Supported decorators are:
* **OneOf.d&lt;T extends string>(...fieldNames: `string[]`)**<br />
annotates a property as a protobuf oneof covering the specified fields.
Decorated types reside in `protobuf.roots.decorators` using a flat structure (no duplicate names).
Decorated types reside in `protobuf.roots["decorators"]` using a flat structure (no duplicate names).
Command line
------------
Expand Down
8 changes: 6 additions & 2 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
var isUnsigned = false;
switch (field.type) {
case "double":
case "float":gen
("m%s=Number(d%s)", prop, prop);
case "float": gen
("m%s=Number(d%s)", prop, prop); // also catches "NaN", "Infinity"
break;
case "uint32":
case "fixed32": gen
Expand Down Expand Up @@ -162,6 +162,10 @@ function genValuePartial_toObject(gen, field, fieldIndex, prop) {
} else {
var isUnsigned = false;
switch (field.type) {
case "double":
case "float": gen
("d%s=o.json&&!isFinite(m%s)?String(m%s):m%s", prop, prop, prop, prop);
break;
case "uint64":
isUnsigned = true;
// eslint-disable-line no-fallthrough
Expand Down
1 change: 1 addition & 0 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Type.prototype.fromObject = function fromObject(object) {
* @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false`
* @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false`
* @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any
* @property {boolean} [json=false] Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings
*/

/**
Expand Down
17 changes: 15 additions & 2 deletions src/util/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,26 @@ util.oneOfSetter = function setOneOf(fieldNames) {
};

/**
* Default conversion options used for {@link Message#toJSON} implementations. Longs, enums and bytes are converted to strings by default.
* Default conversion options used for {@link Message#toJSON} implementations.
*
* These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
*
* - Longs become strings
* - Enums become string keys
* - Bytes become base64 encoded strings
* - (Sub-)Messages become plain objects
* - Maps become plain objects with all string keys
* - Repeated fields become arrays
* - NaN and Infinity for float and double fields become strings
*
* @type {ConversionOptions}
* @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
*/
util.toJSONOptions = {
longs: String,
enums: String,
bytes: String
bytes: String,
json: true
};

util._configure = function() {
Expand Down

0 comments on commit 3939667

Please sign in to comment.