chensenlai 10988628a0 语音房项目初始化 | %!s(int64=5) %!d(string=hai) anos | |
---|---|---|
.. | ||
browser_build | %!s(int64=5) %!d(string=hai) anos | |
lib | %!s(int64=5) %!d(string=hai) anos | |
tools | %!s(int64=5) %!d(string=hai) anos | |
.travis.yml | %!s(int64=5) %!d(string=hai) anos | |
HISTORY | %!s(int64=5) %!d(string=hai) anos | |
LICENSE | %!s(int64=5) %!d(string=hai) anos | |
README.md | %!s(int64=5) %!d(string=hai) anos | |
package.json | %!s(int64=5) %!d(string=hai) anos |
This BSON parser is primarily meant to be used with the mongodb
node.js driver.
However, wonderful tools such as onejs
can package up a BSON parser that will work in the browser.
The current build is located in the browser_build/bson.js
file.
A simple example of how to use BSON in the browser:
<html>
<head>
<script src="https://raw.github.com/mongodb/js-bson/master/browser_build/bson.js">
</script>
</head>
<body onload="start();">
<script>
function start() {
var BSON = bson().BSON;
var Long = bson().Long;
var doc = {long: Long.fromNumber(100)}
// Serialize a document
var data = BSON.serialize(doc, false, true, false);
// De serialize it again
var doc_2 = BSON.deserialize(data);
}
</script>
</body>
</html>
A simple example of how to use BSON in node.js
:
var bson = require("bson");
var BSON = bson.BSONPure.BSON;
var Long = bson.BSONPure.Long;
var doc = {long: Long.fromNumber(100)}
// Serialize a document
var data = BSON.serialize(doc, false, true, false);
console.log("data:", data);
// Deserialize the resulting Buffer
var doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);
The API consists of two simple methods to serialize/deserialize objects to/from BSON format:
BSON.serialize(object, checkKeys, asBuffer, serializeFunctions)
BSON.deserialize(buffer, options, isArray)