|
1 | | -// use oxrdf::{LiteralRef, NamedNodeRef, TermRef, TripleRef}; |
2 | | -// use petgraph::graph::{Graph, NodeIndex}; |
3 | | -// use petgraph::visit::EdgeRef; |
4 | | -// // use rio_api::formatter::TriplesFormatter; |
5 | | -// // use rio_api::model::{NamedNode, Triple}; |
6 | | -// // use rio_turtle::NTriplesFormatter; |
7 | | -// use serde_json::Value; |
8 | | -// use std::fmt::format; |
9 | | -// use std::fs::{read, File}; |
10 | | -// use std::io::{BufReader, Write}; |
11 | | - |
12 | | -// fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Option<NodeIndex>) { |
13 | | -// match value { |
14 | | -// Value::Object(map) => { |
15 | | -// for (key, val) in map { |
16 | | -// let node = graph.add_node(key.clone()); |
17 | | -// if let Some(parent_index) = parent { |
18 | | -// graph.add_edge(parent_index, node, "".to_string()); |
19 | | -// } |
20 | | -// // let node = graph.add_node("".to_string()); |
21 | | -// // if let Some(parent_index) = parent { |
22 | | -// // graph.add_edge(parent_index, node, key.to_string()); |
23 | | -// // } |
24 | | -// json_to_graph(val, graph, Some(node)); |
25 | | -// } |
26 | | -// } |
27 | | -// Value::Array(arr) => { |
28 | | -// for val in arr { |
29 | | -// json_to_graph(val, graph, parent); |
30 | | -// } |
31 | | -// } |
32 | | -// _ => { |
33 | | -// let node = graph.add_node(value.to_string()); |
34 | | -// if let Some(parent_index) = parent { |
35 | | -// graph.add_edge(parent_index, node, "".to_string()); |
36 | | -// // let node = graph.add_node("".to_string()); |
37 | | -// // if let Some(parent_index) = parent { |
38 | | -// // graph.add_edge(parent_index, node, value.to_string()); |
39 | | -// } |
40 | | -// } |
41 | | -// } |
42 | | -// } |
43 | | - |
44 | | -// fn graph_to_ttl(graph: &mut Graph<String, String>, file_path: &str) { |
45 | | -// let mut file = File::create(file_path); |
| 1 | +use oxrdf::{LiteralRef, NamedNodeRef, TermRef, TripleRef}; |
| 2 | +use petgraph::graph::{Graph, NodeIndex}; |
| 3 | +use petgraph::visit::EdgeRef; |
| 4 | +// use rio_api::formatter::TriplesFormatter; |
| 5 | +// use rio_api::model::{NamedNode, Triple}; |
| 6 | +// use rio_turtle::NTriplesFormatter; |
| 7 | +use serde_json::Value; |
| 8 | +use std::fmt::format; |
| 9 | +use std::fs::{read, File}; |
| 10 | +use std::io::{BufReader, Write}; |
| 11 | + |
| 12 | +fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Option<NodeIndex>) { |
| 13 | + match value { |
| 14 | + Value::Object(map) => { |
| 15 | + for (key, val) in map { |
| 16 | + let node = graph.add_node(key.clone()); |
| 17 | + if let Some(parent_index) = parent { |
| 18 | + graph.add_edge(parent_index, node, "".to_string()); |
| 19 | + } |
| 20 | + // let node = graph.add_node("".to_string()); |
| 21 | + // if let Some(parent_index) = parent { |
| 22 | + // graph.add_edge(parent_index, node, key.to_string()); |
| 23 | + // } |
| 24 | + json_to_graph(val, graph, Some(node)); |
| 25 | + } |
| 26 | + } |
| 27 | + Value::Array(arr) => { |
| 28 | + for val in arr { |
| 29 | + json_to_graph(val, graph, parent); |
| 30 | + } |
| 31 | + } |
| 32 | + _ => { |
| 33 | + let node = graph.add_node(value.to_string()); |
| 34 | + if let Some(parent_index) = parent { |
| 35 | + graph.add_edge(parent_index, node, "".to_string()); |
| 36 | + // let node = graph.add_node("".to_string()); |
| 37 | + // if let Some(parent_index) = parent { |
| 38 | + // graph.add_edge(parent_index, node, value.to_string()); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
46 | 43 |
|
47 | | -// let mut triples_graph = oxrdf::Graph::default(); |
| 44 | +fn graph_to_ttl(graph: &mut Graph<String, String>, file_path: &str) { |
| 45 | + let mut file = File::create(file_path); |
48 | 46 |
|
49 | | -// for edge in graph.edge_references() { |
50 | | -// let val = format!("http://www.decisym.ai/data#{}", &graph[edge.source()]); |
| 47 | + let mut triples_graph = oxrdf::Graph::default(); |
51 | 48 |
|
52 | | -// let subject_triple = NamedNodeRef::new(val.as_str()).unwrap(); |
| 49 | + for edge in graph.edge_references() { |
| 50 | + let val = format!("http://www.decisym.ai/data#{}", &graph[edge.source()]); |
53 | 51 |
|
54 | | -// let predicate_triple = NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); |
| 52 | + let subject_triple = NamedNodeRef::new(val.as_str()).unwrap(); |
55 | 53 |
|
56 | | -// let object_triples = TermRef::Literal(LiteralRef::new_simple_literal( |
57 | | -// graph[edge.target()].as_str(), |
58 | | -// )); |
| 54 | + let predicate_triple = NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); |
59 | 55 |
|
60 | | -// let rdf_triple = TripleRef::new(subject_triple, predicate_triple.unwrap(), object_triples); |
| 56 | + let object_triples = TermRef::Literal(LiteralRef::new_simple_literal( |
| 57 | + graph[edge.target()].as_str(), |
| 58 | + )); |
61 | 59 |
|
62 | | -// triples_graph.insert(rdf_triple); |
63 | | -// } |
| 60 | + let rdf_triple = TripleRef::new(subject_triple, predicate_triple.unwrap(), object_triples); |
64 | 61 |
|
65 | | -// writeln!(file.unwrap(), "{}", triples_graph.to_string()).unwrap(); |
66 | | -// } |
| 62 | + triples_graph.insert(rdf_triple); |
| 63 | + } |
67 | 64 |
|
68 | | -use std::{any::Any, collections::HashMap}; |
| 65 | + writeln!(file.unwrap(), "{}", triples_graph.to_string()).unwrap(); |
| 66 | +} |
69 | 67 |
|
70 | 68 | fn main() { |
71 | | - //-> serde_json::Result<()> |
72 | | - // let file_path = "/home/bharath/documents/github/json2rdf/src/airplane.json"; |
73 | | - // let file = File::open(file_path).unwrap(); |
74 | | - // let reader = BufReader::new(file); |
75 | | - // let json_value: Value = serde_json::from_reader(reader).unwrap(); |
76 | | - |
77 | | - // let mut graph = Graph::<String, String>::new(); |
78 | | - |
79 | | - // json_to_graph(&json_value, &mut graph, None); |
80 | | - |
81 | | - // // for edge in graph.edge_references() { |
82 | | - // // println!("{:?}", graph[edge.source()]); |
83 | | - // // println!("{:?}", graph[edge.target()]); |
84 | | - |
85 | | - // // println!("------------") |
86 | | - // // //println!("{:?}", edge.id()); |
87 | | - // // } |
88 | | - |
89 | | - // graph_to_ttl( |
90 | | - // &mut graph, |
91 | | - // "/home/bharath/documents/github/json2rdf/outputfile.ttl", |
92 | | - // ); |
93 | | - // ------------------------------------------------------- |
94 | | - // new json2rdf code |
95 | | - use struson::reader::*; |
96 | | - |
97 | | - let json = r#"{"a": ["1", "true"]}"#; |
98 | | - let mut json_reader = JsonStreamReader::new(json.as_bytes()); |
99 | | - |
100 | | - json_reader.begin_object().unwrap(); |
101 | | - |
102 | | - // assert_eq!(json_reader.next_name().unwrap(), "a"); |
103 | | - |
104 | | - // json_reader.json_reader.begin_array().unwrap(); |
105 | | - // // assert_eq!(json_reader.next_number::<u32>().unwrap(), Ok(1)); |
106 | | - // // assert_eq!(json_reader.next_bool().unwrap(), true); |
107 | | - // json_reader.end_array().unwrap(); |
108 | | - |
109 | | - // json_reader.end_object().unwrap(); |
110 | | - // // Ensures that there is no trailing data |
111 | | - // json_reader.consume_trailing_whitespace().unwrap(); |
112 | | - |
113 | | - let mut subject_stack: Vec<Box<dyn Any>> = vec![]; |
114 | | - let mut array_properties: HashMap<String, Box<dyn Any>> = HashMap::new(); |
115 | | - |
116 | | - while json_reader.has_next().unwrap() { |
117 | | - // read current val |
118 | | - let val = json_reader.current_position(true); |
119 | | - |
120 | | - // match val { |
121 | | - // // start of object {blanknode: subject is object val: } |
122 | | - // // start of array |
123 | | - // // end of array |
124 | | - // // string |
125 | | - // // boolean |
126 | | - // // null |
127 | | - // // whitespace |
128 | | - // } |
129 | | - } |
| 69 | + // -> serde_json::Result<()> |
| 70 | + let file_path = "/home/bharath/documents/github/json2rdf/src/airplane.json"; |
| 71 | + let file = File::open(file_path).unwrap(); |
| 72 | + let reader = BufReader::new(file); |
| 73 | + let json_value: Value = serde_json::from_reader(reader).unwrap(); |
| 74 | + |
| 75 | + let mut graph = Graph::<String, String>::new(); |
| 76 | + |
| 77 | + json_to_graph(&json_value, &mut graph, None); |
| 78 | + |
| 79 | + // for edge in graph.edge_references() { |
| 80 | + // println!("{:?}", graph[edge.source()]); |
| 81 | + // println!("{:?}", graph[edge.target()]); |
| 82 | + |
| 83 | + // println!("------------") |
| 84 | + // //println!("{:?}", edge.id()); |
| 85 | + // } |
| 86 | + |
| 87 | + graph_to_ttl( |
| 88 | + &mut graph, |
| 89 | + "/home/bharath/documents/github/json2rdf/outputfile.ttl", |
| 90 | + ); |
130 | 91 |
|
131 | 92 | // Ok(()) |
132 | 93 | } |
| 94 | + |
| 95 | +// // vibefpk9348o4cawzkgiuotfrkgcvxgqphert24v8d9t9 |
| 96 | + |
| 97 | +// // ------------------------------------------------------- |
| 98 | +// // new json2rdf code |
| 99 | +// use struson::reader::*; |
| 100 | + |
| 101 | +// let json = r#"{"a": ["1", "true"]}"#; |
| 102 | +// let mut json_reader = JsonStreamReader::new(json.as_bytes()); |
| 103 | + |
| 104 | +// json_reader.begin_object().unwrap(); |
| 105 | + |
| 106 | +// // let mut subject_stack: Vec<Box<dyn Any>> = vec![]; |
| 107 | +// // let mut array_properties: HashMap<String, Box<dyn Any>> = HashMap::new(); |
| 108 | + |
| 109 | +// while json_reader.has_next().expect("Filed") { |
| 110 | +// // match json_reader.peek().unwrap() { |
| 111 | +// // //ValueType::Array => todo!(), |
| 112 | +// // ValueType::Boolean => println!("A boolean: {}", json_reader.next_bool().unwrap()), |
| 113 | +// // ValueType::Object => todo!(), |
| 114 | +// // ValueType::String => println!("A string: {}", json_reader.next_str().unwrap()), |
| 115 | +// // ValueType::Number => todo!(), |
| 116 | +// // ValueType::Null => todo!(), |
| 117 | +// // _ => panic!("Unexpected type"), |
| 118 | +// json_reader.skip_value(); |
| 119 | +// println!("hi"); |
| 120 | +// //} |
| 121 | +// } |
| 122 | +// json_reader.end_object().unwrap(); |
| 123 | +// |
| 124 | +//use std::{any::Any, collections::HashMap}; |
0 commit comments