File size: 1,822 Bytes
7e48311
 
 
27e7d8e
 
7e48311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// // Scott Hale (Oxford Internet Institute)
// // Requires sigma.js and jquery to be loaded
// // based on parseGexf from Mathieu Jacomy @ Sciences Po M�dialab & WebAtlas


sigma.publicPrototype.parseJson = function(gzippedJsonPath, callback) {
  var sigmaInstance = this;
  
  // Use XMLHttpRequest for binary data
  var xhr = new XMLHttpRequest();
  xhr.open('GET', gzippedJsonPath, true);
  xhr.responseType = 'arraybuffer';
  
  xhr.onload = function() {
    if (xhr.status === 200) {
      try {
        // Decompress the gzipped data using pako
        var inflatedData = pako.inflate(new Uint8Array(xhr.response));
        
        // Convert binary data to string
        var jsonString = new TextDecoder('utf-8').decode(inflatedData);
        
        // Parse the JSON
        var jsonData = JSON.parse(jsonString);
        
        // Process nodes
        for (var i = 0; i < jsonData.nodes.length; i++) {
          var id = jsonData.nodes[i].id;
          sigmaInstance.addNode(id, jsonData.nodes[i]);
        }
        
        // Process edges
        for (var j = 0; j < jsonData.edges.length; j++) {
          var edgeNode = jsonData.edges[j];
          var source = edgeNode.source;
          var target = edgeNode.target;
          var label = edgeNode.label;
          var eid = edgeNode.id;
          
          sigmaInstance.addEdge(eid, source, target, edgeNode);
        }
        
        // Call the callback function if provided
        if (callback) callback.call(sigmaInstance);
      } catch (error) {
        console.error("Error processing gzipped JSON:", error);
      }
    } else {
      console.error("Error fetching gzipped JSON. Status:", xhr.status);
    }
  };
  
  xhr.onerror = function() {
    console.error("Network error while fetching gzipped JSON");
  };
  
  xhr.send();
};