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