Skip to content

Commit

Permalink
Serialization (#75): add GraphDatabase::fromJSON().
Browse files Browse the repository at this point in the history
Along with a static PropertyContainer.fromJSON() to do the actual parsing.
  • Loading branch information
aseemk committed Nov 17, 2013
1 parent 5b1e446 commit 3e5f319
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/GraphDatabase._coffee
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,18 @@ module.exports = class GraphDatabase
# important: we don't save this db's URL, because it might contain a
# basic auth password!

#
# Transforms the given node or relationship object, parsed from JSON,
# to its appropriate node or relationship instance.
#
fromJSON: (obj) ->
if obj?.package?.name isnt PACKAGE.name
throw new Error "Invalid JSON object: #{JSON.stringify obj}"

{constructor} = obj
Constructor = require "./#{constructor}"
Constructor.fromJSON @, obj

### Misc/Other: ###

#
Expand Down
8 changes: 8 additions & 0 deletions lib/PropertyContainer._coffee
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,11 @@ module.exports = class PropertyContainer
json = @db._toJSON @
json._data = @_data
json

#
# Returns an instance of this property container for the given object,
# parsed from JSON.
#
@fromJSON: (db, obj) ->
{_data} = obj
new @ db, _data

0 comments on commit 3e5f319

Please sign in to comment.