Skip to content

Commit cb627dd

Browse files
committed
Built graph for JSON2GRAPH, need to convert the graph content to RDF formatted file
1 parent 7fbc9eb commit cb627dd

5 files changed

Lines changed: 239 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "json2rdf"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
petgraph = "0.6.5"
8+
rio_api = "0.8.4"
9+
rio_turtle = "0.8.4"
10+
serde = "1.0.203"
11+
serde_json = "1.0.117"

src/airplane.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"aircraft": {
3+
"id": "A12345",
4+
"model": "Boeing 747",
5+
"manufacturer": "Boeing",
6+
"capacity": {
7+
"seats": 416,
8+
"cargo_volume": 150.4
9+
}
10+
}
11+
}

src/main.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use petgraph::graph::{Graph, NodeIndex};
2+
use petgraph::visit::EdgeRef;
3+
use rio_api::formatter::TriplesFormatter;
4+
use rio_api::model::{NamedNode, Triple};
5+
use rio_turtle::NTriplesFormatter;
6+
use serde_json::Value;
7+
use std::fs::File;
8+
use std::io::BufReader;
9+
10+
fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Option<NodeIndex>) {
11+
match value {
12+
Value::Object(map) => {
13+
for (key, val) in map {
14+
let node = graph.add_node(key.clone());
15+
if let Some(parent_index) = parent {
16+
graph.add_edge(parent_index, node, "".to_string());
17+
}
18+
json_to_graph(val, graph, Some(node));
19+
}
20+
}
21+
Value::Array(arr) => {
22+
for val in arr {
23+
json_to_graph(val, graph, parent);
24+
}
25+
}
26+
_ => {
27+
let node = graph.add_node(value.to_string());
28+
if let Some(parent_index) = parent {
29+
graph.add_edge(parent_index, node, "".to_string());
30+
}
31+
}
32+
}
33+
}
34+
35+
fn graph_to_ttl(graph: &mut Graph<String, String>, file_path: &str) {
36+
let mut file = File::create(file_path);
37+
38+
let mut formatter = NTriplesFormatter::new(Vec::default());
39+
40+
for edge in graph.edge_references() {}
41+
}
42+
43+
fn main() -> serde_json::Result<()> {
44+
let file_path = "/home/bharath/documents/github/json2rdf/src/airplane.json";
45+
let file = File::open(file_path).unwrap();
46+
let reader = BufReader::new(file);
47+
let json_value: Value = serde_json::from_reader(reader).unwrap();
48+
49+
let mut graph = Graph::<String, String>::new();
50+
51+
json_to_graph(&json_value, &mut graph, None);
52+
53+
Ok(())
54+
}

0 commit comments

Comments
 (0)