|
| 1 | +use oxrdf::NamedOrBlankNode; |
| 2 | +use serde_json::Value; |
| 3 | +use std::collections::{HashMap, VecDeque}; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::BufReader; |
1 | 6 | use struson::reader::*; |
2 | 7 |
|
3 | 8 | fn main() { |
4 | | - // let json = r#"{"a": true}"#; |
5 | | - // let mut json_reader = JsonStreamReader::new(json.as_bytes()); |
| 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 |
| 20 | + } |
| 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(); |
6 | 38 |
|
7 | | - let mut json_reader = JsonStreamReader::new(r#"{"a": 1, "b": 2}"#.as_bytes()); |
8 | 39 | json_reader.begin_object().unwrap(); |
9 | 40 |
|
10 | 41 | while json_reader.has_next().unwrap() { |
11 | 42 | 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()), |
| 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 | + |
14 | 54 | _ => panic!("Unexpected type"), |
15 | 55 | } |
16 | | - |
17 | | - // json_reader.skip_name().unwrap(); |
18 | | - // json_reader.skip_value().unwrap(); |
19 | | - // println!("SKIPPED"); |
20 | 56 | } |
21 | 57 |
|
22 | 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 | + //------------------------- |
23 | 72 | } |
| 73 | + |
| 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); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 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