json_keyquotes_convert/lib.rs
1//! Documentation for the `json_keyquotes_convert` crate.
2//!
3//! The `json_keyquotes_convert` crate is meant to be used to perform
4//! various transformations to JSON, including but not limited to
5//! adding and removing quotes around the JSON keys.
6//!
7//! It is recommended to use the [JsonKeyQuoteConverter] builder,
8//! but using the core functions in [json_key_quote_utils] is possible too.
9
10pub mod json_key_quote_utils;
11pub mod load_write_utils;
12
13/// The quotes to use for the JSON keys.
14///
15/// This does not affect existing single-quoted or double-quoted keys in JSON.
16///
17/// The default value is [Quotes::DoubleQuote].
18#[derive(Clone, Copy)]
19pub enum Quotes {
20 DoubleQuote,
21 SingleQuote,
22}
23
24impl Quotes {
25 fn as_str(&self) -> &'static str {
26 match self {
27 Quotes::DoubleQuote => "\"",
28 Quotes::SingleQuote => "'",
29 }
30 }
31}
32
33impl Default for Quotes {
34 fn default() -> Self {
35 Quotes::DoubleQuote
36 }
37}
38
39/// The builder for the JSON conversions.
40pub struct JsonKeyQuoteConverter {
41 json: String,
42 quote_type: Quotes,
43}
44
45impl JsonKeyQuoteConverter {
46 /// Returns a new [JsonKeyQuoteConverter].
47 ///
48 /// # Arguments
49 ///
50 /// * `json` - A JSON string.
51 /// * `quote_type` - Whether the JSON keys should be single- or double-quoted.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use json_keyquotes_convert::{JsonKeyQuoteConverter, Quotes};
57 ///
58 /// let converter = JsonKeyQuoteConverter::new("{\"key\": \"val\"}", Quotes::default());
59 /// ```
60 pub fn new(json: &str, quote_type: Quotes) -> JsonKeyQuoteConverter {
61 JsonKeyQuoteConverter {
62 json: String::from(json),
63 quote_type: quote_type,
64 }
65 }
66
67 /// Adds key-quotes to the JSON string.
68 ///
69 /// # Examples
70 ///
71 /// ```
72 /// use json_keyquotes_convert::{JsonKeyQuoteConverter, Quotes};
73 ///
74 /// let json_added = JsonKeyQuoteConverter::new("{key: \"val\"}", Quotes::default())
75 /// .add_key_quotes().json();
76 /// assert_eq!(json_added, "{\"key\": \"val\"}");
77 ///
78 /// let json_already_existing = JsonKeyQuoteConverter::new("{\"key\": \"val\"}", Quotes::default())
79 /// .add_key_quotes().json();
80 /// assert_eq!(json_already_existing, "{\"key\": \"val\"}");
81 /// ```
82 pub fn add_key_quotes(mut self) -> JsonKeyQuoteConverter {
83 self.json = json_key_quote_utils::json_add_key_quotes(&self.json, self.quote_type);
84
85 self
86 }
87
88 /// Removes key-quotes from the JSON string.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use json_keyquotes_convert::{JsonKeyQuoteConverter, Quotes};
94 ///
95 /// let json_removed = JsonKeyQuoteConverter::new("{\"key\": \"val\"}", Quotes::default())
96 /// .remove_key_quotes().json();
97 /// assert_eq!(json_removed, "{key: \"val\"}");
98 ///
99 /// let json_already_removed = JsonKeyQuoteConverter::new("{key: \"val\"}", Quotes::default())
100 /// .remove_key_quotes().json();
101 /// assert_eq!(json_already_removed, "{key: \"val\"}");
102 /// ```
103 pub fn remove_key_quotes(mut self) -> JsonKeyQuoteConverter {
104 self.json = json_key_quote_utils::json_remove_key_quotes(&self.json);
105
106 self
107 }
108
109 /// Escape ctrl-characters from the JSON string values
110 /// and remove ctrl-characters from the JSON keys with keyquotes.
111 ///
112 /// This method will escape `newlines`, `tabs` and `carriage returns` in the JSON string values
113 /// and remove `newlines`, `tabs` and `carriage returns` in the JSON keys with keyquotes.
114 ///
115 /// # Examples
116 ///
117 /// ```
118 /// use json_keyquotes_convert::{JsonKeyQuoteConverter, Quotes};
119 ///
120 /// let json_escaped = JsonKeyQuoteConverter::new(r#"{"key": "va
121 /// l"}"#, Quotes::default())
122 /// .escape_ctrlchars().json();
123 /// assert_eq!(json_escaped, r#"{"key": "va\nl"}"#);
124 ///
125 /// let json_already_escaped = JsonKeyQuoteConverter::new(r#"{"key": "va\nl"}"#, Quotes::default())
126 /// .escape_ctrlchars().escape_ctrlchars().json();
127 /// assert_eq!(json_already_escaped, r#"{"key": "va\nl"}"#);
128 /// ```
129 pub fn escape_ctrlchars(mut self) -> JsonKeyQuoteConverter {
130 self.json = json_key_quote_utils::json_escape_ctrlchars(&self.json);
131
132 self
133 }
134
135 /// Unescape ctrl-characters from the JSON string values
136 /// and remove ctrl-characters from the JSON keys without keyquotes.
137 ///
138 /// This method will unescape `newlines`, `tabs` and `carriage returns` in the JSON string values
139 /// and remove `newlines`, `tabs` and `carriage returns` in the JSON keys without keyquotes.
140 ///
141 /// # Examples
142 ///
143 /// ```
144 /// use json_keyquotes_convert::{JsonKeyQuoteConverter, Quotes};
145 ///
146 /// let json_unescaped = JsonKeyQuoteConverter::new(r#"{key: "va\nl"}"#, Quotes::default())
147 /// .unescape_ctrlchars().json();
148 /// assert_eq!(json_unescaped, r#"{key: "va
149 /// l"}"#);
150 ///
151 /// let json_already_unescaped = JsonKeyQuoteConverter::new(r#"{key: "va\nl"}"#, Quotes::default())
152 /// .unescape_ctrlchars().unescape_ctrlchars().json();
153 /// assert_eq!(json_already_unescaped, r#"{key: "va
154 /// l"}"#);
155 /// ```
156 pub fn unescape_ctrlchars(mut self) -> JsonKeyQuoteConverter {
157 self.json = json_key_quote_utils::json_unescape_ctrlchars(&self.json);
158
159 self
160 }
161
162 /// Returns the JSON string.
163 ///
164 /// # Examples
165 ///
166 /// ```
167 /// use json_keyquotes_convert::{JsonKeyQuoteConverter, Quotes};
168 ///
169 /// let json = JsonKeyQuoteConverter::new(r#"{"key": "value"}"#, Quotes::default())
170 /// .json();
171 /// assert_eq!(json, r#"{"key": "value"}"#);
172 /// ```
173 pub fn json(self) -> String {
174 self.json
175 }
176}