var _ = require('lodash');

var marshallers = require('./marshallers');

var Core = {
  input: function(data, applyUserTransforms) {
    var self = this;
    return _.reduce(data, function(result, value, name) {
      var attribute = self._getAttribute(name);
      if (!(attribute.guarded || attribute.virtual)) {
        result[name] = value;
      }
      return result;
    }, {});
  },
  output: function(data, options, applyUserTransforms) {
    var self = this;
    return _.reduce(data, function(result, value, name) {
      var attribute = self._getAttribute(name);
      if (!(applyUserTransforms && attribute.hidden)) {
        result[name] = value;
      }
      return result;
    }, {});
  },
  save: function(data, cb) {
    // Only check for required variables on initial save.
    if (!this.saved) {
      var attributeNames = _.keys(this.model._attributes);
      var name = _.find(attributeNames, function(name) {
        return (name === null || name === undefined);
      });
      if (name) {return cb('attribute "' + name + '" is required');}
    }
    cb(null, data);
  }
};

var marshaller = marshallers.createInstance(marshallers.JSONMarshaller);

var Serialize = {
  input: function(data, applyUserTransforms) {
    if (applyUserTransforms) {
      data = marshaller.unserializeData(data, this.model._attributeTypes);
    }
    return data;
  },
  output: function(data, options, applyUserTransforms) {
    if (applyUserTransforms) {
      data = serialize(this.model.types, data);
    }
    return data;
  }
}

module.exports = {
  Core: Core,
  Serialize: Serialize
};

@@ -1114,35 +1114,8 @@
 r = 
-marshallers.createInstance(
 mars
@@ -1136,17 +1136,16 @@
 rshaller
-)
 ;%0A%0Avar S
@@ -1241,20 +1241,26 @@
 %7B%0A      
-data
+var result
  = marsh
@@ -1264,16 +1264,17 @@
 rshaller
+s
 .unseria
@@ -1282,21 +1282,35 @@
 izeData(
-data,
+marshaller,%0A       
  this.mo
@@ -1328,19 +1328,132 @@
 uteTypes
+, data
 );%0A
+      // Store errors that should be thrown.%0A      this._errors = result.errors;%0A      data = result.data;%0A
     %7D%0A  
@@ -1577,64 +1577,225 @@
 a = 
-serialize(this.model.types, data);%0A    %7D%0A    return data
+marshallers.serializeData(marshaller, this.model._attributeTypes,%0A        data);%0A    %7D%0A    return data;%0A  %7D,%0A  save: function(data, cb) %7B%0A    // Fail if there were errors when unserializing.%0A    cb(this._errors, data)
 ;%0A  

Use refactored marshallers