- java.lang.Object
-
- org.jgrapht.nio.BaseExporter<V,E>
-
- org.jgrapht.nio.json.JSONExporter<V,E>
-
- Type Parameters:
V
- the graph vertex typeE
- the graph edge type
- All Implemented Interfaces:
GraphExporter<V,E>
public class JSONExporter<V,E> extends BaseExporter<V,E> implements GraphExporter<V,E>
Exports a graph using JSON.The output is one object which contains:
- A member named
nodes
whose value is an array of nodes. - A member named
edges
whose value is an array of edges. - Two members named
creator
andversion
for metadata.
Each node contains an identifier and possibly other attributes. Similarly each edge contains the source and target vertices, a possible identifier and possible other attributes. All these can be adjusted using the setters. The default constructor constructs integer identifiers using an
IntegerIdProvider
for both vertices and edges and does not output any custom attributes.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CREATOR
static java.lang.String
DEFAULT_EDGES_COLLECTION_NAME
Default name for the edges collectionstatic java.lang.String
DEFAULT_VERTICES_COLLECTION_NAME
Default name for the vertices collectionprivate java.lang.String
edgesCollectionName
private static java.lang.String
VERSION
private java.lang.String
verticesCollectionName
-
Fields inherited from class org.jgrapht.nio.BaseExporter
edgeAttributeProvider, edgeIdProvider, graphAttributeProvider, graphIdProvider, vertexAttributeProvider, vertexIdProvider
-
-
Constructor Summary
Constructors Constructor Description JSONExporter()
Creates a new exporter with integers for the vertex identifiers.JSONExporter(java.util.function.Function<V,java.lang.String> vertexIdProvider)
Creates a new exporter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
exportEdge(java.io.PrintWriter out, Graph<V,E> g, E e)
private void
exportEdgeAttributes(java.io.PrintWriter out, Graph<V,E> g, E e)
void
exportGraph(Graph<V,E> g, java.io.Writer writer)
Export a graph using the givenWriter
.private void
exportVertex(java.io.PrintWriter out, Graph<V,E> g, V v)
private void
exportVertexAttributes(java.io.PrintWriter out, Graph<V,E> g, V v)
java.lang.String
getEdgesCollectionName()
Get the name used for the edges collection in the file.java.lang.String
getVerticesCollectionName()
Get the name used for the vertices collection in the file.private void
outputValue(java.io.PrintWriter out, Attribute value)
private java.lang.String
quoted(java.lang.String s)
void
setEdgesCollectionName(java.lang.String edgesCollectionName)
Set the name used for the edges collection in the file.void
setVerticesCollectionName(java.lang.String verticesCollectionName)
Set the name used for the vertices collection in the file.-
Methods inherited from class org.jgrapht.nio.BaseExporter
getEdgeAttribute, getEdgeAttributeProvider, getEdgeAttributes, getEdgeId, getEdgeIdProvider, getGraphAttribute, getGraphAttributeProvider, getGraphId, getGraphIdProvider, getVertexAttribute, getVertexAttributeProvider, getVertexAttributes, getVertexId, getVertexIdProvider, setEdgeAttributeProvider, setEdgeIdProvider, setGraphAttributeProvider, setGraphIdProvider, setVertexAttributeProvider, setVertexIdProvider
-
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.GraphExporter
exportGraph, exportGraph
-
-
-
-
Field Detail
-
DEFAULT_VERTICES_COLLECTION_NAME
public static final java.lang.String DEFAULT_VERTICES_COLLECTION_NAME
Default name for the vertices collection- See Also:
- Constant Field Values
-
DEFAULT_EDGES_COLLECTION_NAME
public static final java.lang.String DEFAULT_EDGES_COLLECTION_NAME
Default name for the edges collection- See Also:
- Constant Field Values
-
CREATOR
private static final java.lang.String CREATOR
- See Also:
- Constant Field Values
-
VERSION
private static final java.lang.String VERSION
- See Also:
- Constant Field Values
-
verticesCollectionName
private java.lang.String verticesCollectionName
-
edgesCollectionName
private java.lang.String edgesCollectionName
-
-
Constructor Detail
-
JSONExporter
public JSONExporter()
Creates a new exporter with integers for the vertex identifiers.
-
JSONExporter
public JSONExporter(java.util.function.Function<V,java.lang.String> vertexIdProvider)
Creates a new exporter.- Parameters:
vertexIdProvider
- for generating vertex identifiers. Must not be null.
-
-
Method Detail
-
getVerticesCollectionName
public java.lang.String getVerticesCollectionName()
Get the name used for the vertices collection in the file.- Returns:
- the name used for the vertices collection in the file.
-
setVerticesCollectionName
public void setVerticesCollectionName(java.lang.String verticesCollectionName)
Set the name used for the vertices collection in the file.- Parameters:
verticesCollectionName
- the name
-
getEdgesCollectionName
public java.lang.String getEdgesCollectionName()
Get the name used for the edges collection in the file.- Returns:
- the name used for the edges collection in the file.
-
setEdgesCollectionName
public void setEdgesCollectionName(java.lang.String edgesCollectionName)
Set the name used for the edges collection in the file.- Parameters:
edgesCollectionName
- the name
-
exportGraph
public void exportGraph(Graph<V,E> g, java.io.Writer writer)
Description copied from interface:GraphExporter
Export a graph using the givenWriter
.It is the callers responsibility to ensure the
Writer
is closed after this method returned.- Specified by:
exportGraph
in interfaceGraphExporter<V,E>
- Parameters:
g
- the graph to exportwriter
- the output writer
-
exportVertexAttributes
private void exportVertexAttributes(java.io.PrintWriter out, Graph<V,E> g, V v)
-
exportEdgeAttributes
private void exportEdgeAttributes(java.io.PrintWriter out, Graph<V,E> g, E e)
-
outputValue
private void outputValue(java.io.PrintWriter out, Attribute value)
-
quoted
private java.lang.String quoted(java.lang.String s)
-
-