@@ -10,11 +10,13 @@ nextId = 1
10
10
# Older browser compatibility
11
11
isArray = Array .isArray || (obj ) -> Object .prototype .toString .call (obj) == ' [object Array]'
12
12
13
+ JSOG_OBJECT_ID = ' __jsogObjectId'
14
+
13
15
#
14
16
# Take a JSON structure with cycles and turn it into a JSOG-encoded structure. Adds
15
17
# @id to every object and replaces duplicate references with @refs.
16
18
#
17
- # Note that this modifies the original objects adding __object_id fields and leaves
19
+ # Note that this modifies the original objects adding __jsogObjectId fields and leaves
18
20
# them there. There does not appear to be another way to define object identity in JS.
19
21
#
20
22
JSOG .encode = (original ) ->
@@ -24,10 +26,10 @@ JSOG.encode = (original) ->
24
26
25
27
# Get (and if necessary, set) an object id. This ends up being left behind in the original object.
26
28
idOf = (obj ) ->
27
- if ! obj . __jsogObjectId
28
- obj . __jsogObjectId = " #{ nextId++ } "
29
+ if ! obj[JSOG_OBJECT_ID]
30
+ obj[JSOG_OBJECT_ID] = " #{ nextId++ } "
29
31
30
- return obj . __jsogObjectId
32
+ return obj[JSOG_OBJECT_ID]
31
33
32
34
doEncode = (original ) ->
33
35
encodeObject = (original ) ->
@@ -37,7 +39,7 @@ JSOG.encode = (original) ->
37
39
38
40
result = sofar[id] = { ' @id' : id }
39
41
for key, value of original
40
- if key != ' __jsogObjectId '
42
+ if key != JSOG_OBJECT_ID
41
43
result[key] = doEncode (value)
42
44
43
45
return result
0 commit comments