/*
* (C) Copyright 2019-2023, by Dimitrios Michail and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* See the CONTRIBUTORS.md file distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the
* GNU Lesser General Public License v2.1 or later
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
*
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
*/
package org.jgrapht.nio.json;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import org.jgrapht.Graph;
import org.jgrapht.GraphType;
import org.jgrapht.alg.util.Pair;
import org.jgrapht.alg.util.Triple;
import org.jgrapht.nio.Attribute;
import org.jgrapht.nio.BaseEventDrivenImporter;
import org.jgrapht.nio.DefaultAttribute;
import org.jgrapht.nio.GraphImporter;
import org.jgrapht.nio.ImportException;
/**
* 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 {@link #setVertexFactory(Function)}. The factory method is responsible to create a new
* graph vertex given the vertex identifier read from file. Additionally this importer also supports
* creating vertices with {@link #setVertexWithAttributesFactory(BiFunction)}. This factory method
* is responsible for creating a new graph vertex given the vertex identifier read from file
* together with all available attributes of the vertex at the location of the file where the vertex
* is first defined.
*
*
* The default behavior of the importer is to use the graph edge supplier in order to create edges.
* The user can also bypass edge creation by providing a custom edge factory method using
* {@link #setEdgeWithAttributesFactory(Function)}. The factory method is responsible to create a
* new graph edge given all available attributes of the edge at the location of the file where the
* edge is first defined.
*
* @param the vertex type
* @param the edge type
*
* @author Dimitrios Michail
*/
public class JSONImporter
extends BaseEventDrivenImporter
implements GraphImporter
{
/**
* Default key used for vertex ID.
*/
public static final String DEFAULT_VERTEX_ID_KEY = "ID";
/**
* Default name for the vertices collection
*/
public static final String DEFAULT_VERTICES_COLLECTION_NAME = "nodes";
/**
* Default name for the edges collection
*/
public static final String DEFAULT_EDGES_COLLECTION_NAME = "edges";
private Function vertexFactory;
private BiFunction, V> vertexWithAttributesFactory;
private Function