1use alloc::vec::Vec;
6use core::str;
7
8use deser_core::de::{self, DeserializeDriver};
9use deser_core::ext::{Raw, RawFormat, RawFormatId, RawFormatInfo, TextRawFormat};
10use deser_core::ser::SerializeRef;
11use deser_core::{Atom, Error, State, Text};
12
13use crate::de::Deserializer;
14use crate::parser::Cursor;
15
16pub struct Json5;
18
19pub type RawJson5<'a> = Raw<'a, Json5>;
25
26pub(crate) type Dialect = Json5;
27
28const NAME: &str = "json5";
29
30pub(crate) static ID: RawFormatId = RawFormatId::new(NAME, true);
36
37static FORMAT: RawFormatInfo = {
39 let mut info = RawFormatInfo::new(&ID, replay, encode, fallback);
40 info.set_data(&SCANNER);
41 info
42};
43
44pub(crate) struct Scanner(pub(crate) ScanFn);
49
50type ScanFn =
52 for<'i> fn(&mut Cursor<'i>, &mut Vec<u8>, bool, usize, &mut State) -> Result<bool, Error>;
53
54static SCANNER: Scanner = Scanner(crate::parser::skip_raw);
56
57impl RawFormat for Dialect {
58 #[inline(always)]
59 fn info() -> &'static RawFormatInfo {
60 &FORMAT
61 }
62}
63
64unsafe impl TextRawFormat for Dialect {}
67
68fn replay<'de>(input: &'de [u8], driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
70 let input = unsafe { str::from_utf8_unchecked(input) };
72 de::Deserializer::drive(&mut Deserializer::from_str(input), driver)
73}
74
75fn encode(value: SerializeRef<'_>) -> Result<Vec<u8>, Error> {
77 crate::to_string(&value).map(|text| text.into_bytes())
78}
79
80fn fallback(input: &[u8]) -> Atom<'_> {
84 match input {
85 b"null" => Atom::Null,
86 b"true" => Atom::Bool(true),
87 b"false" => Atom::Bool(false),
88 _ => Atom::Str(Text::borrowed(unsafe { str::from_utf8_unchecked(input) })),
90 }
91}