Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # BSON parser
  2. BSON is short for Bin­ary JSON and is the bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. You can learn more about it in [the specification](http://bsonspec.org).
  3. This browser version of the BSON parser is compiled using [webpack](https://webpack.js.org/) and the current version is pre-compiled in the `browser_build` directory.
  4. This is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at [mongod-js/bson-ext](https://github.com/mongodb-js/bson-ext).
  5. ## Usage
  6. To build a new version perform the following operations:
  7. ```
  8. npm install
  9. npm run build
  10. ```
  11. A simple example of how to use BSON in the browser:
  12. ```html
  13. <script src="./browser_build/bson.js"></script>
  14. <script>
  15. function start() {
  16. // Get the Long type
  17. var Long = BSON.Long;
  18. // Create a bson parser instance
  19. var bson = new BSON();
  20. // Serialize document
  21. var doc = { long: Long.fromNumber(100) }
  22. // Serialize a document
  23. var data = bson.serialize(doc)
  24. // De serialize it again
  25. var doc_2 = bson.deserialize(data)
  26. }
  27. </script>
  28. ```
  29. A simple example of how to use BSON in `Node.js`:
  30. ```js
  31. // Get BSON parser class
  32. var BSON = require('bson')
  33. // Get the Long type
  34. var Long = BSON.Long;
  35. // Create a bson parser instance
  36. var bson = new BSON();
  37. // Serialize document
  38. var doc = { long: Long.fromNumber(100) }
  39. // Serialize a document
  40. var data = bson.serialize(doc)
  41. console.log('data:', data)
  42. // Deserialize the resulting Buffer
  43. var doc_2 = bson.deserialize(data)
  44. console.log('doc_2:', doc_2)
  45. ```
  46. ## Installation
  47. `npm install bson`
  48. ## API
  49. ### BSON types
  50. For all BSON types documentation, please refer to the following sources:
  51. * [MongoDB BSON Type Reference](https://docs.mongodb.com/manual/reference/bson-types/)
  52. * [BSON Spec](https://bsonspec.org/)
  53. ### BSON serialization and deserialiation
  54. **`new BSON()`** - Creates a new BSON serializer/deserializer you can use to serialize and deserialize BSON.
  55. #### BSON.serialize
  56. The BSON `serialize` method takes a JavaScript object and an optional options object and returns a Node.js Buffer.
  57. * `BSON.serialize(object, options)`
  58. * @param {Object} object the JavaScript object to serialize.
  59. * @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid.
  60. * @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
  61. * @param {Boolean} [options.ignoreUndefined=true]
  62. * @return {Buffer} returns a Buffer instance.
  63. #### BSON.serializeWithBufferAndIndex
  64. The BSON `serializeWithBufferAndIndex` method takes an object, a target buffer instance and an optional options object and returns the end serialization index in the final buffer.
  65. * `BSON.serializeWithBufferAndIndex(object, buffer, options)`
  66. * @param {Object} object the JavaScript object to serialize.
  67. * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
  68. * @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid.
  69. * @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
  70. * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields.
  71. * @param {Number} [options.index=0] the index in the buffer where we wish to start serializing into.
  72. * @return {Number} returns the index pointing to the last written byte in the buffer.
  73. #### BSON.calculateObjectSize
  74. The BSON `calculateObjectSize` method takes a JavaScript object and an optional options object and returns the size of the BSON object.
  75. * `BSON.calculateObjectSize(object, options)`
  76. * @param {Object} object the JavaScript object to serialize.
  77. * @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
  78. * @param {Boolean} [options.ignoreUndefined=true]
  79. * @return {Buffer} returns a Buffer instance.
  80. #### BSON.deserialize
  81. The BSON `deserialize` method takes a Node.js Buffer and an optional options object and returns a deserialized JavaScript object.
  82. * `BSON.deserialize(buffer, options)`
  83. * @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
  84. * @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
  85. * @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
  86. * @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
  87. * @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
  88. * @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
  89. * @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
  90. * @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
  91. * @return {Object} returns the deserialized Javascript Object.
  92. #### BSON.deserializeStream
  93. The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and allow more control over deserialization of a Buffer containing concatenated BSON documents.
  94. * `BSON.deserializeStream(buffer, startIndex, numberOfDocuments, documents, docStartIndex, options)`
  95. * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
  96. * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
  97. * @param {Number} numberOfDocuments number of documents to deserialize.
  98. * @param {Array} documents an array where to store the deserialized documents.
  99. * @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
  100. * @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
  101. * @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
  102. * @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
  103. * @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
  104. * @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
  105. * @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
  106. * @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
  107. * @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
  108. * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
  109. ## FAQ
  110. #### Why does `undefined` get converted to `null`?
  111. The `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys.
  112. #### How do I add custom serialization logic?
  113. This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.
  114. ```javascript
  115. var bson = new BSON();
  116. class CustomSerialize {
  117. toBSON() {
  118. return 42;
  119. }
  120. }
  121. const obj = { answer: new CustomSerialize() };
  122. // "{ answer: 42 }"
  123. console.log(bson.deserialize(bson.serialize(obj)));
  124. ```