chensenlai 10988628a0 语音房项目初始化 | 5 years ago | |
---|---|---|
.. | ||
browser_build | 5 years ago | |
lib | 5 years ago | |
tools | 5 years ago | |
.travis.yml | 5 years ago | |
HISTORY | 5 years ago | |
LICENSE | 5 years ago | |
README.md | 5 years ago | |
package.json | 5 years ago |
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)