-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-ref-resolver.js
More file actions
190 lines (159 loc) · 7.09 KB
/
Copy pathjson-ref-resolver.js
File metadata and controls
190 lines (159 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
angular.module('myapp.services').factory('jsonRefResolver', ['$q', function ($q) {
// The backend sends back data with references to refer to an object and thus preventing circular loops.
// This response interceptor resolves those references.
// My backend was a REST API created with ASP.NET MVC 5 with Web API 2. This was the configuration for
// dealing with circular references.
// var jsonFormatter = config.Formatters.JsonFormatter;
// jsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
// Note: Douglas Crockford has a similar script at https://github.com/douglascrockford/JSON-js/blob/master/cycle.js,
// but this assumes that the references contain JSON paths as references instead of IDs.
// This person ran into a similar situation: http://stackoverflow.com/questions/15118667/how-to-parse-json-string-contains-circular-reference-in-javascript
// One more note: we can't actually fully restore the referenced objects. angular.copy() and the
// filter filter don't handle circular references properly. If object A contains an object B, where
// object B refers back to object A, angular.copy() doesn't know how to handle this and the filter filter
// just goes on and on searching. Thus, it can't be parsed. Unless, of course, an object C enters the
// picture and has a reference to object B. In that scenario, B should contain a reference to A.
return {
response: jsonRefParse
};
function jsonRefParse(response) {
var idObjects = [];
if (response.headers()['content-type'] === 'application/json; charset=utf-8') {
// When accounting for circular referencing, a list of $ids is kept when traversing
// the hierarchy of objects. Objects lower in the hierarchy should not have a live object
// of some thing higher up in the hierarchy. Thus, it's an ignore list.
// But, it turns out that a third object sometimes comes into play. When object B is
// in the same hierarchy as A, object B should not contain A's. But object C, outside
// the hierarchy of object A, sometimes references B. In that scenario, A should
// appear in that hierarchy. Thus, traversing the tree again looking for ignored
// references the first time makes sure we replace more.
// Note that we don't traverse the tree fully, only resolved objects are reevaluated with
// the ignore list for that part in the tree.
// Find objects with an identifier ($id)
findIdObjects(response.data);
// Go through all data, find references and resolve them recursively
resolve(response.data);
// Cleanup
removeAllReferences(response.data);
}
return response;
// This is the most important function in this file: it resolves all references in the data
function resolve(data) {
// Find object with references to other objects
var refObjects = findRefs(data);
// Resolve all objects that hold references to unique objects
for (var i = refObjects.length - 1; i >= 0; i--) {
resolveRefObject(refObjects[i]);
}
// After resolving all the current refobjects, call this function again for each of those objects
for (var i = refObjects.length - 1; i >= 0; i--) {
resolve(refObjects[i]);
}
}
function findIdObjects(data) {
// Is data an array? The top-level data is an array. Go through the array and parse every object in the array.
if (angular.isArray(data)) {
for (var i = 0; i < data.length; i++) {
findIdObjects(data[i]);
}
return;
}
// Is data an object?
if (angular.isObject(data)) {
if ('$id' in data) {
idObjects.push(data);
}
// Look for nested objects to go through recursively
for (var key in data) {
// For an array or object, recursively call this function again, until the point that we can parse primitive values
// We're only checking for isObject() here, since isObject() gives true for an array as well.
if (angular.isObject(data[key])) {
findIdObjects(data[key]);
}
}
}
}
function findRefs(data) {
data.$refIdsToIgnore = data.$refIdsToIgnore || [];
var refObjects = [];
// Is data an array? The top-level data is an array. Go through the array and parse every object in the array.
if (angular.isArray(data)) {
for (var i = 0; i < data.length; i++) {
data[i].$refIdsToIgnore = angular.copy(data.$refIdsToIgnore);
refObjects = refObjects.concat(findRefs(data[i]));
}
return refObjects;
}
// Is data an object?
if (angular.isObject(data)) {
if ('$ref' in data && data.$refIdsToIgnore.indexOf(data.$ref) === -1) {
refObjects.push(data);
}
if ('$id' in data) {
data.$refIdsToIgnore.push(data.$id);
}
// Look for nested objects to go through recursively
for (var key in data) {
// For an array or object, recursively call this function again, until the point that we can parse primitive values
// We're only checking for isObject() here, since isObject() gives true for an array as well.
if (key !== '$refIdsToIgnore' && angular.isObject(data[key])) {
data[key].$refIdsToIgnore = angular.copy(data.$refIdsToIgnore);
refObjects = refObjects.concat(findRefs(data[key]));
}
}
}
return refObjects;
}
function resolveRefObject(refObject) {
for (var j = 0; j < idObjects.length; j++) {
var idObject = idObjects[j];
if (refObject.$ref === idObject.$id) {
// Directly assigning refObject[i] will not cause the root object to change, it just adds a completely new item to the array
// By copying all properties from the unique object to the referencing object, thus not losing the connection to the root.
for (var key in idObject) {
if (key !== '$refIdsToIgnore') {
if (angular.isObject(idObject[key])) {
refObject[key] = angular.copy(idObject[key]);
}
else {
refObject[key] = idObject[key];
}
}
}
// Append the ignore list with the $id of the object we just resolved to prevent circular references
refObject.$refIdsToIgnore.push(idObject.$id);
// Cleanup: the object has been resolved, $ref needs to be removed to avoid more parsing
delete refObject.$ref;
break;
}
}
}
// Cleanup functions (just one at the moment, actually)
function removeAllReferences(data) {
delete data.$id;
delete data.$ref;
delete data.$refIdsToIgnore;
// Is data an array? The top-level data is an array. Go through the array and parse every object in the array.
if (angular.isArray(data)) {
for (var i = 0; i < data.length; i++) {
removeAllReferences(data[i]);
}
return;
}
// Is data an object?
if (angular.isObject(data)) {
delete data.$id;
delete data.$ref;
delete data.$refIdsToIgnore;
// Inspect individual properties and look for $ref
for (var key in data) {
// For an array or object, recursively call this function again, until the point that we can parse primitive values
// We're only checking for isObject() here, since isObject() gives true for an array as well.
if (angular.isObject(data[key])) {
removeAllReferences(data[key]);
}
}
}
}
};
}]);