1 // Take a look at the license at the top of the repository in the LICENSE file.
4 json_minifier
::{keep_element, JsonMinifier}
,
5 read
::json_read
::JsonRead
,
6 string
::JsonMultiFilter
,
9 use std
::io
::{self, Read}
;
21 type JsonMethod
= fn(&mut JsonMinifier
, &char, Option
<&char>) -> bool
;
23 /// Minifies a given String by JSON minification rules
28 /// use minifier::json::minify;
36 /// let json_minified = minify(json);
37 /// assert_eq!(&json_minified.to_string(), r#"{"test":"test","test2":2}"#);
40 pub fn minify(json
: &str) -> Minified
<'_
> {
41 Minified(JsonMultiFilter
::new(json
.chars(), keep_element
))
45 pub struct Minified
<'a
>(JsonMultiFilter
<'a
, JsonMethod
>);
47 impl<'a
> Minified
<'a
> {
48 pub fn write
<W
: io
::Write
>(self, w
: W
) -> io
::Result
<()> {
53 impl<'a
> fmt
::Display
for Minified
<'a
> {
54 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
59 /// Minifies a given Read by JSON minification rules
64 /// extern crate minifier;
65 /// use std::fs::File;
66 /// use std::io::Read;
67 /// use minifier::json::minify_from_read;
70 /// let mut html_minified = String::new();
71 /// let mut file = File::open("tests/files/test.json").expect("file not found");
72 /// minify_from_read(file).read_to_string(&mut html_minified);
76 pub fn minify_from_read
<R
: Read
>(json
: R
) -> JsonRead
<JsonMethod
, R
> {
77 JsonRead
::new(json
, keep_element
)
81 fn removal_from_read() {
84 let input
= File
::open("tests/files/test.json").expect("file not found");
85 let expected
: String
= "{\"test\":\"\\\" test2\",\"test2\":\"\",\"test3\":\" \"}".into();
86 let mut actual
= String
::new();
87 minify_from_read(input
)
88 .read_to_string(&mut actual
)
89 .expect("error at read");
90 assert_eq
!(actual
, expected
);
94 fn removal_of_control_characters() {
95 let input
= "\n".into();
96 let expected
: String
= "".into();
97 let actual
= minify(input
);
98 assert_eq
!(actual
.to_string(), expected
);
102 fn removal_of_whitespace_outside_of_tags() {
111 let expected
: String
= "{\"test\":\"\\\" test2\",\"test2\":\"\",\"test3\":\" \"}".into();
112 let actual
= minify(input
);
113 assert_eq
!(actual
.to_string(), expected
);