- java.lang.Object
-
- org.jgrapht.nio.BaseEventDrivenImporter<V,E>
-
- org.jgrapht.nio.json.JSONImporter<V,E>
-
- Type Parameters:
V
- the vertex typeE
- the edge type
- All Implemented Interfaces:
GraphImporter<V,E>
public class JSONImporter<V,E> extends BaseEventDrivenImporter<V,E> implements GraphImporter<V,E>
Imports a graph from a JSON file. Below is a small example of a graph in JSON format.{ "nodes": [ { "id": "1" }, { "id": "2", "label": "Node 2 label" }, { "id": "3" } ], "edges": [ { "source": "1", "target": "2", "weight": 2.0, "label": "Edge between 1 and 2" }, { "source": "2", "target": "3", "weight": 3.0, "label": "Edge between 2 and 3" } ] }
In case the graph is weighted then the importer also reads edge weights. Otherwise edge weights are ignored. The importer also supports reading additional string attributes such as label or custom user attributes.
The parser completely ignores elements from the input that are not related to vertices or edges of the graph. Moreover, complicated nested structures which are inside vertices or edges are simply returned as a whole. For example, in the following graph
{ "nodes": [ { "id": "1" }, { "id": "2" } ], "edges": [ { "source": "1", "target": "2", "points": { "x": 1.0, "y": 2.0 } } ] }
the points attribute of the edge is returned as a string containing {"x":1.0,"y":2.0}. The same is done for arrays or any other arbitrary nested structure.The graph vertices and edges are build using the corresponding graph suppliers. The id of the vertices in the original dot file are reported as a vertex attribute named "ID". Thus, in case vertices in the dot file also contain an "ID" attribute, such an attribute will be reported multiple times.
The default behavior of the importer is to use the graph vertex supplier in order to create vertices. The user can also bypass vertex creation by providing a custom vertex factory method using
setVertexFactory(Function)
. The factory method is responsible to create a new graph vertex given the vertex identifier read from file.- Author:
- Dimitrios Michail
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_VERTEX_ID_KEY
Default key used for vertex ID.
-
Constructor Summary
Constructors Constructor Description JSONImporter()
Construct a new importer
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.function.Function<java.lang.String,V>
getVertexFactory()
Get the user custom vertex factory.void
importGraph(Graph<V,E> graph, java.io.Reader input)
Import a graph.void
setVertexFactory(java.util.function.Function<java.lang.String,V> vertexFactory)
Set the user custom vertex factory.-
Methods inherited from class org.jgrapht.nio.BaseEventDrivenImporter
addEdgeAttributeConsumer, addEdgeConsumer, addEdgeCountConsumer, addGraphAttributeConsumer, addImportEventConsumer, addVertexAttributeConsumer, addVertexConsumer, addVertexCountConsumer, notifyEdge, notifyEdgeAttribute, notifyEdgeCount, notifyGraphAttribute, notifyImportEvent, notifyVertex, notifyVertexAttribute, notifyVertexCount, removeEdgeAttributeConsumer, removeEdgeConsumer, removeEdgeCountConsumer, removeGraphAttributeConsumer, removeImportEventConsumer, removeVertexAttributeConsumer, removeVertexConsumer, removeVertexCountConsumer
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jgrapht.nio.GraphImporter
importGraph, importGraph
-
-
-
-
Field Detail
-
DEFAULT_VERTEX_ID_KEY
public static final java.lang.String DEFAULT_VERTEX_ID_KEY
Default key used for vertex ID.- See Also:
- Constant Field Values
-
-
Method Detail
-
importGraph
public void importGraph(Graph<V,E> graph, java.io.Reader input)
Import a graph.The provided graph must be able to support the features of the graph that is read. For example if the file contains self-loops then the graph provided must also support self-loops. The same for multiple edges.
If the provided graph is a weighted graph, the importer also reads edge weights. Otherwise edge weights are ignored.
- Specified by:
importGraph
in interfaceGraphImporter<V,E>
- Parameters:
graph
- the output graphinput
- the input reader- Throws:
ImportException
- in case an error occurs, such as I/O or parse error
-
getVertexFactory
public java.util.function.Function<java.lang.String,V> getVertexFactory()
Get the user custom vertex factory. This is null by default and the graph supplier is used instead.- Returns:
- the user custom vertex factory
-
setVertexFactory
public void setVertexFactory(java.util.function.Function<java.lang.String,V> vertexFactory)
Set the user custom vertex factory. The default behavior is being null in which case the graph vertex supplier is used. If supplied the vertex factory is called every time a new vertex is encountered in the file. The method is called with parameter the vertex identifier from the file and should return the actual graph vertex to add to the graph.- Parameters:
vertexFactory
- a vertex factory
-
-