1use std::fmt::{Display, LowerExp};
6use std::io;
7use std::num::FpCategory;
8
9use nu_utils::ObviousFloat;
10
11use super::error::{Error, ErrorCode, Result};
12use serde::ser;
13
14pub struct Serializer<W, F> {
16 writer: W,
17 formatter: F,
18}
19
20impl<'a, W> Serializer<W, HjsonFormatter<'a>>
21where
22 W: io::Write,
23{
24 #[inline]
26 pub fn new(writer: W) -> Self {
27 Serializer::with_formatter(writer, HjsonFormatter::new())
28 }
29
30 #[inline]
31 pub fn with_indent(writer: W, indent: &'a [u8]) -> Self {
32 Serializer::with_formatter(writer, HjsonFormatter::with_indent(indent))
33 }
34}
35
36impl<W, F> Serializer<W, F>
37where
38 W: io::Write,
39 F: Formatter,
40{
41 #[inline]
44 pub fn with_formatter(writer: W, formatter: F) -> Self {
45 Serializer { writer, formatter }
46 }
47
48 #[inline]
50 pub fn into_inner(self) -> W {
51 self.writer
52 }
53}
54
55#[doc(hidden)]
56#[derive(Eq, PartialEq)]
57pub enum State {
58 Empty,
59 First,
60 Rest,
61}
62
63#[doc(hidden)]
64pub struct Compound<'a, W, F> {
65 ser: &'a mut Serializer<W, F>,
66 state: State,
67}
68
69impl<'a, W, F> ser::Serializer for &'a mut Serializer<W, F>
70where
71 W: io::Write,
72 F: Formatter,
73{
74 type Ok = ();
75 type Error = Error;
76
77 type SerializeSeq = Compound<'a, W, F>;
78 type SerializeTuple = Compound<'a, W, F>;
79 type SerializeTupleStruct = Compound<'a, W, F>;
80 type SerializeTupleVariant = Compound<'a, W, F>;
81 type SerializeMap = Compound<'a, W, F>;
82 type SerializeStruct = Compound<'a, W, F>;
83 type SerializeStructVariant = Compound<'a, W, F>;
84
85 #[inline]
86 fn serialize_bool(self, value: bool) -> Result<()> {
87 self.formatter.start_value(&mut self.writer)?;
88 if value {
89 self.writer.write_all(b"true").map_err(From::from)
90 } else {
91 self.writer.write_all(b"false").map_err(From::from)
92 }
93 }
94
95 #[inline]
96 fn serialize_i8(self, value: i8) -> Result<()> {
97 self.formatter.start_value(&mut self.writer)?;
98 write!(&mut self.writer, "{value}").map_err(From::from)
99 }
100
101 #[inline]
102 fn serialize_i16(self, value: i16) -> Result<()> {
103 self.formatter.start_value(&mut self.writer)?;
104 write!(&mut self.writer, "{value}").map_err(From::from)
105 }
106
107 #[inline]
108 fn serialize_i32(self, value: i32) -> Result<()> {
109 self.formatter.start_value(&mut self.writer)?;
110 write!(&mut self.writer, "{value}").map_err(From::from)
111 }
112
113 #[inline]
114 fn serialize_i64(self, value: i64) -> Result<()> {
115 self.formatter.start_value(&mut self.writer)?;
116 write!(&mut self.writer, "{value}").map_err(From::from)
117 }
118
119 #[inline]
120 fn serialize_u8(self, value: u8) -> Result<()> {
121 self.formatter.start_value(&mut self.writer)?;
122 write!(&mut self.writer, "{value}").map_err(From::from)
123 }
124
125 #[inline]
126 fn serialize_u16(self, value: u16) -> Result<()> {
127 self.formatter.start_value(&mut self.writer)?;
128 write!(&mut self.writer, "{value}").map_err(From::from)
129 }
130
131 #[inline]
132 fn serialize_u32(self, value: u32) -> Result<()> {
133 self.formatter.start_value(&mut self.writer)?;
134 write!(&mut self.writer, "{value}").map_err(From::from)
135 }
136
137 #[inline]
138 fn serialize_u64(self, value: u64) -> Result<()> {
139 self.formatter.start_value(&mut self.writer)?;
140 write!(&mut self.writer, "{value}").map_err(From::from)
141 }
142
143 #[inline]
144 fn serialize_f32(self, value: f32) -> Result<()> {
145 self.formatter.start_value(&mut self.writer)?;
146 fmt_f32_or_null(&mut self.writer, if value == -0f32 { 0f32 } else { value })
147 }
148
149 #[inline]
150 fn serialize_f64(self, value: f64) -> Result<()> {
151 self.formatter.start_value(&mut self.writer)?;
152 fmt_f64_or_null(&mut self.writer, if value == -0f64 { 0f64 } else { value })
153 }
154
155 #[inline]
156 fn serialize_char(self, value: char) -> Result<()> {
157 self.formatter.start_value(&mut self.writer)?;
158 escape_char(&mut self.writer, value)
159 }
160
161 #[inline]
162 fn serialize_str(self, value: &str) -> Result<()> {
163 quote_str(&mut self.writer, &mut self.formatter, value)
164 }
165
166 #[inline]
167 fn serialize_bytes(self, value: &[u8]) -> Result<()> {
168 let mut seq = self.serialize_seq(Some(value.len()))?;
169 for byte in value {
170 ser::SerializeSeq::serialize_element(&mut seq, byte)?
171 }
172 ser::SerializeSeq::end(seq)
173 }
174
175 #[inline]
176 fn serialize_unit(self) -> Result<()> {
177 self.formatter.start_value(&mut self.writer)?;
178 self.writer.write_all(b"null").map_err(From::from)
179 }
180
181 #[inline]
182 fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
183 self.serialize_unit()
184 }
185
186 #[inline]
187 fn serialize_unit_variant(
188 self,
189 _name: &'static str,
190 _variant_index: u32,
191 variant: &'static str,
192 ) -> Result<()> {
193 self.serialize_str(variant)
194 }
195
196 #[inline]
198 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
199 where
200 T: ?Sized + ser::Serialize,
201 {
202 value.serialize(self)
203 }
204
205 #[inline]
206 fn serialize_newtype_variant<T>(
207 self,
208 _name: &'static str,
209 _variant_index: u32,
210 variant: &'static str,
211 value: &T,
212 ) -> Result<()>
213 where
214 T: ?Sized + ser::Serialize,
215 {
216 self.formatter.open(&mut self.writer, b'{')?;
217 self.formatter.comma(&mut self.writer, true)?;
218 escape_key(&mut self.writer, variant)?;
219 self.formatter.colon(&mut self.writer)?;
220 value.serialize(&mut *self)?;
221 self.formatter.close(&mut self.writer, b'}')
222 }
223
224 #[inline]
225 fn serialize_none(self) -> Result<()> {
226 self.serialize_unit()
227 }
228
229 #[inline]
230 fn serialize_some<V>(self, value: &V) -> Result<()>
231 where
232 V: ?Sized + ser::Serialize,
233 {
234 value.serialize(self)
235 }
236
237 #[inline]
238 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
239 let state = if len == Some(0) {
240 self.formatter.start_value(&mut self.writer)?;
241 self.writer.write_all(b"[]")?;
242 State::Empty
243 } else {
244 self.formatter.open(&mut self.writer, b'[')?;
245 State::First
246 };
247 Ok(Compound { ser: self, state })
248 }
249
250 #[inline]
251 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
252 self.serialize_seq(Some(len))
253 }
254
255 #[inline]
256 fn serialize_tuple_struct(
257 self,
258 _name: &'static str,
259 len: usize,
260 ) -> Result<Self::SerializeTupleStruct> {
261 self.serialize_seq(Some(len))
262 }
263
264 #[inline]
265 fn serialize_tuple_variant(
266 self,
267 _name: &'static str,
268 _variant_index: u32,
269 variant: &'static str,
270 len: usize,
271 ) -> Result<Self::SerializeTupleVariant> {
272 self.formatter.open(&mut self.writer, b'{')?;
273 self.formatter.comma(&mut self.writer, true)?;
274 escape_key(&mut self.writer, variant)?;
275 self.formatter.colon(&mut self.writer)?;
276 self.serialize_seq(Some(len))
277 }
278
279 #[inline]
280 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
281 let state = if len == Some(0) {
282 self.formatter.start_value(&mut self.writer)?;
283 self.writer.write_all(b"{}")?;
284 State::Empty
285 } else {
286 self.formatter.open(&mut self.writer, b'{')?;
287 State::First
288 };
289 Ok(Compound { ser: self, state })
290 }
291
292 #[inline]
293 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
294 self.serialize_map(Some(len))
295 }
296
297 #[inline]
298 fn serialize_struct_variant(
299 self,
300 _name: &'static str,
301 _variant_index: u32,
302 variant: &'static str,
303 len: usize,
304 ) -> Result<Self::SerializeStructVariant> {
305 self.formatter.open(&mut self.writer, b'{')?;
306 self.formatter.comma(&mut self.writer, true)?;
307 escape_key(&mut self.writer, variant)?;
308 self.formatter.colon(&mut self.writer)?;
309 self.serialize_map(Some(len))
310 }
311}
312
313impl<W, F> ser::SerializeSeq for Compound<'_, W, F>
314where
315 W: io::Write,
316 F: Formatter,
317{
318 type Ok = ();
319 type Error = Error;
320
321 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
322 where
323 T: serde::Serialize + ?Sized,
324 {
325 self.ser
326 .formatter
327 .comma(&mut self.ser.writer, self.state == State::First)?;
328 self.state = State::Rest;
329 value.serialize(&mut *self.ser)
330 }
331
332 fn end(self) -> Result<Self::Ok> {
333 match self.state {
334 State::Empty => Ok(()),
335 _ => self.ser.formatter.close(&mut self.ser.writer, b']'),
336 }
337 }
338}
339
340impl<W, F> ser::SerializeTuple for Compound<'_, W, F>
341where
342 W: io::Write,
343 F: Formatter,
344{
345 type Ok = ();
346 type Error = Error;
347
348 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
349 where
350 T: serde::Serialize + ?Sized,
351 {
352 ser::SerializeSeq::serialize_element(self, value)
353 }
354
355 fn end(self) -> Result<Self::Ok> {
356 ser::SerializeSeq::end(self)
357 }
358}
359
360impl<W, F> ser::SerializeTupleStruct for Compound<'_, W, F>
361where
362 W: io::Write,
363 F: Formatter,
364{
365 type Ok = ();
366 type Error = Error;
367
368 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
369 where
370 T: serde::Serialize + ?Sized,
371 {
372 ser::SerializeSeq::serialize_element(self, value)
373 }
374
375 fn end(self) -> Result<Self::Ok> {
376 ser::SerializeSeq::end(self)
377 }
378}
379
380impl<W, F> ser::SerializeTupleVariant for Compound<'_, W, F>
381where
382 W: io::Write,
383 F: Formatter,
384{
385 type Ok = ();
386 type Error = Error;
387
388 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
389 where
390 T: serde::Serialize + ?Sized,
391 {
392 ser::SerializeSeq::serialize_element(self, value)
393 }
394
395 fn end(self) -> Result<Self::Ok> {
396 match self.state {
397 State::Empty => {}
398 _ => self.ser.formatter.close(&mut self.ser.writer, b']')?,
399 }
400 self.ser.formatter.close(&mut self.ser.writer, b'}')
401 }
402}
403
404impl<W, F> ser::SerializeMap for Compound<'_, W, F>
405where
406 W: io::Write,
407 F: Formatter,
408{
409 type Ok = ();
410 type Error = Error;
411
412 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
413 where
414 T: serde::Serialize + ?Sized,
415 {
416 self.ser
417 .formatter
418 .comma(&mut self.ser.writer, self.state == State::First)?;
419 self.state = State::Rest;
420
421 key.serialize(MapKeySerializer { ser: self.ser })?;
422
423 self.ser.formatter.colon(&mut self.ser.writer)
424 }
425
426 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
427 where
428 T: serde::Serialize + ?Sized,
429 {
430 value.serialize(&mut *self.ser)
431 }
432
433 fn end(self) -> Result<Self::Ok> {
434 match self.state {
435 State::Empty => Ok(()),
436 _ => self.ser.formatter.close(&mut self.ser.writer, b'}'),
437 }
438 }
439}
440
441impl<W, F> ser::SerializeStruct for Compound<'_, W, F>
442where
443 W: io::Write,
444 F: Formatter,
445{
446 type Ok = ();
447 type Error = Error;
448
449 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
450 where
451 T: serde::Serialize + ?Sized,
452 {
453 ser::SerializeMap::serialize_entry(self, key, value)
454 }
455
456 fn end(self) -> Result<Self::Ok> {
457 ser::SerializeMap::end(self)
458 }
459}
460
461impl<W, F> ser::SerializeStructVariant for Compound<'_, W, F>
462where
463 W: io::Write,
464 F: Formatter,
465{
466 type Ok = ();
467 type Error = Error;
468
469 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
470 where
471 T: serde::Serialize + ?Sized,
472 {
473 ser::SerializeStruct::serialize_field(self, key, value)
474 }
475
476 fn end(self) -> Result<Self::Ok> {
477 match self.state {
478 State::Empty => {}
479 _ => self.ser.formatter.close(&mut self.ser.writer, b'}')?,
480 }
481 self.ser.formatter.close(&mut self.ser.writer, b'}')
482 }
483}
484
485struct MapKeySerializer<'a, W: 'a, F: 'a> {
486 ser: &'a mut Serializer<W, F>,
487}
488
489impl<W, F> ser::Serializer for MapKeySerializer<'_, W, F>
490where
491 W: io::Write,
492 F: Formatter,
493{
494 type Ok = ();
495 type Error = Error;
496
497 #[inline]
498 fn serialize_str(self, value: &str) -> Result<()> {
499 escape_key(&mut self.ser.writer, value)
500 }
501
502 type SerializeSeq = ser::Impossible<(), Error>;
503 type SerializeTuple = ser::Impossible<(), Error>;
504 type SerializeTupleStruct = ser::Impossible<(), Error>;
505 type SerializeTupleVariant = ser::Impossible<(), Error>;
506 type SerializeMap = ser::Impossible<(), Error>;
507 type SerializeStruct = ser::Impossible<(), Error>;
508 type SerializeStructVariant = ser::Impossible<(), Error>;
509
510 fn serialize_bool(self, _value: bool) -> Result<()> {
511 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
512 }
513
514 fn serialize_i8(self, _value: i8) -> Result<()> {
515 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
516 }
517
518 fn serialize_i16(self, _value: i16) -> Result<()> {
519 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
520 }
521
522 fn serialize_i32(self, _value: i32) -> Result<()> {
523 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
524 }
525
526 fn serialize_i64(self, _value: i64) -> Result<()> {
527 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
528 }
529
530 fn serialize_u8(self, _value: u8) -> Result<()> {
531 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
532 }
533
534 fn serialize_u16(self, _value: u16) -> Result<()> {
535 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
536 }
537
538 fn serialize_u32(self, _value: u32) -> Result<()> {
539 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
540 }
541
542 fn serialize_u64(self, _value: u64) -> Result<()> {
543 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
544 }
545
546 fn serialize_f32(self, _value: f32) -> Result<()> {
547 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
548 }
549
550 fn serialize_f64(self, _value: f64) -> Result<()> {
551 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
552 }
553
554 fn serialize_char(self, _value: char) -> Result<()> {
555 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
556 }
557
558 fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
559 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
560 }
561
562 fn serialize_unit(self) -> Result<()> {
563 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
564 }
565
566 fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
567 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
568 }
569
570 fn serialize_unit_variant(
571 self,
572 _name: &'static str,
573 _variant_index: u32,
574 _variant: &'static str,
575 ) -> Result<()> {
576 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
577 }
578
579 fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
580 where
581 T: ?Sized + ser::Serialize,
582 {
583 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
584 }
585
586 fn serialize_newtype_variant<T>(
587 self,
588 _name: &'static str,
589 _variant_index: u32,
590 _variant: &'static str,
591 _value: &T,
592 ) -> Result<()>
593 where
594 T: ?Sized + ser::Serialize,
595 {
596 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
597 }
598
599 fn serialize_none(self) -> Result<()> {
600 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
601 }
602
603 fn serialize_some<T>(self, _value: &T) -> Result<()>
604 where
605 T: ?Sized + ser::Serialize,
606 {
607 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
608 }
609
610 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeStruct> {
611 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
612 }
613
614 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
615 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
616 }
617
618 fn serialize_tuple_struct(
619 self,
620 _name: &'static str,
621 _len: usize,
622 ) -> Result<Self::SerializeTupleStruct> {
623 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
624 }
625
626 fn serialize_tuple_variant(
627 self,
628 _name: &'static str,
629 _variant_index: u32,
630 _variant: &'static str,
631 _len: usize,
632 ) -> Result<Self::SerializeTupleVariant> {
633 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
634 }
635
636 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
637 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
638 }
639
640 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
641 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
642 }
643
644 fn serialize_struct_variant(
645 self,
646 _name: &'static str,
647 _variant_index: u32,
648 _variant: &'static str,
649 _len: usize,
650 ) -> Result<Self::SerializeStructVariant> {
651 Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
652 }
653}
654
655pub trait Formatter {
657 fn open<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
659 where
660 W: io::Write;
661
662 fn comma<W>(&mut self, writer: &mut W, first: bool) -> Result<()>
664 where
665 W: io::Write;
666
667 fn colon<W>(&mut self, writer: &mut W) -> Result<()>
669 where
670 W: io::Write;
671
672 fn close<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
674 where
675 W: io::Write;
676
677 fn newline<W>(&mut self, writer: &mut W, add_indent: i32) -> Result<()>
679 where
680 W: io::Write;
681
682 fn start_value<W>(&mut self, writer: &mut W) -> Result<()>
684 where
685 W: io::Write;
686}
687
688struct HjsonFormatter<'a> {
689 current_indent: usize,
690 current_is_array: bool,
691 stack: Vec<bool>,
692 at_colon: bool,
693 indent: &'a [u8],
694 braces_same_line: bool,
695}
696
697impl Default for HjsonFormatter<'_> {
698 fn default() -> Self {
699 Self::new()
700 }
701}
702
703impl<'a> HjsonFormatter<'a> {
704 pub fn new() -> Self {
706 HjsonFormatter::with_indent(b" ")
707 }
708
709 pub fn with_indent(indent: &'a [u8]) -> Self {
711 HjsonFormatter {
712 current_indent: 0,
713 current_is_array: false,
714 stack: Vec::new(),
715 at_colon: false,
716 indent,
717 braces_same_line: true,
718 }
719 }
720}
721
722impl Formatter for HjsonFormatter<'_> {
723 fn open<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
724 where
725 W: io::Write,
726 {
727 if self.current_indent > 0 && !self.current_is_array && !self.braces_same_line {
728 self.newline(writer, 0)?;
729 } else {
730 self.start_value(writer)?;
731 }
732 self.current_indent += 1;
733 self.stack.push(self.current_is_array);
734 self.current_is_array = ch == b'[';
735 writer.write_all(&[ch]).map_err(From::from)
736 }
737
738 fn comma<W>(&mut self, writer: &mut W, first: bool) -> Result<()>
739 where
740 W: io::Write,
741 {
742 if !first {
743 writer.write_all(b",\n")?;
744 } else {
745 writer.write_all(b"\n")?;
746 }
747 indent(writer, self.current_indent, self.indent)
748 }
749
750 fn colon<W>(&mut self, writer: &mut W) -> Result<()>
751 where
752 W: io::Write,
753 {
754 self.at_colon = !self.braces_same_line;
755 writer
756 .write_all(if self.braces_same_line { b": " } else { b":" })
757 .map_err(From::from)
758 }
759
760 fn close<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
761 where
762 W: io::Write,
763 {
764 self.current_indent -= 1;
765 self.current_is_array = self.stack.pop().expect("Internal error: json parsing");
766 writer.write_all(b"\n")?;
767 indent(writer, self.current_indent, self.indent)?;
768 writer.write_all(&[ch]).map_err(From::from)
769 }
770
771 fn newline<W>(&mut self, writer: &mut W, add_indent: i32) -> Result<()>
772 where
773 W: io::Write,
774 {
775 self.at_colon = false;
776 writer.write_all(b"\n")?;
777 let ii = self.current_indent as i32 + add_indent;
778 indent(writer, if ii < 0 { 0 } else { ii as usize }, self.indent)
779 }
780
781 fn start_value<W>(&mut self, writer: &mut W) -> Result<()>
782 where
783 W: io::Write,
784 {
785 if self.at_colon {
786 self.at_colon = false;
787 writer.write_all(b" ")?
788 }
789 Ok(())
790 }
791}
792
793#[inline]
795pub fn escape_bytes<W>(wr: &mut W, bytes: &[u8]) -> Result<()>
796where
797 W: io::Write,
798{
799 wr.write_all(b"\"")?;
800
801 let mut start = 0;
802
803 for (i, byte) in bytes.iter().enumerate() {
804 let escaped = match *byte {
805 b'"' => b"\\\"",
806 b'\\' => b"\\\\",
807 b'\x08' => b"\\b",
808 b'\x0c' => b"\\f",
809 b'\n' => b"\\n",
810 b'\r' => b"\\r",
811 b'\t' => b"\\t",
812 _ => {
813 continue;
814 }
815 };
816
817 if start < i {
818 wr.write_all(&bytes[start..i])?;
819 }
820
821 wr.write_all(escaped)?;
822
823 start = i + 1;
824 }
825
826 if start != bytes.len() {
827 wr.write_all(&bytes[start..])?;
828 }
829
830 wr.write_all(b"\"")?;
831 Ok(())
832}
833
834#[inline]
836pub fn quote_str<W, F>(wr: &mut W, formatter: &mut F, value: &str) -> Result<()>
837where
838 W: io::Write,
839 F: Formatter,
840{
841 if value.is_empty() {
842 formatter.start_value(wr)?;
843 return escape_bytes(wr, value.as_bytes());
844 }
845
846 formatter.start_value(wr)?;
847 escape_bytes(wr, value.as_bytes())
848}
849
850#[inline]
852pub fn escape_key<W>(wr: &mut W, value: &str) -> Result<()>
853where
854 W: io::Write,
855{
856 escape_bytes(wr, value.as_bytes())
857}
858
859#[inline]
860fn escape_char<W>(wr: &mut W, value: char) -> Result<()>
861where
862 W: io::Write,
863{
864 escape_bytes(wr, value.encode_utf8(&mut [0; 4]).as_bytes())
865}
866
867fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> Result<()>
868where
869 W: io::Write,
870{
871 match value.classify() {
872 FpCategory::Nan | FpCategory::Infinite => wr.write_all(b"null")?,
873 _ => wr.write_all(fmt_small(ObviousFloat(value as f64)).as_bytes())?,
874 }
875
876 Ok(())
877}
878
879fn fmt_f64_or_null<W>(wr: &mut W, value: f64) -> Result<()>
880where
881 W: io::Write,
882{
883 match value.classify() {
884 FpCategory::Nan | FpCategory::Infinite => wr.write_all(b"null")?,
885 _ => wr.write_all(fmt_small(ObviousFloat(value)).as_bytes())?,
886 }
887
888 Ok(())
889}
890
891fn indent<W>(wr: &mut W, n: usize, s: &[u8]) -> Result<()>
892where
893 W: io::Write,
894{
895 for _ in 0..n {
896 wr.write_all(s)?;
897 }
898
899 Ok(())
900}
901
902fn fmt_small<N>(value: N) -> String
904where
905 N: Display + LowerExp,
906{
907 let f1 = value.to_string();
908 let f2 = format!("{value:e}");
909 if f1.len() <= f2.len() + 1 {
910 f1
911 } else if !f2.contains("e-") {
912 f2.replace('e', "e+")
913 } else {
914 f2
915 }
916}
917
918#[inline]
920pub fn to_writer<W, T>(writer: &mut W, value: &T) -> Result<()>
921where
922 W: io::Write,
923 T: ser::Serialize,
924{
925 let mut ser = Serializer::new(writer);
926 value.serialize(&mut ser)?;
927 Ok(())
928}
929
930#[inline]
932pub fn to_writer_with_tab_indentation<W, T>(writer: &mut W, value: &T, tabs: usize) -> Result<()>
933where
934 W: io::Write,
935 T: ser::Serialize,
936{
937 let indent_string = "\t".repeat(tabs);
938 let mut ser = Serializer::with_indent(writer, indent_string.as_bytes());
939 value.serialize(&mut ser)?;
940 Ok(())
941}
942
943#[inline]
945pub fn to_writer_with_indent<W, T>(writer: &mut W, value: &T, indent: usize) -> Result<()>
946where
947 W: io::Write,
948 T: ser::Serialize,
949{
950 let indent_string = " ".repeat(indent);
951 let mut ser = Serializer::with_indent(writer, indent_string.as_bytes());
952 value.serialize(&mut ser)?;
953 Ok(())
954}
955
956#[inline]
958pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>
959where
960 T: ser::Serialize,
961{
962 let mut writer = Vec::with_capacity(128);
965 to_writer(&mut writer, value)?;
966 Ok(writer)
967}
968
969#[inline]
971pub fn to_vec_with_tab_indentation<T>(value: &T, tabs: usize) -> Result<Vec<u8>>
972where
973 T: ser::Serialize,
974{
975 let mut writer = Vec::with_capacity(128);
978 to_writer_with_tab_indentation(&mut writer, value, tabs)?;
979 Ok(writer)
980}
981
982#[inline]
984pub fn to_vec_with_indent<T>(value: &T, indent: usize) -> Result<Vec<u8>>
985where
986 T: ser::Serialize,
987{
988 let mut writer = Vec::with_capacity(128);
991 to_writer_with_indent(&mut writer, value, indent)?;
992 Ok(writer)
993}
994
995#[inline]
997pub fn to_string<T>(value: &T) -> Result<String>
998where
999 T: ser::Serialize,
1000{
1001 let vec = to_vec(value)?;
1002 let string = String::from_utf8(vec)?;
1003 Ok(string)
1004}
1005
1006#[inline]
1008pub fn to_string_with_indent<T>(value: &T, indent: usize) -> Result<String>
1009where
1010 T: ser::Serialize,
1011{
1012 let vec = to_vec_with_indent(value, indent)?;
1013 let string = String::from_utf8(vec)?;
1014 Ok(string)
1015}
1016
1017#[inline]
1019pub fn to_string_with_tab_indentation<T>(value: &T, tabs: usize) -> Result<String>
1020where
1021 T: ser::Serialize,
1022{
1023 let vec = to_vec_with_tab_indentation(value, tabs)?;
1024 let string = String::from_utf8(vec)?;
1025 Ok(string)
1026}
1027
1028#[inline]
1031pub fn to_string_raw<T>(value: &T) -> Result<String>
1032where
1033 T: ser::Serialize,
1034{
1035 let result = serde_json::to_string(value);
1036 match result {
1037 Ok(result_string) => Ok(result_string),
1038 Err(error) => Err(Error::Io(std::io::Error::from(error))),
1039 }
1040}