Browse files

Adds Swiz.buildObjectSync(obj) to support a synchronous mode for seri…

…alizing objects
  • Loading branch information...
1 parent ea673f3 commit 21df078cc40a33011b68ae87cb208c923c54ebf0 @robert-chiniquy robert-chiniquy committed Nov 5, 2012
Showing with 106 additions and 2 deletions.
  1. +81 −2 lib/serializer.js
  2. +25 −0 tests/test-swiz.js
View
83 lib/serializer.js
@@ -210,6 +210,85 @@ Swiz.prototype._buildObject = function(parent, obj, callback) {
}
};
+/**
+ * Given a datastructure supported by Swiz, convert it into an Object that
+ * can be serialized directly to JSON, or to XML using Swiz's serializeXml
+ * method. This method may be called synchronously.
+ *
+ * Note: Currently, calling functions in swiz definitions is not supported
+ * for this method. If you require this, use Swiz.buildObject()
+ *
+ * @param {Object} obj The datastructure to be converted.
+ * @returns {Object} The Swiz representation of the object.
+ */
+Swiz.prototype.buildObjectSync = function(obj) {
+ var result, self = this;
+
+ if (obj instanceof Function) {
+ // this feature is not supported in buildObjectSync yet
+ return;
+ }
+ else if (obj instanceof Array) {
+ // Recurse onto every element of an array
+ return obj.map(self.buildObjectSync);
+ }
+ else if (obj instanceof Object && obj.getSerializerType) {
+ // Recurse onto each property named in the definition
+ stype = obj.getSerializerType();
+ def = this._defs[stype];
+ result = {};
+
+ Object.defineProperty(result, 'serializerType', {
+ value: stype,
+ enumerable: false
+ });
+
+ if (!def) {
+ throw(new Error('No definition for this type; no way to serialize ' + stype));
+ }
+
+ def.fields.forEach(function(field, callback) {
+ var dst = field.name;
+ var src = field.src || dst;
+ var k;
+
+ if (self._options.for && (field.filterFrom.length > 0) &&
+ (field.filterFrom.indexOf(self._options.for) !== -1)) {
+ return;
+ }
+
+ if (field.enumerated) {
+ for (k in field.enumerated) {
+ if (field.enumerated[k] === obj[src]) {
+ result[dst] = k;
+ return;
+ }
+ }
+ }
+
+ result[dst] = self.buildObjectSync(obj[src]);
+ });
+ return result;
+ }
+ else if (obj instanceof Object) {
+ // Treat it as a map, recurse onto each property
+ keys = Object.keys(obj);
+ result = {};
+
+ keys.forEach(function(key) {
+ if (!obj.hasOwnProperty(key)) {
+ return;
+ }
+ result[key] = self.buildObjectSync(obj[key]);
+ });
+ return result;
+ }
+ else {
+ // Simple value, pass it back
+ return obj;
+ }
+};
+
// transforms object prior to being fed through JSON.parse()
function jsonTransform(options) {
return function(key, value) {
@@ -566,14 +645,14 @@ Swiz.prototype._serializeXml = function(stype, elem, obj, arrayItem, key) {
value = obj.toString();
field = (stype) ? this.getFieldDefinition(stype, key) : null;
attribute = (field) ? field.attribute : false;
-
+
if (attribute) {
// Key will be used as an attribute
if (!elem) {
throw new Error(sprintf('%s should be used as an attribute, but it doesn\'t ' +
'have a parent element', key));
}
-
+
elem.set(key, value);
return elem;
}
View
25 tests/test-swiz.js
@@ -277,6 +277,31 @@ exports['test_build_object'] = function(test, assert) {
});
};
+exports['test_build_object_sync'] = function(test, assert) {
+ var blahnode = new Node();
+ var sw = new swiz.Swiz(def);
+ var result = sw.buildObjectSync(blahnode);
+
+ assert.equal(result.id, 15245);
+ assert.equal(result.is_active, true);
+ // don't expect get_name to be set, functions will not be called in buildObjectSync
+ assert.equal(result.agent_name, 'gl<ah');
+ // don't expect ip_address to be set either
+ assert.deepEqual(result.public_ips, ['123.45.55.44', '122.123.32.2']);
+ assert.deepEqual(result.data, {
+ foo: 'thingone',
+ bar: 'thingtwo'
+ });
+ assert.equal(result.state, 'active');
+ assert.deepEqual(result.opts, {
+ option1: 'defaultval',
+ option2: 'defaultval',
+ // do not expect a function to be called to return a value
+ option3: undefined
+ });
+ test.finish();
+};
+
exports['test_deserialize_text_only_entities'] = function(test, assert) {
var xml = '<accounting><entities>2</entities><serializerType>accounting</serializerType></accounting>';
var sw = new swiz.Swiz(def, {stripNulls: true});

0 comments on commit 21df078

Please sign in to comment.