forked from protobufjs/protobuf.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom to, from object for google.protobuf Timestamp and Duration
* Support Date or date string for Timestamp in fromObject * Conversion to JSON in toObject to Timestamp outputs a Date object * Support parsing simple duration strings, eg 5s or 1m, to Duration. Fractions of a second are not supported, eg 1.05s * Conversion to JSON in toObject for Duration outputs a string with the second unit only if the nanos component is zero
- Loading branch information
1 parent
e1bb46c
commit a6eddb4
Showing
4 changed files
with
148 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// this example demonstrates how to use well known types. | ||
|
||
/*eslint-disable strict, no-console*/ | ||
var protobuf = require(".."); | ||
|
||
var root = protobuf.Root.fromJSON({ | ||
nested: { | ||
google: { | ||
nested: { | ||
protobuf: { | ||
nested: { | ||
Timestamp: { | ||
fields: { | ||
seconds: { | ||
type: "int64", | ||
id: 1 | ||
}, | ||
nanos: { | ||
type: "int32", | ||
id: 2 | ||
} | ||
} | ||
}, | ||
Duration: { | ||
fields: { | ||
seconds: { | ||
type: "int64", | ||
id: 1 | ||
}, | ||
nanos: { | ||
type: "int32", | ||
id: 2 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
Message: { | ||
fields: { | ||
timestamp: { | ||
type: "google.protobuf.Timestamp", | ||
id: 1 | ||
}, | ||
duration: { | ||
type: "google.protobuf.Duration", | ||
id: 2 | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
var Message = root.lookup("Message"); | ||
|
||
const msg = Message.fromObject({ | ||
timestamp: new Date("2019-01-01"), | ||
duration: "5m", | ||
}); | ||
|
||
console.log(msg); | ||
|
||
console.log(msg.toJSON()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters