|
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}; |
3 | 4 | use std::collections::{HashMap, VecDeque}; |
4 | 5 | use std::fs::File; |
5 | 6 | use std::io::BufReader; |
6 | | -use struson::reader::*; |
7 | 7 |
|
8 | 8 | 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); |
20 | 55 | } |
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"), |
55 | 56 | } |
56 | 57 | } |
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 | | - //------------------------- |
72 | 58 | } |
73 | 59 |
|
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 | + } |
92 | 100 | } |
93 | 101 | } |
94 | 102 | } |
95 | 103 | } |
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