module JSONGraph

JSON-related functions

Public Class Methods

load(filename) click to toggle source

Loads a JSON file and return a new Graph object @param filename [String] a valid filename @return [Graph] @see JSONGraph.parse

# File lib/graphs/json.rb, line 36
def self.load(filename)
    self.parse(File.read(filename))
end
parse(content) click to toggle source

Parse some JSON text and return a new Graph object @param content [String] a valid GDF String @return [Graph] @see JSONGraph.load @see JSONGraph.unparse

# File lib/graphs/json.rb, line 45
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
unparse(graph, opts=nil) click to toggle source

Return a JSON String which describe the given Graph @param graph [Graph] @param opts [Hash] A customizable set of options @return [String] @see Graph#write

# File lib/graphs/json.rb, line 64
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