|
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); |
46 | | - |
47 | | - let mut triples_graph = oxrdf::Graph::default(); |
48 | | - |
49 | | - for edge in graph.edge_references() { |
50 | | - let val = format!("http://www.decisym.ai/data#{}", &graph[edge.source()]); |
51 | | - |
52 | | - let subject_triple = NamedNodeRef::new(val.as_str()).unwrap(); |
53 | | - |
54 | | - let predicate_triple = NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); |
55 | | - |
56 | | - let object_triples = TermRef::Literal(LiteralRef::new_simple_literal( |
57 | | - graph[edge.target()].as_str(), |
58 | | - )); |
59 | | - |
60 | | - let rdf_triple = TripleRef::new(subject_triple, predicate_triple.unwrap(), object_triples); |
61 | | - |
62 | | - triples_graph.insert(rdf_triple); |
63 | | - } |
64 | | - |
65 | | - writeln!(file.unwrap(), "{}", triples_graph.to_string()).unwrap(); |
66 | | -} |
| 1 | +use struson::reader::*; |
67 | 2 |
|
68 | 3 | fn main() { |
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(); |
| 4 | + // let json = r#"{"a": true}"#; |
| 5 | + // let mut json_reader = JsonStreamReader::new(json.as_bytes()); |
76 | 6 |
|
77 | | - json_to_graph(&json_value, &mut graph, None); |
| 7 | + let mut json_reader = JsonStreamReader::new(r#"{"a": 1, "b": 2}"#.as_bytes()); |
| 8 | + json_reader.begin_object().unwrap(); |
78 | 9 |
|
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 | | - // } |
| 10 | + while json_reader.has_next().unwrap() { |
| 11 | + match json_reader.peek().unwrap() { |
| 12 | + ValueType::Boolean => println!("A boolean: {}", json_reader.next_bool().unwrap()), |
| 13 | + ValueType::String => println!("A string: {}", json_reader.next_str().unwrap()), |
| 14 | + _ => panic!("Unexpected type"), |
| 15 | + } |
86 | 16 |
|
87 | | - graph_to_ttl( |
88 | | - &mut graph, |
89 | | - "/home/bharath/documents/github/json2rdf/outputfile.ttl", |
90 | | - ); |
| 17 | + // json_reader.skip_name().unwrap(); |
| 18 | + // json_reader.skip_value().unwrap(); |
| 19 | + // println!("SKIPPED"); |
| 20 | + } |
91 | 21 |
|
92 | | - // Ok(()) |
| 22 | + json_reader.end_object().unwrap(); |
93 | 23 | } |
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