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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Output options: layout, escaping, and float text.
/// 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). The plain formats
/// copy a `RawValue`'s text as it is, so U+2028 and U+2029 inside its
/// strings are written raw there (valid JSON, but not JavaScript source
/// before ES2019); every string the writers make has them escaped. For
/// output pasted into JavaScript, use `html_safe`, which escapes them in
/// raw text too.
///
/// Two options change the text of floats written from Rust values
/// ([`ValueWriter::f64`](crate::ValueWriter::f64) and
/// [`f32`](crate::ValueWriter::f32)), for output that must match another
/// writer's byte for byte: [`exponent_plus`](Self::exponent_plus) writes
/// `1e+16` for `1e16`, and [`even_ties`](Self::even_ties) changes which of
/// two equally close shortest texts is written. A [`Number`](crate::Number)
/// keeps its own text in every format. 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>(())
/// ```