code: function(rec) {
              var dat;
              if( !angular.isObject(rec) ) {
                rec = {$value: rec};
              }
              if (angular.isFunction(rec.toJSON)) {
                dat = rec.toJSON();
              }
              else {
                dat = {};
                utils.each(rec, function (v, k) {
                  dat[k] = stripDollarPrefixedKeys(v);
                });
              }
              if( angular.isDefined(rec.$value) && Object.keys(dat).length === 0 && rec.$value !== null ) {
                dat['.value'] = rec.$value;
              }
              if( angular.isDefined(rec.$priority) && Object.keys(dat).length > 0 && rec.$priority !== null ) {
                dat['.priority'] = rec.$priority;
              }
              angular.forEach(dat, function(v,k) {
                if (k.match(/[.$\[\]#\/]/) && k !== '.value' && k !== '.priority' ) {
                  throw new Error('Invalid key ' + k + ' (cannot contain .$[]#)');
                }
                else if( angular.isUndefined(v) ) {
                  throw new Error('Key '+k+' was undefined. Cannot pass undefined in JSON. Use null instead.');
                }
              });
              return dat;
            }
docs: A utility for converting records to JSON objects
  which we can save into Wilddog. It asserts valid
  keys and strips off any items prefixed with $.
  
  If the rec passed into this method has a toJSON()
  method, that will be used in place of the custom
  functionality here.
  
  @param rec
  @returns {*}