Skip to content

Commit

Permalink
🔖 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zS1L3NT committed Sep 13, 2022
1 parent 8c041eb commit 1a07a4a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
18 changes: 0 additions & 18 deletions .vscode/launch.json

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parson"
version = "1.0.1"
version = "1.1.0"
edition = "2021"
description = "A crate for parsing JSON into Rust types"
documentation = "https://www.github.com/zS1L3NT/rs-parson"
Expand Down
19 changes: 19 additions & 0 deletions src/json_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ impl JSONArray {
result
}

pub fn format_string(&self, indents: i32, spaces: i32) -> String {
if self.data.len() == 0 {
return String::from("[]");
}

let mut result = "[".to_string();

for value in self.data.iter() {
result.push_str(&format!(
"\n{}{},",
" ".repeat(((indents + 1) * spaces) as usize),
value.format_string(indents + 1, spaces)
));
}

result.push_str(&format!("\n{}]", " ".repeat((indents * spaces) as usize)));
result
}

/// Convert JSON Array to a Rust Vector
pub fn to_vec(&self) -> Vec<JSONValue> {
self.data.clone()
Expand Down
21 changes: 21 additions & 0 deletions src/json_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ impl JSONObject {
result
}

/// Format JSON Object to a Rust owned string
pub fn format_string(&self, indents: i32, spaces: i32) -> String {
if self.data.len() == 0 {
return String::from("{}");
}

let mut result = "{".to_string();

for (key, value) in self.data.iter() {
result.push_str(&format!(
"\n{}\"{}\": {},",
" ".repeat(((indents + 1) * spaces) as usize),
key,
value.format_string(indents + 1, spaces)
));
}

result.push_str(&format!("\n{}}}", " ".repeat((indents * spaces) as usize)));
result
}

/// Convert JSON Object to a Rust HashMap
pub fn to_hashmap(&self) -> HashMap<String, JSONValue> {
self.data
Expand Down
11 changes: 11 additions & 0 deletions src/json_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,15 @@ impl JSONValue {
JSONType::Object(json_object) => json_object.to_string(),
}
}

pub fn format_string(&self, indents: i32, spaces: i32) -> String {
match &self.data {
JSONType::String(json_string) => json_string.to_string(),
JSONType::Number(json_number) => json_number.to_string(),
JSONType::Boolean(json_boolean) => json_boolean.to_string(),
JSONType::Null(json_null) => json_null.to_string(),
JSONType::Array(json_array) => json_array.format_string(indents, spaces),
JSONType::Object(json_object) => json_object.format_string(indents, spaces),
}
}
}

0 comments on commit 1a07a4a

Please sign in to comment.