Skip to content

Commit 58a80d1

Browse files
committed
Created working JSON reader to parse code and generate RDF. Currently everything is a string, need to make it into oxrdf graph
1 parent 3d8945b commit 58a80d1

2 files changed

Lines changed: 115 additions & 112 deletions

File tree

src/airplane.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66
"capacity": {
77
"seats": 416,
88
"cargo_volume": 150.4
9-
}
9+
},
10+
"flights": [
11+
{
12+
"flight_number": "BA123",
13+
"departure": {
14+
"airport": "JFK",
15+
"time": "2024-10-29T10:00:00Z"
16+
},
17+
"arrival": {
18+
"airport": "LHR",
19+
"time": "2024-10-29T22:00:00Z"
20+
}
21+
},
22+
{
23+
"flight_number": "BA456",
24+
"departure": {
25+
"airport": "LHR",
26+
"time": "2024-10-30T12:00:00Z"
27+
},
28+
"arrival": {
29+
"airport": "JFK",
30+
"time": "2024-10-30T20:00:00Z"
31+
}
32+
}
33+
]
1034
}
11-
}
35+
}

src/main.rs

Lines changed: 89 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,103 @@
1-
use oxrdf::NamedOrBlankNode;
2-
use serde_json::Value;
1+
use oxrdf::{Graph, NamedOrBlankNode};
2+
use serde::de::value;
3+
use serde_json::{Deserializer, Value};
34
use std::collections::{HashMap, VecDeque};
45
use std::fs::File;
56
use std::io::BufReader;
6-
use struson::reader::*;
77

