Skip to content

Commit c990c92

Browse files
authored
Merge pull request #2 from DeciSym/bharathselvaraj/json2rdf-fixes
bharathselvaraj/json2rdf fixes
2 parents e635960 + 79b34ce commit c990c92

3 files changed

Lines changed: 34 additions & 51 deletions

File tree

src/lib.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
//! - Allows specifying a custom RDF namespace for generated predicates and objects.
1313
//! - Outputs the RDF data to a specified file or prints it to the console.
1414
15+
use clap::Error;
1516
use oxrdf::vocab::xsd;
1617
use oxrdf::{BlankNode, Graph, Literal, NamedNodeRef, TripleRef};
1718

1819
use serde_json::{Deserializer, Value};
1920
use std::collections::VecDeque;
20-
use std::fs::File;
21+
use std::fs::{File, OpenOptions};
2122
use std::io::{BufReader, Write};
2223

23-
pub enum GraphOrMessage {
24-
Graph(Graph),
25-
Message(String),
26-
}
27-
2824
/// Converts JSON data to RDF format.
2925
///
3026
/// This function reads JSON data from the specified file, processes it into RDF triples,
@@ -47,7 +43,7 @@ pub fn json_to_rdf(
4743
file_path: &String,
4844
namespace: &Option<String>,
4945
output_file: &Option<String>,
50-
) -> Result<GraphOrMessage, String> {
46+
) -> Result<Option<Graph>, Error> {
5147
let rdf_namespace: String = if namespace.is_some() {
5248
namespace.clone().unwrap()
5349
} else {
@@ -109,14 +105,16 @@ pub fn json_to_rdf(
109105
}
110106

111107
if let Some(output_path) = output_file {
112-
let mut file = File::create(output_path).expect("Error creating file");
113-
writeln!(file, "{}", graph).expect("Error writing to file");
114-
Ok(GraphOrMessage::Message(format!(
115-
"RDF created at: {}",
116-
output_path
117-
)))
108+
let mut file = OpenOptions::new()
109+
.create(true)
110+
.append(true)
111+
.open(output_path)
112+
.expect("Error opening file");
113+
114+
writeln!(file, "{}", graph).expect("Error writing json2rdf data to file");
115+
Ok(None)
118116
} else {
119-
Ok(GraphOrMessage::Graph(graph))
117+
return Ok(Some(graph));
120118
}
121119
}
122120

@@ -164,18 +162,21 @@ fn process_value(
164162
));
165163
}
166164
Value::Number(num) => {
167-
let literal = if let Some(int) = num.as_i64() {
168-
int.to_string()
169-
} else if let Some(float) = num.as_f64() {
170-
float.to_string()
165+
if num.as_i64().is_some() {
166+
graph.insert(TripleRef::new(
167+
subject_stack.back().unwrap(),
168+
NamedNodeRef::new(prop.as_str()).unwrap(),
169+
&Literal::new_typed_literal(num.to_string(), xsd::INT),
170+
));
171+
} else if num.as_f64().is_some() {
172+
graph.insert(TripleRef::new(
173+
subject_stack.back().unwrap(),
174+
NamedNodeRef::new(prop.as_str()).unwrap(),
175+
&Literal::new_typed_literal(num.to_string(), xsd::FLOAT),
176+
));
171177
} else {
172178
return;
173-
};
174-
graph.insert(TripleRef::new(
175-
subject_stack.back().unwrap(),
176-
NamedNodeRef::new(prop.as_str()).unwrap(),
177-
&Literal::new_typed_literal(literal, xsd::INT),
178-
));
179+
}
179180
}
180181
Value::String(s) => {
181182
graph.insert(TripleRef::new(
@@ -199,7 +200,7 @@ fn process_value(
199200

200201
for (key, val) in obj {
201202
let nested_property: Option<String> =
202-
Some(format!("{}/{}/", namespace, key));
203+
Some(format!("{}/{}", namespace, key));
203204
process_value(subject_stack, &nested_property, val, graph, namespace);
204205
}
205206
subject_stack.pop_back();

src/main.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! ```
2222
//! This will take `data.json`, apply the specified namespace, and save the RDF output in `output.nt`.
2323
use clap::{Parser, Subcommand};
24-
mod lib;
24+
use json2rdf::*;
2525

2626
/// Command-line interface for JSON2RDF Converter
2727
///
@@ -73,21 +73,10 @@ fn main() {
7373
namespace,
7474
json_files,
7575
output_file,
76-
}) => {
77-
match lib::json_to_rdf(json_files, namespace, output_file) {
78-
Ok(res) => match res {
79-
lib::GraphOrMessage::Graph(graph) => {
80-
// Handle the case where the function returns a Graph
81-
println!("Graph created with {} triples", graph.len());
82-
}
83-
lib::GraphOrMessage::Message(message) => {
84-
// Handle the case where the function returns a success message
85-
println!("{}", message);
86-
}
87-
},
88-
Err(e) => eprintln!("Error writing: {}", e),
89-
}
90-
}
76+
}) => match json_to_rdf(json_files, namespace, output_file) {
77+
Ok(_) => {}
78+
Err(e) => eprintln!("Error writing: {}", e),
79+
},
9180
None => {}
9281
}
9382
}

tests/integration_test.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ fn test_graph_triple_count() {
55
let triple_count_string = json_to_rdf(&"tests/airplane.json".to_string(), &None, &None);
66

77
match triple_count_string {
8-
Ok(res) => match res {
9-
json2rdf::GraphOrMessage::Graph(graph) => {
10-
// Handle the case where the function returns a Graph
11-
println!("Graph created with {} triples", graph.len());
12-
assert_eq!(graph.len(), 23)
13-
}
14-
json2rdf::GraphOrMessage::Message(message) => {
15-
println!("{}", message);
16-
}
17-
},
8+
Ok(res) => {
9+
assert_eq!(res.unwrap().len(), 23)
10+
}
1811
Err(e) => eprintln!("Error writing: {}", e),
1912
}
2013
}

0 commit comments

Comments
 (0)