From 4e1cf2bdcafb45ba5fc8a295bc9103328ed071b1 Mon Sep 17 00:00:00 2001 From: Miko0187 Date: Thu, 3 Aug 2023 20:52:02 +0200 Subject: [PATCH] Added custom json deserializer --- Cargo.toml | 2 - src/json/deserialize.rs | 135 ++++++++++++++++++++++++++++++++++++++++ src/json/serialize.rs | 7 +++ src/main.rs | 5 ++ src/stores/container.rs | 26 ++++---- src/stores/db.rs | 2 +- 6 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 src/json/deserialize.rs create mode 100644 src/json/serialize.rs diff --git a/Cargo.toml b/Cargo.toml index 0331372..cfba332 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,4 @@ edition = "2021" [dependencies] hashbrown = "0.14" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" zstd = "0.12.4" \ No newline at end of file diff --git a/src/json/deserialize.rs b/src/json/deserialize.rs new file mode 100644 index 0000000..123b918 --- /dev/null +++ b/src/json/deserialize.rs @@ -0,0 +1,135 @@ +use hashbrown::HashMap; + +use crate::values::key::Key; + +struct Deserializer { + data: String, + index: usize, +} + +impl Deserializer { + pub(crate) fn new(data: String) -> Deserializer { + Deserializer { data, index: 0 } + } + + fn next(&mut self) -> char { + let c = self + .data + .chars() + .nth(self.index) + .expect("Unable to get next character"); + self.index += 1; + c + } + + fn peek(&self, offset: usize) -> char { + self.data + .chars() + .nth(self.index + offset) + .expect("Unable to peek") + } + + fn skip_whitespace(&mut self) { + while self.peek(0).is_whitespace() { + self.next(); + } + } + + fn parse_string(&mut self) -> String { + let mut string = String::new(); + self.next(); + while self.peek(0) != '"' { + string.push(self.next()); + } + self.next(); + string + } + + fn parse_number(&mut self) -> f64 { + let mut number = String::new(); + while self.peek(0).is_numeric() || self.peek(0) == '.' { + number.push(self.next()); + } + number.parse::().expect("Unable to parse number") + } + + fn parse_bool(&mut self) -> bool { + let mut boolean = String::new(); + while self.peek(0).is_alphabetic() { + boolean.push(self.next()); + } + match boolean.as_str() { + "true" => true, + "false" => false, + _ => panic!("Unable to parse boolean"), + } + } + + fn parse_null(&mut self) -> () { + let mut null = String::new(); + while self.peek(0).is_alphabetic() { + null.push(self.next()); + } + match null.as_str() { + "null" => (), + _ => panic!("Unable to parse null"), + } + } + + fn parse_array(&mut self) -> Vec { + let mut array = Vec::new(); + self.next(); + self.skip_whitespace(); + while self.peek(0) != ']' { + let value = self.parse_value(); + self.skip_whitespace(); + array.push(value); + if self.peek(0) == ',' { + self.next(); + } + self.skip_whitespace(); + } + self.next(); + array + } + + fn parse_object(&mut self) -> HashMap { + let mut object = HashMap::new(); + self.next(); + self.skip_whitespace(); + while self.peek(0) != '}' { + let key = self.parse_string(); + self.skip_whitespace(); + self.next(); + self.skip_whitespace(); + let value = self.parse_value(); + self.skip_whitespace(); + object.insert(key, value); + if self.peek(0) == ',' { + self.next(); + } + self.skip_whitespace(); + } + self.next(); + object + } + + fn parse_value(&mut self) -> Key { + self.skip_whitespace(); + let value = match self.peek(0) { + '"' => Key::String(self.parse_string()), + '[' => Key::Array(self.parse_array()), + '{' => Key::Object(self.parse_object()), + '0'..='9' => Key::Number(self.parse_number()), + 't' | 'f' => Key::Boolean(self.parse_bool()), + 'n' => Key::Null(self.parse_null()), + _ => panic!("Invalid value"), + }; + self.skip_whitespace(); + value + } + + pub(crate) fn deserialize(&mut self) -> HashMap { + self.parse_object() + } +} diff --git a/src/json/serialize.rs b/src/json/serialize.rs new file mode 100644 index 0000000..f771f23 --- /dev/null +++ b/src/json/serialize.rs @@ -0,0 +1,7 @@ +use hashbrown::HashMap; + +use crate::values::key::Key; + +pub fn serialize(map: HashMap) -> String { + unimplemented!() +} diff --git a/src/main.rs b/src/main.rs index 7ae7592..376c153 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,11 @@ use std::io::Read; mod encoding; +mod json { + mod deserialize; + mod serialize; +} + mod stores { pub mod container; pub mod db; diff --git a/src/stores/container.rs b/src/stores/container.rs index cbf51fc..d4056ec 100644 --- a/src/stores/container.rs +++ b/src/stores/container.rs @@ -8,21 +8,21 @@ pub struct Container { } /* -Container { - "TestContainer", - { - aaron: { - name: "Aaron", - age: 20, +{ + "name": "TestContainer", + "keys": { + "aaron": { + "name": "Aaron", + "age": 20 }, - bob: { - name: "Bob", - age: 21, - }, - carl: { - name: "Carl", - age: 22, + "bob": { + "name": "Bob", + "age": 21 }, + "carl": { + "name": "Carl", + "age": 22 + } } } */ diff --git a/src/stores/db.rs b/src/stores/db.rs index a414ce5..af960d3 100644 --- a/src/stores/db.rs +++ b/src/stores/db.rs @@ -40,7 +40,7 @@ fn load_containers(file: String) -> Vec> { let mut containers = Vec::new(); for parse in parsed { - containers.push(serde_json::from_str(&parse).unwrap()); + containers.push(); } containers