1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Output options: layout and escaping.
/// How the writers lay out and escape their output.
///
/// The default is compact JSON (no whitespace) with the escaping described
/// on [`write_string`](crate::write_string): quotes, backslashes, control
/// characters, and U+2028/U+2029, and nothing else. [`pretty`](Self::pretty)
/// is the [`to_string_pretty`](crate::to_string_pretty) layout. Two stricter
/// escapings are available for output that is embedded somewhere JSON's own
/// rules are not enough:
///
/// - [`html_safe`](Self::html_safe) also escapes `<`, `>`, and `&`, so the
/// output can sit inside an HTML `<script>` element or attribute without
/// ending it (`</script>` cannot appear in it);
/// - [`ascii`](Self::ascii) escapes every non-ASCII character as `\uXXXX`
/// (a surrogate pair above U+FFFF), for channels that are not 8-bit clean.
///
/// Both apply to everything written, [`RawValue`](crate::RawValue) text
/// included (its strings are escaped the same way). Every format writes
/// valid JSON that reads back as the same value (for objects without
/// repeated keys: the writers do not check for them; see
/// [`ObjectWriter`](crate::ObjectWriter)); the formats differ only in
/// bytes.
///
/// The struct is `#[non_exhaustive]`: build it from [`Format::compact`] or
/// [`Format::pretty`] and the `with_*` methods.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{json, to_string_with, Format};
///
/// let v = json!({ "html": "</script>", "word": "caf\u{e9}" });
/// let safe = to_string_with(&v, Format::compact().html_safe(true).ascii(true));
/// assert!(!safe.contains('<') && safe.is_ascii());
/// assert_eq!(jsonx_lib::parse(&safe)?, v);
///
/// let wide = to_string_with(&json!([1]), Format::pretty().with_indent(4));
/// assert_eq!(wide, "[\n 1\n]");
/// # Ok::<(), jsonx_lib::Error>(())
/// ```