about summary refs log tree commit diff
path: root/src/json.rs
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-25 21:25:06 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-25 21:25:06 -0700
commitb3cb3553ab5c4649b0fe6b5f94ac42c5b69e8f89 (patch)
treeed33922a84eeaa541d8a5c772445b81be1eb10fa /src/json.rs
parentc312a47101bf1787e7bbc44c409e15315684fd19 (diff)
refactor JSON code into its own file, now a full parser
also, update to the latest version of the JSON library. it's under active development and had a release today. huh!

Force-Push: yes
Change-Id: I0e162f64c963744cf786a85d00c83dbabade05be
Diffstat (limited to 'src/json.rs')
-rw-r--r--src/json.rs207
1 files changed, 207 insertions, 0 deletions
diff --git a/src/json.rs b/src/json.rs
new file mode 100644
index 0000000..0ed2efa
--- /dev/null
+++ b/src/json.rs
@@ -0,0 +1,207 @@
+use crate::error::{ Error, Result };
+
+use std::collections::HashMap;
+use std::str::FromStr;
+
+use json_event_parser::{ JsonEvent, SliceJsonParser };
+
+
+#[allow(unused)]
+#[derive(Debug)]
+pub enum Value {
+  Object(Object),
+  Array(Array),
+  Integer(i64),
+  Float(f64),
+  String(String),
+  Boolean(bool),
+  Null,
+}
+pub type Object = HashMap<String, Value>;
+pub type Array = Vec<Value>;
+
+
+pub fn parse_json(json: &[u8]) -> Result<Value> {
+  let mut parser = SliceJsonParser::new(json);
+
+  if let Some(event) = parser.next() {
+    match event? {
+      JsonEvent::StartObject => {
+        Ok(Value::Object(parse_json_object(&mut parser)?))
+      }
+      JsonEvent::StartArray => {
+        Ok(Value::Array(parse_json_array(&mut parser)?))
+      }
+      JsonEvent::String(_) => {
+        Err(Error::custom("JSON: unexpected string at top level"))
+      }
+      JsonEvent::Number(_) => {
+        Err(Error::custom("JSON: unexpected number at top level"))
+      }
+      JsonEvent::Boolean(_) => {
+        Err(Error::custom("JSON: unexpected Boolean at top level"))
+      }
+      JsonEvent::Null => {
+        Err(Error::custom("JSON: unexpected null at top level"))
+      }
+      JsonEvent::ObjectKey(_) => {
+        Err(Error::custom("JSON: unexpected key at top level"))
+      }
+      JsonEvent::EndObject => {
+        Err(Error::custom("JSON: unexpected object end at top level"))
+      }
+      JsonEvent::EndArray => {
+        Err(Error::custom("JSON: unexpected array end at top level"))
+      }
+    }
+  } else {
+    Err(Error::custom("JSON: unexpected end at top level"))
+  }
+}
+
+
+fn parse_json_object(parser: &mut SliceJsonParser) -> Result<Object> {
+  let mut object = HashMap::new();
+
+  loop {
+    if let Some(event) = parser.next() {
+      match event? {
+        JsonEvent::EndObject => {
+          return Ok(object);
+        }
+
+        JsonEvent::ObjectKey(key) => {
+          if object.contains_key(key.as_ref()) {
+            return Err(Error::custom(&format!("JSON: duplicate key {}", key)));
+          }
+
+          let value = parse_json_value(parser)?;
+          object.insert(key.into_owned(), value);
+        }
+
+        JsonEvent::StartObject => {
+          return Err(Error::custom("JSON: unexpected object instead of key"));
+        }
+        JsonEvent::StartArray => {
+          return Err(Error::custom("JSON: unexpected array instead of key"));
+        }
+
+        JsonEvent::String(_) => {
+          return Err(Error::custom("JSON: unexpected string instead of key"));
+        }
+        JsonEvent::Number(_) => {
+          return Err(Error::custom("JSON: unexpected number instead of key"));
+        }
+        JsonEvent::Boolean(_) => {
+          return Err(Error::custom("JSON: unexpected Boolean instead of key"));
+        }
+        JsonEvent::Null => {
+          return Err(Error::custom("JSON: unexpected null instead of key"));
+        }
+
+        JsonEvent::EndArray => {
+          return Err(Error::custom("JSON: unexpected array end instead of key"));
+        }
+      }
+    } else {
+      return Err(Error::custom("JSON: unexpected end instead of key"));
+    }
+  }
+}
+
+
+fn parse_json_array(parser: &mut SliceJsonParser) -> Result<Array> {
+  let mut array = Vec::new();
+
+  loop {
+    if let Some(event) = parser.next() {
+      match event? {
+        JsonEvent::EndArray => {
+          return Ok(array);
+        }
+
+        JsonEvent::StartObject => {
+          array.push(Value::Object(parse_json_object(parser)?));
+        }
+        JsonEvent::StartArray => {
+          array.push(Value::Array(parse_json_array(parser)?));
+        }
+        other => {
+          array.push(parse_json_atomic_value(other)?);
+        }
+      }
+    } else {
+      return Err(Error::custom("JSON: unexpected end instead of value"));
+    }
+  }
+}
+
+
+fn parse_json_value(parser: &mut SliceJsonParser) -> Result<Value> {
+  if let Some(event) = parser.next() {
+    match event? {
+      JsonEvent::StartObject => {
+        Ok(Value::Object(parse_json_object(parser)?))
+      }
+      JsonEvent::StartArray => {
+        Ok(Value::Array(parse_json_array(parser)?))
+      }
+      other => parse_json_atomic_value(other),
+    }
+  } else {
+    Err(Error::custom("JSON: unexpected end instead of value"))
+  }
+}
+
+
+fn parse_json_atomic_value(event: JsonEvent) -> Result<Value> {
+  match event {
+    JsonEvent::String(string) => {
+      Ok(Value::String(string.into_owned()))
+    }
+    JsonEvent::Number(token) => {
+      parse_json_number(&token)
+    }
+    JsonEvent::Boolean(value) => {
+      Ok(Value::Boolean(value))
+    }
+    JsonEvent::Null => {
+      Ok(Value::Null)
+    }
+
+    JsonEvent::ObjectKey(_) => {
+      Err(Error::custom("JSON: unexpected key instead of value"))
+    }
+    JsonEvent::EndObject => {
+      Err(Error::custom("JSON: unexpected object end instead of value"))
+    }
+    JsonEvent::EndArray => {
+      Err(Error::custom("JSON: unexpected array end instead of value"))
+    }
+
+    /*   These ones don't actually come up from how we're called, but it's
+     * better to handle the cases exhaustively.
+     */
+    JsonEvent::StartObject => {
+      Err(Error::custom(
+          "JSON: unexpected object start instead of atomic value"))
+    }
+    JsonEvent::StartArray => {
+      Err(Error::custom(
+          "JSON: unexpected array start instead of atomic value"))
+    }
+  }
+}
+
+
+fn parse_json_number(token: &str) -> Result<Value> {
+  if let Ok(number) = i64::from_str_radix(&token, 10) {
+    Ok(Value::Integer(number))
+  } else if let Ok(number) = f64::from_str(&token) {
+    Ok(Value::Float(number))
+  } else {
+    /* JSON is highly permissive about numbers, but we aren't. */
+    Err(Error::custom("JSON: unsupported number syntax"))
+  }
+}
+