Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

JSON files support added.

  • Loading branch information...
commit b89260c3802bc5738bac7e45ed4e9910da7fe214 1 parent a01cc38
@bfontaine authored
Showing with 129 additions and 0 deletions.
  1. +67 −0 lib/graphs/json.rb
  2. +62 −0 tests/json_tests.rb
View
67 lib/graphs/json.rb
@@ -0,0 +1,67 @@
+#! /usr/bin/ruby1.9.1
+# -*- coding: UTF-8 -*-
+
+require 'json'
+require_relative '../graph'
+
+class Graph
+ # Returns a JSON version of the current graph
+ # @param opts [Hash] A customizable set of options
+ # @see JSONGraph::unparse
+ def to_json(opts=nil)
+ JSONGraph::unparse(self, opts)
+ end
+
+ # Write the current graph into a JSON file. This method is used internally,
+ # use Graph#write instead.
+ # @param filename [String] a valid filename
+ # @see JSON::unparse
+ def write_json(filename, opts=nil)
+ json = JSONGraph::unparse(self, opts)
+ f = File.open(filename, 'w')
+ f.write(json)
+ f.close
+ end
+end
+
+# JSON-related functions
+module JSONGraph
+
+ # Loads a JSON file and return a new Graph object
+ # @param filename [String] a valid filename
+ # @see JSONGraph::parse
+ def self.load(filename)
+ self.parse(File.read(filename))
+ end
+
+ # Parse some JSON text and return a new Graph object
+ # @param content [String] a valid GDF String
+ # @see JSONGraph::load
+ # @see JSONGraph::unparse
+ def self.parse(content)
+
+ if (content.nil? || content.length == 0)
+ return Graph.new([],[])
+ end
+
+ content = JSON.parse content
+
+ nodes = content['nodes']
+ edges = content['edges']
+
+ Graph.new(nodes, edges)
+ end
+
+ # Return a JSON String which describe the given Graph
+ # @param graph [Graph]
+ # @param opts [Hash] A customizable set of options
+ # @see Graph#write
+ def self.unparse(graph, opts=nil)
+
+ nodes = graph.nodes.map { |n| n.to_hash }
+ edges = graph.edges.map { |e| e.to_hash }
+
+ JSON.dump({ 'nodes' => nodes, 'edges' => edges })
+ end
+
+end
View
62 tests/json_tests.rb
@@ -0,0 +1,62 @@
+#! /usr/bin/ruby1.9.1
+# -*- coding: UTF-8 -*-
+
+require_relative '../lib/graphs/json'
+
+module JSONUtils
+ def self.get_sample_graph
+ @@json
+ end
+
+ @@json = <<EOJSON
+{
+ "nodes" : [
+ { "label": "foo" }, { "label": "bar" }
+ ],
+ "edges" : [
+ { "node1": "bar", "node2": "foo" }
+ ]
+}
+EOJSON
+
+ @@json.gsub!(/\s+/, '')
+end
+
+class JSON_Graph_test < Test::Unit::TestCase
+
+ # == Graph#to_json == #
+
+ def test_empty_graph_to_json
+ g = Graph.new
+ empty_json = '{"nodes":[],"edges":[]}'
+ assert_equal(empty_json, g.to_json)
+ end
+
+ def test_sample_graph_to_json
+ json = JSONUtils::get_sample_graph
+ g = JSONGraph::parse(json)
+ assert_equal(json, g.to_json)
+ end
+
+ # == Graph#write('….json') == #
+
+ def test_empty_graph_write_json
+ g = Graph.new
+
+ f = Tempfile.new([ 'foo', '.json' ])
+ f.close
+
+ g.write(f.path)
+ g2 = JSONGraph.load(f.path)
+
+ assert_equal(g, g2)
+ f.unlink
+ end
+
+ def setup
+ if File.exists? '/tmp/_graph_test.json'
+ File.delete '/tmp/_graph_test.json'
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.