Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing to custom targets (classes) #253

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/bson/parser/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var deserialize = function(buffer, options, isArray) {
return deserializeObject(buffer, index, options, isArray);
};

var deserializeObject = function(buffer, index, options, isArray) {
var deserializeObject = function(buffer, index, options, isArray, target) {
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
var cacheFunctionsCrc32 =
Expand Down Expand Up @@ -73,11 +73,14 @@ var deserializeObject = function(buffer, index, options, isArray) {
if (size < 5 || size > buffer.length) throw new Error('corrupt bson message');

// Create holding object
var object = isArray ? [] : {};
var object = (target instanceof Function) ? new target() : (isArray ? [] : {});
// Used for arrays to skip having to perform utf8 decoding
var arrayIndex = 0;

var done = false;
var newTarget = null;

if (isArray && target) newTarget = target['$'];

// While we have more left data left keep parsing
// while (buffer[index + 1] !== 0) {
Expand All @@ -98,6 +101,9 @@ var deserializeObject = function(buffer, index, options, isArray) {
if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString');
var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);

if (name === 'firstBatch' || name === 'nextBatch') newTarget = {$: options.target};
else if (!isArray && target) newTarget = target['$' + name];

index = i + 1;

if (elementType === BSON.BSON_DATA_STRING) {
Expand Down Expand Up @@ -164,7 +170,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
if (raw) {
object[name] = buffer.slice(index, index + objectSize);
} else {
object[name] = deserializeObject(buffer, _index, options, false);
object[name] = deserializeObject(buffer, _index, options, false, newTarget);
}

index = index + objectSize;
Expand All @@ -187,7 +193,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
arrayOptions['raw'] = true;
}

object[name] = deserializeObject(buffer, _index, arrayOptions, true);
object[name] = deserializeObject(buffer, _index, arrayOptions, true, newTarget);
index = index + objectSize;

if (buffer[index - 1] !== 0) throw new Error('invalid array terminator byte');
Expand Down