       if cb? then cb err else err
     else
       @_cache[id] = o
-      if cb? then  cb null, id else id
-  try
-    json = JSON.stringify data, null, 2
-    if cb? then fs.writeFile file, json, 'utf8', done
-    else done fs.writeFileSync file, json, 'utf8'
-  catch e
-    if cb? then cb e else e
+      if cb? then cb null, id else id
+  if cb? then saveObjectToFile data, file, done
+  else done saveObjectToFile data, file
 
 get = (id, cb) ->
   o = clone @_cache[id]


  all: (cb=->) ->
    if @_single
      fs.readFile @_getFileName(), "utf8", (err, content) ->
        return cb err if err?
        try
          cb null, JSON.parse content
        catch e
          cb e
    else
      readIDs @_dir, (err, ids) =>
        return cb err if err?

  if @_single
    backup = @_cache[id]
    delete @_cache[id]
    done = (err)=>
      if err?
        @_cache[id] = backup
        if cb? then cb err else err
      else cb? null
    if cb?
      saveObjectToFile @_cache, file, done
    else
      err = (o = saveObjectToFile @_cache, file) instanceof Error
      done err, o
  else
    done = (err) =>
      return (if cb? cb err else err) if err?
