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