88
fn main() {
9-
// build using struson
10-
//-------------------------
11-
let mut json_reader = JsonStreamReader::new(
12-
r#"{
13-
"aircraft": {
14-
"id": "A12345",
15-
"model": "Boeing 747",
16-
"manufacturer": "Boeing",
17-
"capacity": {
18-
"seats": 416,
19-
"cargo_volume": 150.4
9+
let file = File::open("src/airplane.json").unwrap();
10+
let reader = BufReader::new(file);
11+
let stream = Deserializer::from_reader(reader).into_iter::<Value>();
12+
13+
let mut subject_stack: VecDeque<String> = VecDeque::new();
14+
let mut array_properties: HashMap<String, String> = HashMap::new();
15+
let mut property: Option<String> = None;
16+
17+
for value in stream {
18+
match value {
19+
Ok(Value::Object(obj)) => {
20+
let subject = format!("_:b{}", subject_stack.len()); // Create a new blank node
21+
println!("Created new subject: {}", subject);
22+
subject_stack.push_back(subject.clone());
23+
24+
if let Some(last_subject) = subject_stack.back() {
25+
println!("The last subject in the stack is: {}", last_subject);
26+
if let Some(prop) = &property {
27+
println!("Adding property for parent -> child relationship: {}", prop);
28+
}
29+
}
30+
31+
for (key, val) in obj {
32+
property = Some(format!("#{}", key));
33+
println!("Processing key: {}", key);
34+
process_value(&mut subject_stack, &property, val);
35+
}
36+
37+
// End of object; remove the subject from stack
38+
subject_stack.pop_back();
39+
}
40+
Ok(Value::Array(arr)) => {
41+
if let Some(last_subject) = subject_stack.back() {
42+
if let Some(prop) = &property {
43+
array_properties.insert(last_subject.clone(), prop.clone());
44+
}
45+
}
46+
for val in arr {
47+
process_value(&mut subject_stack, &property, val);
48+
}
49+
}
50+
Ok(other) => {
51+
process_value(&mut subject_stack, &property, other);
52+
}
53+
Err(e) => {
54+
eprintln!("Error parsing JSON: {}", e);
2055
}
21-
}
22-
}"#
23-
.as_bytes(),
24-
);
25-
26-
let mut subject_stack: VecDeque<NamedOrBlankNode> = VecDeque::new();
27-
let mut array_properties: HashMap<NamedOrBlankNode, NamedOrBlankNode> = HashMap::new();
28-
29-
json_reader.begin_object().unwrap();
30-
json_reader.next_name().unwrap();
31-
32-
// println!("hi");
33-
34-
// json_reader.begin_array().unwrap();
35-
// assert_eq!(json_reader.next_number::<u32>().unwrap().unwrap(), 1);
36-
// assert_eq!(json_reader.next_bool().unwrap(), true);
37-
// json_reader.end_array().unwrap();
38-
39-
json_reader.begin_object().unwrap();
40-
41-
while json_reader.has_next().unwrap() {
42-
match json_reader.peek().unwrap() {
43-
ValueType::Array => match token {
44-
if token
45-
},
46-
ValueType::Object => todo!(),
47-
ValueType::String => println!("A string: {}", json_reader.next_string().unwrap()),
48-
ValueType::Boolean => println!("a bool: {}", json_reader.next_bool().unwrap()),
49-
ValueType::Number => println!(
50-
"A string: {}",
51-
json_reader.next_number::<u32>().unwrap().unwrap()
52-
),
53-
54-
_ => panic!("Unexpected type"),
5556
}
5657
}
57-
58-
json_reader.end_object().unwrap();
59-
// Ensures that there is no trailing data
60-
json_reader.consume_trailing_whitespace().unwrap();
61-
//-------------------------
62-
63-
//built using serde_json
64-
//-------------------------
65-
// let file = File::open("src/airplane.json").unwrap();
66-
// let reader = BufReader::new(file);
67-
68-
// // Parse the JSON file into a serde_json::Value
69-
// let json_data: Value = serde_json::from_reader(reader).unwrap();
70-
// parse_json(&json_data);
71-
//-------------------------
7258
}
7359

74-
fn parse_json(value: &Value) {
75-
match value {
76-
Value::Null => println!("null"),
77-
Value::Bool(b) => println!("Boolean: {}", b),
78-
Value::Number(n) => println!("Number: {}", n),
79-
Value::String(s) => println!("String: {}", s),
80-
Value::Array(arr) => {
81-
println!("Array:");
82-
for (index, item) in arr.iter().enumerate() {
83-
print!("Index {}: ", index);
84-
parse_json(item);
85-
}
86-
}
87-
Value::Object(obj) => {
88-
println!("Object:");
89-
for (key, val) in obj.iter() {
90-
print!("Key: {}, Value: ", key);
91-
parse_json(val);
60+
fn process_value(subject_stack: &mut VecDeque<String>, property: &Option<String>, value: Value) {
61+
if let Some(last_subject) = subject_stack.back() {
62+
println!("The last subject in the stack is: {}", last_subject);
63+
if let Some(prop) = property {
64+
println!("Processing property: {}", prop);
65+
match value {
66+
Value::Bool(b) => {
67+
println!("Boolean value: {}", b);
68+
}
69+
Value::Number(num) => {
70+
let literal = if let Some(int) = num.as_i64() {
71+
int.to_string()
72+
} else if let Some(float) = num.as_f64() {
73+
float.to_string()
74+
} else {
75+
return;
76+
};
77+
println!("Number value: {}", literal);
78+
}
79+
Value::String(s) => {
80+
println!("String value: {}", s);
81+
}
82+
Value::Null => {
83+
println!("Null value");
84+
}
85+
Value::Object(obj) => {
86+
let subject = format!("_:b{}", subject_stack.len());
87+
println!("Created nested subject: {}", subject);
88+
subject_stack.push_back(subject);
89+
for (key, val) in obj {
90+
let nested_property = Some(format!("#{}", key));
91+
process_value(subject_stack, &nested_property, val);
92+
}
93+
subject_stack.pop_back();
94+
}
95+
Value::Array(arr) => {
96+
for val in arr {
97+
process_value(subject_stack, property, val);
98+
}
99+
}
92100
}
93101
}
94102
}
95103
}
96-
97-
98-
// psuedocode
99-
// subject array
100-
// properties array
101-
102-
// property = null
103-
104-
// While Next val exists
105-
106-
// if start of array
107-
108-
// if end of array
109-
110-
// if start of object
111-
// create blank node
112-
// if property exists and subject isnt empty create triples with subject array.last(),property,subject)
113-
// add blank node to subject array to track
114-
// if end of object
115-
// .pop() on subject array
116-
// WHAT DOES THIS MEAN( if (!subjectStack.isEmpty() && arrayProperties.containsKey(subjectStack.getLast())) property = arrayProperties.get(subjectStack.getLast());)
117-
// if string
118-
// create triple with last element of blank node array, property , string val
119-
// if number
120-
// create triple with last element of blank node array, property , number val
121-
// if bool
122-
// create triple with last element of blank node array, property , bool val
123-
// if key_name
124-
// property = key_name

0 commit comments

Comments
 (0)