Skip to main content

serde_reflection/
json_converter.rs

1// Copyright (c) Zefchain Labs, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Dynamic conversion between binary-serialized data and JSON values.
5//!
6//! This module provides dynamic conversion between binary-serialized data and JSON values.
7//! This is useful when you need to inspect or manipulate serialized data without having
8//! access to the original Rust types at compile time.
9//!
10//! # Example
11//!
12//! ```rust
13//! use bincode::Options;
14//! use serde::{Serialize, Deserialize};
15//! use serde_reflection::{Tracer, TracerConfig, Samples};
16//! use serde_reflection::json_converter::{DeserializationContext, SerializationContext, EmptyEnvironment};
17//! use serde_json::json;
18//!
19//! #[derive(Serialize, Deserialize)]
20//! struct Point {
21//!     x: i32,
22//!     y: i32,
23//! }
24//!
25//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
26//! // Use tracer to extract the format
27//! let mut tracer = Tracer::new(TracerConfig::default());
28//! let (format, _) = tracer
29//!     .trace_type::<Point>(&Samples::new())?;
30//! let registry = tracer.registry()?;
31//!
32//! // Serialize with bincode
33//! let config = bincode::DefaultOptions::new();
34//! let point = Point { x: 10, y: 20 };
35//! let encoded = config.serialize(&point)?;
36//!
37//! // Deserialize to JSON using DeserializationContext
38//! let mut deserializer = bincode::Deserializer::from_slice(&encoded, config);
39//! let context = DeserializationContext {
40//!     format: format.clone(),
41//!     registry: &registry,
42//!     environment: &EmptyEnvironment,
43//! };
44//! let value: serde_json::Value = serde::de::DeserializeSeed::deserialize(context, &mut deserializer)?;
45//! assert_eq!(value["x"], json!(10));
46//! assert_eq!(value["y"], json!(20));
47//!
48//! // Serialize JSON back to binary using SerializationContext
49//! let context = SerializationContext {
50//!     value: &value,
51//!     format: &format,
52//!     registry: &registry,
53//!     environment: &EmptyEnvironment,
54//! };
55//! let bytes = config.serialize(&context)?;
56//! assert_eq!(encoded, bytes);
57//! # Ok(())
58//! # }
59//! ```
60//!
61//! This approach is particularly useful for cryptographic applications where you need to
62//! compute hashes of JSON values using a binary format like [BCS](https://github.com/diem/bcs).
63
64use crate::{ContainerFormat, Format, Named, Registry, VariantFormat};
65use serde::de::{DeserializeSeed, MapAccess, SeqAccess, Visitor};
66use serde::ser::{
67    SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
68    SerializeTupleStruct, SerializeTupleVariant,
69};
70use serde::{Deserialize, Deserializer, Serialize, Serializer};
71use serde_json::{Number, Value};
72use std::collections::BTreeMap;
73
74/// A deserialization context to create a JSON value from a serialized object in a dynamic
75/// format.
76pub struct DeserializationContext<'a, E> {
77    /// The format of the main value.
78    pub format: Format,
79    /// The registry of container formats.
80    pub registry: &'a Registry,
81    /// The environment containing external parsers.
82    pub environment: &'a E,
83}
84
85use once_cell::sync::Lazy;
86use std::{collections::HashSet, sync::Mutex};
87
88static GLOBAL_STRING_SET: Lazy<Mutex<HashSet<&'static str>>> =
89    Lazy::new(|| Mutex::new(HashSet::new()));
90
91static GLOBAL_FIELDS_SET: Lazy<Mutex<HashSet<&'static [&'static str]>>> =
92    Lazy::new(|| Mutex::new(HashSet::new()));
93
94/// The requirement for an `environment` that manages a symbol table.
95pub trait SymbolTableEnvironment {
96    fn get_static_name(&self, name: &str) -> &'static str {
97        let mut set = GLOBAL_STRING_SET.lock().unwrap();
98        // TODO: use https://github.com/rust-lang/rust/issues/60896 when available
99        if let Some(value) = set.get(name) {
100            value
101        } else {
102            set.insert(name.to_string().leak());
103            set.get(name).unwrap()
104        }
105    }
106
107    fn get_static_fields<'a>(
108        &self,
109        fields: impl IntoIterator<Item = &'a str>,
110    ) -> &'static [&'static str] {
111        let fields = fields
112            .into_iter()
113            .map(|name| self.get_static_name(name))
114            .collect::<Vec<_>>();
115        let mut set = GLOBAL_FIELDS_SET.lock().unwrap();
116        // TODO: use https://github.com/rust-lang/rust/issues/60896 when available
117        if let Some(value) = set.get(fields.as_slice()) {
118            value
119        } else {
120            set.insert(fields.to_vec().leak());
121            set.get(fields.as_slice()).unwrap()
122        }
123    }
124}
125
126/// The requirement for the `environment` objects to help with Deserialize.
127pub trait DeserializationEnvironment<'de>: SymbolTableEnvironment {
128    /// Deserialize a value of an external type `name`.
129    fn deserialize<D>(&self, name: String, deserializer: D) -> Result<Value, String>
130    where
131        D: Deserializer<'de>;
132}
133
134pub struct EmptyEnvironment;
135
136impl SymbolTableEnvironment for EmptyEnvironment {}
137
138impl<'de> DeserializationEnvironment<'de> for EmptyEnvironment {
139    fn deserialize<D>(&self, name: String, _deserializer: D) -> Result<Value, String>
140    where
141        D: Deserializer<'de>,
142    {
143        Err(format!("No external definition available for {name}"))
144    }
145}
146
147impl<'a, 'de, E> DeserializeSeed<'de> for DeserializationContext<'a, E>
148where
149    E: DeserializationEnvironment<'de>,
150{
151    type Value = Value;
152
153    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
154    where
155        D: Deserializer<'de>,
156    {
157        use Format::*;
158
159        match self.format {
160            Variable(_) => Err(<D::Error as serde::de::Error>::custom(
161                "Required formats cannot contain variables",
162            )),
163            TypeName(name) => {
164                if let Some(container_format) = self.registry.get(&name) {
165                    // Process the container format by deserializing according to its structure
166                    deserialize_container_format(
167                        &name,
168                        container_format,
169                        self.registry,
170                        self.environment,
171                        deserializer,
172                    )
173                } else {
174                    Ok(self
175                        .environment
176                        .deserialize(name, deserializer)
177                        .map_err(<D::Error as serde::de::Error>::custom)?)
178                }
179            }
180            Unit => Ok(Value::Null),
181            Bool => {
182                let value = bool::deserialize(deserializer)?;
183                Ok(Value::Bool(value))
184            }
185            I8 => {
186                let value = i8::deserialize(deserializer)?;
187                Ok(Value::Number(Number::from(value)))
188            }
189            I16 => {
190                let value = i16::deserialize(deserializer)?;
191                Ok(Value::Number(Number::from(value)))
192            }
193            I32 => {
194                let value = i32::deserialize(deserializer)?;
195                Ok(Value::Number(Number::from(value)))
196            }
197            I64 => {
198                let value = i64::deserialize(deserializer)?;
199                Ok(Value::Number(Number::from(value)))
200            }
201            I128 => {
202                let value = i128::deserialize(deserializer)?;
203                // i128 is too large for JSON Number, so we convert to i64 if possible
204                // or use a string representation
205                if let Ok(small_value) = i64::try_from(value) {
206                    Ok(Value::Number(Number::from(small_value)))
207                } else {
208                    Ok(Value::String(value.to_string()))
209                }
210            }
211            U8 => {
212                let value = u8::deserialize(deserializer)?;
213                Ok(Value::Number(Number::from(value)))
214            }
215            U16 => {
216                let value = u16::deserialize(deserializer)?;
217                Ok(Value::Number(Number::from(value)))
218            }
219            U32 => {
220                let value = u32::deserialize(deserializer)?;
221                Ok(Value::Number(Number::from(value)))
222            }
223            U64 => {
224                let value = u64::deserialize(deserializer)?;
225                Ok(Value::Number(Number::from(value)))
226            }
227            U128 => {
228                let value = u128::deserialize(deserializer)?;
229                // u128 is too large for JSON Number, so we convert to u64 if possible
230                // or use a string representation
231                if let Ok(small_value) = u64::try_from(value) {
232                    Ok(Value::Number(Number::from(small_value)))
233                } else {
234                    Ok(Value::String(value.to_string()))
235                }
236            }
237            F32 => {
238                let value = f32::deserialize(deserializer)?;
239                Number::from_f64(value as f64)
240                    .map(Value::Number)
241                    .ok_or_else(|| <D::Error as serde::de::Error>::custom("Invalid f32 value"))
242            }
243            F64 => {
244                let value = f64::deserialize(deserializer)?;
245                Number::from_f64(value)
246                    .map(Value::Number)
247                    .ok_or_else(|| <D::Error as serde::de::Error>::custom("Invalid f64 value"))
248            }
249            Char => {
250                let value = char::deserialize(deserializer)?;
251                Ok(Value::String(value.to_string()))
252            }
253            Str => {
254                let value = String::deserialize(deserializer)?;
255                Ok(Value::String(value))
256            }
257            Bytes => {
258                let value = Vec::<u8>::deserialize(deserializer)?;
259                Ok(Value::Array(
260                    value
261                        .into_iter()
262                        .map(|b| Value::Number(Number::from(b)))
263                        .collect(),
264                ))
265            }
266            Option(format) => {
267                let visitor = OptionVisitor {
268                    format: *format,
269                    registry: self.registry,
270                    environment: self.environment,
271                };
272                deserializer.deserialize_option(visitor)
273            }
274            Seq(format) => {
275                let visitor = SeqVisitor {
276                    format: *format,
277                    registry: self.registry,
278                    environment: self.environment,
279                };
280                deserializer.deserialize_seq(visitor)
281            }
282            Map { key, value } => {
283                let visitor = MapVisitor {
284                    key_format: *key,
285                    value_format: *value,
286                    registry: self.registry,
287                    environment: self.environment,
288                };
289                deserializer.deserialize_map(visitor)
290            }
291            Tuple(formats) => {
292                let visitor = TupleVisitor {
293                    formats,
294                    registry: self.registry,
295                    environment: self.environment,
296                };
297                deserializer.deserialize_tuple(visitor.formats.len(), visitor)
298            }
299            TupleArray { content, size } => {
300                let visitor = TupleArrayVisitor {
301                    format: *content,
302                    size,
303                    registry: self.registry,
304                    environment: self.environment,
305                };
306                deserializer.deserialize_tuple(visitor.size, visitor)
307            }
308        }
309    }
310}
311
312struct OptionVisitor<'a, E> {
313    format: Format,
314    registry: &'a Registry,
315    environment: &'a E,
316}
317
318impl<'a, 'de, E> Visitor<'de> for OptionVisitor<'a, E>
319where
320    E: DeserializationEnvironment<'de>,
321{
322    type Value = Value;
323
324    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
325        formatter.write_str("an optional value")
326    }
327
328    fn visit_none<Err>(self) -> Result<Self::Value, Err>
329    where
330        Err: serde::de::Error,
331    {
332        Ok(Value::Null)
333    }
334
335    fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
336    where
337        D: Deserializer<'de>,
338    {
339        let context = DeserializationContext {
340            format: self.format,
341            registry: self.registry,
342            environment: self.environment,
343        };
344        context.deserialize(deserializer)
345    }
346
347    fn visit_unit<Err>(self) -> Result<Self::Value, Err>
348    where
349        Err: serde::de::Error,
350    {
351        Ok(Value::Null)
352    }
353}
354
355struct SeqVisitor<'a, E> {
356    format: Format,
357    registry: &'a Registry,
358    environment: &'a E,
359}
360
361impl<'a, 'de, E> Visitor<'de> for SeqVisitor<'a, E>
362where
363    E: DeserializationEnvironment<'de>,
364{
365    type Value = Value;
366
367    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
368        formatter.write_str("a sequence")
369    }
370
371    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
372    where
373        A: SeqAccess<'de>,
374    {
375        let mut values = Vec::new();
376        while let Some(value) = seq.next_element_seed(DeserializationContext {
377            format: self.format.clone(),
378            registry: self.registry,
379            environment: self.environment,
380        })? {
381            values.push(value);
382        }
383        Ok(Value::Array(values))
384    }
385}
386
387struct MapVisitor<'a, E> {
388    key_format: Format,
389    value_format: Format,
390    registry: &'a Registry,
391    environment: &'a E,
392}
393
394impl<'a, 'de, E> Visitor<'de> for MapVisitor<'a, E>
395where
396    E: DeserializationEnvironment<'de>,
397{
398    type Value = Value;
399
400    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
401        formatter.write_str("a map")
402    }
403
404    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
405    where
406        A: MapAccess<'de>,
407    {
408        let mut object = serde_json::Map::new();
409        while let Some((key, value)) = map.next_entry_seed(
410            DeserializationContext {
411                format: self.key_format.clone(),
412                registry: self.registry,
413                environment: self.environment,
414            },
415            DeserializationContext {
416                format: self.value_format.clone(),
417                registry: self.registry,
418                environment: self.environment,
419            },
420        )? {
421            // Convert the key Value to a String
422            let key_string = match key {
423                Value::String(s) => s,
424                Value::Number(n) => n.to_string(),
425                Value::Bool(b) => b.to_string(),
426                _ => {
427                    return Err(serde::de::Error::custom(
428                        "Map keys must be strings, numbers, or booleans",
429                    ))
430                }
431            };
432            object.insert(key_string, value);
433        }
434        Ok(Value::Object(object))
435    }
436}
437
438struct TupleVisitor<'a, E> {
439    formats: Vec<Format>,
440    registry: &'a Registry,
441    environment: &'a E,
442}
443
444impl<'a, 'de, E> Visitor<'de> for TupleVisitor<'a, E>
445where
446    E: DeserializationEnvironment<'de>,
447{
448    type Value = Value;
449
450    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
451        formatter.write_str("a tuple")
452    }
453
454    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
455    where
456        A: SeqAccess<'de>,
457    {
458        let mut values = Vec::new();
459        for format in self.formats {
460            match seq.next_element_seed(DeserializationContext {
461                format,
462                registry: self.registry,
463                environment: self.environment,
464            })? {
465                Some(value) => values.push(value),
466                None => {
467                    return Err(serde::de::Error::custom(
468                        "Tuple has fewer elements than expected",
469                    ))
470                }
471            }
472        }
473        Ok(Value::Array(values))
474    }
475}
476
477struct TupleArrayVisitor<'a, E> {
478    format: Format,
479    size: usize,
480    registry: &'a Registry,
481    environment: &'a E,
482}
483
484impl<'a, 'de, E> Visitor<'de> for TupleArrayVisitor<'a, E>
485where
486    E: DeserializationEnvironment<'de>,
487{
488    type Value = Value;
489
490    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
491        formatter.write_str("a tuple array")
492    }
493
494    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
495    where
496        A: SeqAccess<'de>,
497    {
498        let mut values = Vec::new();
499        for _ in 0..self.size {
500            match seq.next_element_seed(DeserializationContext {
501                format: self.format.clone(),
502                registry: self.registry,
503                environment: self.environment,
504            })? {
505                Some(value) => values.push(value),
506                None => {
507                    return Err(serde::de::Error::custom(
508                        "Tuple array has fewer elements than expected",
509                    ))
510                }
511            }
512        }
513        Ok(Value::Array(values))
514    }
515}
516
517// Helper function to deserialize a container format
518fn deserialize_container_format<'a, 'de, E, D>(
519    name: &str,
520    container_format: &'a ContainerFormat,
521    registry: &'a Registry,
522    environment: &'a E,
523    deserializer: D,
524) -> Result<Value, D::Error>
525where
526    E: DeserializationEnvironment<'de>,
527    D: Deserializer<'de>,
528{
529    use ContainerFormat::*;
530
531    match container_format {
532        UnitStruct => {
533            // Unit structs deserialize as null
534            deserializer.deserialize_unit(UnitStructVisitor)
535        }
536        NewTypeStruct(format) => {
537            // NewType structs unwrap to their inner value
538            let name = environment.get_static_name(name);
539            let visitor = NewTypeStructVisitor {
540                format: (**format).clone(),
541                registry,
542                environment,
543            };
544            deserializer.deserialize_newtype_struct(name, visitor)
545        }
546        TupleStruct(formats) => {
547            // Tuple structs deserialize as sequences
548            let visitor = TupleStructVisitor {
549                formats: formats.clone(),
550                registry,
551                environment,
552            };
553            deserializer.deserialize_tuple(formats.len(), visitor)
554        }
555        Struct(fields) => {
556            // Named structs deserialize as maps
557            let name = environment.get_static_name(name);
558            let static_fields =
559                environment.get_static_fields(fields.iter().map(|f| f.name.as_str()));
560            let visitor = StructVisitor {
561                fields: fields.clone(),
562                registry,
563                environment,
564            };
565            deserializer.deserialize_struct(name, static_fields, visitor)
566        }
567        Enum(variants) => {
568            // Enums need special handling
569            let name = environment.get_static_name(name);
570            let static_fields =
571                environment.get_static_fields(variants.iter().map(|(_, v)| v.name.as_str()));
572            let visitor = EnumVisitor {
573                variants: variants.clone(),
574                registry,
575                environment,
576            };
577            deserializer.deserialize_enum(name, static_fields, visitor)
578        }
579    }
580}
581
582struct UnitStructVisitor;
583
584impl<'de> Visitor<'de> for UnitStructVisitor {
585    type Value = Value;
586
587    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
588        formatter.write_str("a unit struct")
589    }
590
591    fn visit_unit<E>(self) -> Result<Self::Value, E>
592    where
593        E: serde::de::Error,
594    {
595        Ok(Value::Null)
596    }
597}
598
599struct NewTypeStructVisitor<'a, E> {
600    format: Format,
601    registry: &'a Registry,
602    environment: &'a E,
603}
604
605impl<'a, 'de, E> Visitor<'de> for NewTypeStructVisitor<'a, E>
606where
607    E: DeserializationEnvironment<'de>,
608{
609    type Value = Value;
610
611    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
612        formatter.write_str("a newtype struct")
613    }
614
615    fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
616    where
617        D: Deserializer<'de>,
618    {
619        let context = DeserializationContext {
620            format: self.format,
621            registry: self.registry,
622            environment: self.environment,
623        };
624        context.deserialize(deserializer)
625    }
626}
627
628struct TupleStructVisitor<'a, E> {
629    formats: Vec<Format>,
630    registry: &'a Registry,
631    environment: &'a E,
632}
633
634impl<'a, 'de, E> Visitor<'de> for TupleStructVisitor<'a, E>
635where
636    E: DeserializationEnvironment<'de>,
637{
638    type Value = Value;
639
640    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
641        formatter.write_str("a tuple struct")
642    }
643
644    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
645    where
646        A: SeqAccess<'de>,
647    {
648        let mut values = Vec::new();
649        for format in self.formats {
650            match seq.next_element_seed(DeserializationContext {
651                format,
652                registry: self.registry,
653                environment: self.environment,
654            })? {
655                Some(value) => values.push(value),
656                None => {
657                    return Err(serde::de::Error::custom(
658                        "Tuple struct has fewer elements than expected",
659                    ))
660                }
661            }
662        }
663        Ok(Value::Array(values))
664    }
665}
666
667struct StructVisitor<'a, E> {
668    fields: Vec<Named<Format>>,
669    registry: &'a Registry,
670    environment: &'a E,
671}
672
673impl<'a, 'de, E> Visitor<'de> for StructVisitor<'a, E>
674where
675    E: DeserializationEnvironment<'de>,
676{
677    type Value = Value;
678
679    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
680        formatter.write_str("a struct")
681    }
682
683    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
684    where
685        A: SeqAccess<'de>,
686    {
687        let mut object = serde_json::Map::new();
688        for field in self.fields {
689            match seq.next_element_seed(DeserializationContext {
690                format: field.value,
691                registry: self.registry,
692                environment: self.environment,
693            })? {
694                Some(value) => {
695                    object.insert(field.name, value);
696                }
697                None => {
698                    return Err(serde::de::Error::custom(
699                        "Struct has fewer fields than expected",
700                    ))
701                }
702            }
703        }
704        Ok(Value::Object(object))
705    }
706
707    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
708    where
709        A: MapAccess<'de>,
710    {
711        let mut object = serde_json::Map::new();
712        let fields_map: BTreeMap<_, _> = self
713            .fields
714            .into_iter()
715            .map(|f| (f.name.clone(), f.value))
716            .collect();
717
718        while let Some(key) = map.next_key::<String>()? {
719            if let Some(format) = fields_map.get(&key) {
720                let value = map.next_value_seed(DeserializationContext {
721                    format: format.clone(),
722                    registry: self.registry,
723                    environment: self.environment,
724                })?;
725                object.insert(key, value);
726            } else {
727                // Skip unknown fields
728                map.next_value::<serde::de::IgnoredAny>()?;
729            }
730        }
731        Ok(Value::Object(object))
732    }
733}
734
735struct EnumVisitor<'a, E> {
736    variants: BTreeMap<u32, Named<VariantFormat>>,
737    registry: &'a Registry,
738    environment: &'a E,
739}
740
741impl<'a, 'de, E> Visitor<'de> for EnumVisitor<'a, E>
742where
743    E: DeserializationEnvironment<'de>,
744{
745    type Value = Value;
746
747    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
748        formatter.write_str("an enum")
749    }
750
751    fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
752    where
753        A: serde::de::EnumAccess<'de>,
754    {
755        // Create a custom deserializer for the variant identifier that can handle
756        // both string names (e.g., JSON) and integer indices (e.g., bincode)
757        let variant_visitor = VariantIdentifierVisitor {
758            variants: &self.variants,
759        };
760
761        let (variant_info, variant_data) = data.variant_seed(variant_visitor)?;
762        let (variant_name, variant_format) = variant_info;
763
764        let variant_value = deserialize_variant_format(
765            &variant_format.value,
766            self.registry,
767            self.environment,
768            variant_data,
769        )?;
770
771        // Return a JSON object with the variant name as key
772        let mut object = serde_json::Map::new();
773        object.insert(variant_name, variant_value);
774        Ok(Value::Object(object))
775    }
776}
777
778struct VariantIdentifierVisitor<'a> {
779    variants: &'a BTreeMap<u32, Named<VariantFormat>>,
780}
781
782impl<'de> serde::de::DeserializeSeed<'de> for VariantIdentifierVisitor<'_> {
783    type Value = (String, Named<VariantFormat>);
784
785    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
786    where
787        D: serde::Deserializer<'de>,
788    {
789        deserializer.deserialize_identifier(self)
790    }
791}
792
793impl<'de> Visitor<'de> for VariantIdentifierVisitor<'_> {
794    type Value = (String, Named<VariantFormat>);
795
796    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
797        formatter.write_str("variant identifier")
798    }
799
800    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
801    where
802        E: serde::de::Error,
803    {
804        // Handle integer variant indices (e.g., bincode)
805        let variant = self
806            .variants
807            .get(&(value as u32))
808            .ok_or_else(|| serde::de::Error::custom(format!("Unknown variant index: {}", value)))?;
809        Ok((variant.name.clone(), variant.clone()))
810    }
811
812    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
813    where
814        E: serde::de::Error,
815    {
816        // Handle string variant names (e.g., JSON)
817        let variant = self
818            .variants
819            .values()
820            .find(|v| v.name == value)
821            .ok_or_else(|| serde::de::Error::custom(format!("Unknown variant: {}", value)))?;
822        Ok((variant.name.clone(), variant.clone()))
823    }
824
825    fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
826    where
827        E: serde::de::Error,
828    {
829        // Handle byte string variant names
830        let value_str = std::str::from_utf8(value)
831            .map_err(|_| serde::de::Error::custom("Invalid UTF-8 in variant name"))?;
832        self.visit_str(value_str)
833    }
834}
835
836fn deserialize_variant_format<'a, 'de, E, A>(
837    variant_format: &VariantFormat,
838    registry: &'a Registry,
839    environment: &'a E,
840    variant_data: A,
841) -> Result<Value, A::Error>
842where
843    E: DeserializationEnvironment<'de>,
844    A: serde::de::VariantAccess<'de>,
845{
846    use VariantFormat::*;
847
848    match variant_format {
849        Variable(_) => Err(serde::de::Error::custom(
850            "Variant format cannot contain variables",
851        )),
852        Unit => {
853            variant_data.unit_variant()?;
854            Ok(Value::Null)
855        }
856        NewType(format) => {
857            let context = DeserializationContext {
858                format: (**format).clone(),
859                registry,
860                environment,
861            };
862            variant_data.newtype_variant_seed(context)
863        }
864        Tuple(formats) => {
865            let visitor = TupleVariantVisitor {
866                formats: formats.clone(),
867                registry,
868                environment,
869            };
870            variant_data.tuple_variant(formats.len(), visitor)
871        }
872        Struct(fields) => {
873            let static_fields =
874                environment.get_static_fields(fields.iter().map(|v| v.name.as_str()));
875            let visitor = StructVariantVisitor {
876                fields: fields.clone(),
877                registry,
878                environment,
879            };
880            variant_data.struct_variant(static_fields, visitor)
881        }
882    }
883}
884
885struct TupleVariantVisitor<'a, E> {
886    formats: Vec<Format>,
887    registry: &'a Registry,
888    environment: &'a E,
889}
890
891impl<'a, 'de, E> Visitor<'de> for TupleVariantVisitor<'a, E>
892where
893    E: DeserializationEnvironment<'de>,
894{
895    type Value = Value;
896
897    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
898        formatter.write_str("a tuple variant")
899    }
900
901    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
902    where
903        A: SeqAccess<'de>,
904    {
905        let mut values = Vec::new();
906        for format in self.formats {
907            match seq.next_element_seed(DeserializationContext {
908                format,
909                registry: self.registry,
910                environment: self.environment,
911            })? {
912                Some(value) => values.push(value),
913                None => {
914                    return Err(serde::de::Error::custom(
915                        "Tuple variant has fewer elements than expected",
916                    ))
917                }
918            }
919        }
920        Ok(Value::Array(values))
921    }
922}
923
924struct StructVariantVisitor<'a, E> {
925    fields: Vec<Named<Format>>,
926    registry: &'a Registry,
927    environment: &'a E,
928}
929
930impl<'a, 'de, E> Visitor<'de> for StructVariantVisitor<'a, E>
931where
932    E: DeserializationEnvironment<'de>,
933{
934    type Value = Value;
935
936    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
937        formatter.write_str("a struct variant")
938    }
939
940    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
941    where
942        A: SeqAccess<'de>,
943    {
944        let mut object = serde_json::Map::new();
945        for field in self.fields {
946            match seq.next_element_seed(DeserializationContext {
947                format: field.value,
948                registry: self.registry,
949                environment: self.environment,
950            })? {
951                Some(value) => {
952                    object.insert(field.name, value);
953                }
954                None => {
955                    return Err(serde::de::Error::custom(
956                        "Struct variant has fewer fields than expected",
957                    ))
958                }
959            }
960        }
961        Ok(Value::Object(object))
962    }
963
964    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
965    where
966        A: MapAccess<'de>,
967    {
968        let mut object = serde_json::Map::new();
969        let fields_map: BTreeMap<_, _> = self
970            .fields
971            .into_iter()
972            .map(|f| (f.name.clone(), f.value))
973            .collect();
974
975        while let Some(key) = map.next_key::<String>()? {
976            if let Some(format) = fields_map.get(&key) {
977                let value = map.next_value_seed(DeserializationContext {
978                    format: format.clone(),
979                    registry: self.registry,
980                    environment: self.environment,
981                })?;
982                object.insert(key, value);
983            } else {
984                // Skip unknown fields
985                map.next_value::<serde::de::IgnoredAny>()?;
986            }
987        }
988        Ok(Value::Object(object))
989    }
990}
991
992/// A serialization context to convert a JSON value to a serialized object in a dynamic format.
993pub struct SerializationContext<'a, E> {
994    /// The JSON value to serialize.
995    pub value: &'a Value,
996    /// The format to serialize to.
997    pub format: &'a Format,
998    /// The registry of container formats.
999    pub registry: &'a Registry,
1000    /// The environment containing external serializers.
1001    pub environment: &'a E,
1002}
1003
1004/// The requirement for the `environment` object for serialization.
1005pub trait SerializationEnvironment: SymbolTableEnvironment {
1006    /// Serialize a value of an external type `name`.
1007    fn serialize<S>(&self, name: &str, value: &Value, serializer: S) -> Result<S::Ok, S::Error>
1008    where
1009        S: Serializer;
1010}
1011
1012impl SerializationEnvironment for EmptyEnvironment {
1013    fn serialize<S>(&self, name: &str, _value: &Value, _serializer: S) -> Result<S::Ok, S::Error>
1014    where
1015        S: Serializer,
1016    {
1017        Err(serde::ser::Error::custom(format!(
1018            "No external serializer available for {name}"
1019        )))
1020    }
1021}
1022
1023impl<'a, E> Serialize for SerializationContext<'a, E>
1024where
1025    E: SerializationEnvironment,
1026{
1027    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1028    where
1029        S: Serializer,
1030    {
1031        use Format::*;
1032
1033        match self.format {
1034            Variable(_) => Err(serde::ser::Error::custom(
1035                "Required formats cannot contain variables",
1036            )),
1037            TypeName(name) => {
1038                if let Some(container_format) = self.registry.get(name) {
1039                    serialize_container_format(
1040                        name,
1041                        container_format,
1042                        self.value,
1043                        self.registry,
1044                        self.environment,
1045                        serializer,
1046                    )
1047                } else {
1048                    self.environment.serialize(name, self.value, serializer)
1049                }
1050            }
1051            Unit => serializer.serialize_unit(),
1052            Bool => match self.value {
1053                Value::Bool(b) => serializer.serialize_bool(*b),
1054                _ => Err(serde::ser::Error::custom("Expected bool value")),
1055            },
1056            I8 => serialize_integer::<S, i8>(self.value, serializer),
1057            I16 => serialize_integer::<S, i16>(self.value, serializer),
1058            I32 => serialize_integer::<S, i32>(self.value, serializer),
1059            I64 => serialize_integer::<S, i64>(self.value, serializer),
1060            I128 => serialize_integer_or_string::<S, i128>(self.value, serializer),
1061            U8 => serialize_integer::<S, u8>(self.value, serializer),
1062            U16 => serialize_integer::<S, u16>(self.value, serializer),
1063            U32 => serialize_integer::<S, u32>(self.value, serializer),
1064            U64 => serialize_integer::<S, u64>(self.value, serializer),
1065            U128 => serialize_integer_or_string::<S, u128>(self.value, serializer),
1066            F32 => serialize_f32(self.value, serializer),
1067            F64 => serialize_f64(self.value, serializer),
1068            Char => match self.value {
1069                Value::String(s) => {
1070                    let mut chars = s.chars();
1071                    if let Some(c) = chars.next() {
1072                        if chars.next().is_none() {
1073                            serializer.serialize_char(c)
1074                        } else {
1075                            Err(serde::ser::Error::custom(
1076                                "Expected single character string",
1077                            ))
1078                        }
1079                    } else {
1080                        Err(serde::ser::Error::custom("Expected non-empty string"))
1081                    }
1082                }
1083                _ => Err(serde::ser::Error::custom("Expected string for char")),
1084            },
1085            Str => match self.value {
1086                Value::String(s) => serializer.serialize_str(s),
1087                _ => Err(serde::ser::Error::custom("Expected string value")),
1088            },
1089            Bytes => match self.value {
1090                Value::Array(arr) => {
1091                    let bytes: Result<Vec<u8>, _> = arr
1092                        .iter()
1093                        .map(|v| match v {
1094                            Value::Number(n) => n
1095                                .as_u64()
1096                                .and_then(|n| u8::try_from(n).ok())
1097                                .ok_or_else(|| {
1098                                    serde::ser::Error::custom("Invalid byte value in array")
1099                                }),
1100                            _ => Err(serde::ser::Error::custom("Expected number in byte array")),
1101                        })
1102                        .collect();
1103                    serializer.serialize_bytes(&bytes?)
1104                }
1105                _ => Err(serde::ser::Error::custom("Expected array for bytes")),
1106            },
1107            Option(inner_format) => match self.value {
1108                Value::Null => serializer.serialize_none(),
1109                _ => serializer.serialize_some(&SerializationContext {
1110                    value: self.value,
1111                    format: inner_format,
1112                    registry: self.registry,
1113                    environment: self.environment,
1114                }),
1115            },
1116            Seq(inner_format) => match self.value {
1117                Value::Array(arr) => {
1118                    let mut seq = serializer.serialize_seq(Some(arr.len()))?;
1119                    for item in arr {
1120                        seq.serialize_element(&SerializationContext {
1121                            value: item,
1122                            format: inner_format,
1123                            registry: self.registry,
1124                            environment: self.environment,
1125                        })?;
1126                    }
1127                    seq.end()
1128                }
1129                _ => Err(serde::ser::Error::custom("Expected array for sequence")),
1130            },
1131            Map { key, value } => match self.value {
1132                Value::Object(obj) => {
1133                    let mut map = serializer.serialize_map(Some(obj.len()))?;
1134                    for (k, v) in obj {
1135                        map.serialize_entry(
1136                            &SerializationContext {
1137                                value: &Value::String(k.clone()),
1138                                format: key,
1139                                registry: self.registry,
1140                                environment: self.environment,
1141                            },
1142                            &SerializationContext {
1143                                value: v,
1144                                format: value,
1145                                registry: self.registry,
1146                                environment: self.environment,
1147                            },
1148                        )?;
1149                    }
1150                    map.end()
1151                }
1152                _ => Err(serde::ser::Error::custom("Expected object for map")),
1153            },
1154            Tuple(formats) => match self.value {
1155                Value::Array(arr) => {
1156                    if arr.len() != formats.len() {
1157                        return Err(serde::ser::Error::custom(format!(
1158                            "Expected tuple of length {}, got {}",
1159                            formats.len(),
1160                            arr.len()
1161                        )));
1162                    }
1163                    let mut tuple = serializer.serialize_tuple(formats.len())?;
1164                    for (item, format) in arr.iter().zip(formats.iter()) {
1165                        tuple.serialize_element(&SerializationContext {
1166                            value: item,
1167                            format,
1168                            registry: self.registry,
1169                            environment: self.environment,
1170                        })?;
1171                    }
1172                    tuple.end()
1173                }
1174                _ => Err(serde::ser::Error::custom("Expected array for tuple")),
1175            },
1176            TupleArray { content, size } => match self.value {
1177                Value::Array(arr) => {
1178                    if arr.len() != *size {
1179                        return Err(serde::ser::Error::custom(format!(
1180                            "Expected array of length {}, got {}",
1181                            size,
1182                            arr.len()
1183                        )));
1184                    }
1185                    let mut tuple = serializer.serialize_tuple(*size)?;
1186                    for item in arr {
1187                        tuple.serialize_element(&SerializationContext {
1188                            value: item,
1189                            format: content,
1190                            registry: self.registry,
1191                            environment: self.environment,
1192                        })?;
1193                    }
1194                    tuple.end()
1195                }
1196                _ => Err(serde::ser::Error::custom("Expected array for tuple array")),
1197            },
1198        }
1199    }
1200}
1201
1202fn serialize_integer<S, I>(value: &Value, serializer: S) -> Result<S::Ok, S::Error>
1203where
1204    S: Serializer,
1205    I: TryFrom<i64> + TryFrom<u64> + Serialize,
1206{
1207    match value {
1208        Value::Number(n) => {
1209            if let Some(i) = n.as_i64() {
1210                let converted = I::try_from(i)
1211                    .map_err(|_| serde::ser::Error::custom("Integer out of range"))?;
1212                converted.serialize(serializer)
1213            } else if let Some(u) = n.as_u64() {
1214                let converted = I::try_from(u)
1215                    .map_err(|_| serde::ser::Error::custom("Integer out of range"))?;
1216                converted.serialize(serializer)
1217            } else {
1218                Err(serde::ser::Error::custom("Invalid number"))
1219            }
1220        }
1221        _ => Err(serde::ser::Error::custom("Expected number")),
1222    }
1223}
1224
1225fn serialize_integer_or_string<S, I>(value: &Value, serializer: S) -> Result<S::Ok, S::Error>
1226where
1227    S: Serializer,
1228    I: TryFrom<i64> + TryFrom<u64> + std::str::FromStr + Serialize,
1229{
1230    match value {
1231        Value::Number(n) => {
1232            if let Some(i) = n.as_i64() {
1233                let converted = I::try_from(i)
1234                    .map_err(|_| serde::ser::Error::custom("Integer out of range"))?;
1235                converted.serialize(serializer)
1236            } else if let Some(u) = n.as_u64() {
1237                let converted = I::try_from(u)
1238                    .map_err(|_| serde::ser::Error::custom("Integer out of range"))?;
1239                converted.serialize(serializer)
1240            } else {
1241                Err(serde::ser::Error::custom("Invalid number"))
1242            }
1243        }
1244        Value::String(s) => {
1245            let converted = s
1246                .parse::<I>()
1247                .map_err(|_| serde::ser::Error::custom("Invalid integer string"))?;
1248            converted.serialize(serializer)
1249        }
1250        _ => Err(serde::ser::Error::custom("Expected number or string")),
1251    }
1252}
1253
1254fn serialize_f32<S>(value: &Value, serializer: S) -> Result<S::Ok, S::Error>
1255where
1256    S: Serializer,
1257{
1258    match value {
1259        Value::Number(n) => {
1260            if let Some(f) = n.as_f64() {
1261                serializer.serialize_f32(f as f32)
1262            } else {
1263                Err(serde::ser::Error::custom("Invalid float"))
1264            }
1265        }
1266        _ => Err(serde::ser::Error::custom("Expected number for float")),
1267    }
1268}
1269
1270fn serialize_f64<S>(value: &Value, serializer: S) -> Result<S::Ok, S::Error>
1271where
1272    S: Serializer,
1273{
1274    match value {
1275        Value::Number(n) => {
1276            if let Some(f) = n.as_f64() {
1277                serializer.serialize_f64(f)
1278            } else {
1279                Err(serde::ser::Error::custom("Invalid float"))
1280            }
1281        }
1282        _ => Err(serde::ser::Error::custom("Expected number for float")),
1283    }
1284}
1285
1286fn serialize_container_format<S, E>(
1287    name: &str,
1288    container_format: &ContainerFormat,
1289    value: &Value,
1290    registry: &Registry,
1291    environment: &E,
1292    serializer: S,
1293) -> Result<S::Ok, S::Error>
1294where
1295    S: Serializer,
1296    E: SerializationEnvironment,
1297{
1298    use ContainerFormat::*;
1299
1300    let static_name = environment.get_static_name(name);
1301
1302    match container_format {
1303        UnitStruct => serializer.serialize_unit_struct(static_name),
1304        NewTypeStruct(format) => {
1305            let context = SerializationContext {
1306                value,
1307                format,
1308                registry,
1309                environment,
1310            };
1311            serializer.serialize_newtype_struct(static_name, &context)
1312        }
1313        TupleStruct(formats) => match value {
1314            Value::Array(arr) => {
1315                if arr.len() != formats.len() {
1316                    return Err(serde::ser::Error::custom(format!(
1317                        "Expected tuple struct of length {}, got {}",
1318                        formats.len(),
1319                        arr.len()
1320                    )));
1321                }
1322                let mut tuple_struct =
1323                    serializer.serialize_tuple_struct(static_name, formats.len())?;
1324                for (item, format) in arr.iter().zip(formats.iter()) {
1325                    tuple_struct.serialize_field(&SerializationContext {
1326                        value: item,
1327                        format,
1328                        registry,
1329                        environment,
1330                    })?;
1331                }
1332                tuple_struct.end()
1333            }
1334            _ => Err(serde::ser::Error::custom("Expected array for tuple struct")),
1335        },
1336        Struct(fields) => match value {
1337            Value::Object(obj) => {
1338                let mut struct_ser = serializer.serialize_struct(static_name, fields.len())?;
1339                for field in fields {
1340                    let field_value = obj.get(&field.name).ok_or_else(|| {
1341                        serde::ser::Error::custom(format!("Missing field: {}", field.name))
1342                    })?;
1343                    let static_field_name = environment.get_static_name(&field.name);
1344                    struct_ser.serialize_field(
1345                        static_field_name,
1346                        &SerializationContext {
1347                            value: field_value,
1348                            format: &field.value,
1349                            registry,
1350                            environment,
1351                        },
1352                    )?;
1353                }
1354                struct_ser.end()
1355            }
1356            _ => Err(serde::ser::Error::custom("Expected object for struct")),
1357        },
1358        Enum(variants) => match value {
1359            Value::Object(obj) => {
1360                if obj.len() != 1 {
1361                    return Err(serde::ser::Error::custom(
1362                        "Expected object with single variant key",
1363                    ));
1364                }
1365                let (variant_name, variant_value) = obj.iter().next().unwrap();
1366
1367                // Find the variant by name
1368                let (variant_index, variant_format) = variants
1369                    .iter()
1370                    .find(|(_, v)| v.name == *variant_name)
1371                    .ok_or_else(|| {
1372                        serde::ser::Error::custom(format!("Unknown variant: {}", variant_name))
1373                    })?;
1374
1375                serialize_enum_variant(
1376                    static_name,
1377                    *variant_index,
1378                    variant_name,
1379                    &variant_format.value,
1380                    variant_value,
1381                    registry,
1382                    environment,
1383                    serializer,
1384                )
1385            }
1386            _ => Err(serde::ser::Error::custom("Expected object for enum")),
1387        },
1388    }
1389}
1390
1391#[allow(clippy::too_many_arguments)]
1392fn serialize_enum_variant<S, E>(
1393    enum_name: &'static str,
1394    variant_index: u32,
1395    variant_name: &str,
1396    variant_format: &VariantFormat,
1397    value: &Value,
1398    registry: &Registry,
1399    environment: &E,
1400    serializer: S,
1401) -> Result<S::Ok, S::Error>
1402where
1403    S: Serializer,
1404    E: SerializationEnvironment,
1405{
1406    use VariantFormat::*;
1407
1408    let static_variant_name = environment.get_static_name(variant_name);
1409
1410    match variant_format {
1411        Variable(_) => Err(serde::ser::Error::custom(
1412            "Variant format cannot contain variables",
1413        )),
1414        Unit => match value {
1415            Value::Null => {
1416                serializer.serialize_unit_variant(enum_name, variant_index, static_variant_name)
1417            }
1418            _ => Err(serde::ser::Error::custom("Expected null for unit variant")),
1419        },
1420        NewType(format) => {
1421            let context = SerializationContext {
1422                value,
1423                format,
1424                registry,
1425                environment,
1426            };
1427            serializer.serialize_newtype_variant(
1428                enum_name,
1429                variant_index,
1430                static_variant_name,
1431                &context,
1432            )
1433        }
1434        Tuple(formats) => match value {
1435            Value::Array(arr) => {
1436                if arr.len() != formats.len() {
1437                    return Err(serde::ser::Error::custom(format!(
1438                        "Expected tuple variant of length {}, got {}",
1439                        formats.len(),
1440                        arr.len()
1441                    )));
1442                }
1443                let mut tuple_variant = serializer.serialize_tuple_variant(
1444                    enum_name,
1445                    variant_index,
1446                    static_variant_name,
1447                    formats.len(),
1448                )?;
1449                for (item, format) in arr.iter().zip(formats.iter()) {
1450                    tuple_variant.serialize_field(&SerializationContext {
1451                        value: item,
1452                        format,
1453                        registry,
1454                        environment,
1455                    })?;
1456                }
1457                tuple_variant.end()
1458            }
1459            _ => Err(serde::ser::Error::custom(
1460                "Expected array for tuple variant",
1461            )),
1462        },
1463        Struct(fields) => match value {
1464            Value::Object(obj) => {
1465                let mut struct_variant = serializer.serialize_struct_variant(
1466                    enum_name,
1467                    variant_index,
1468                    static_variant_name,
1469                    fields.len(),
1470                )?;
1471                for field in fields {
1472                    let field_value = obj.get(&field.name).ok_or_else(|| {
1473                        serde::ser::Error::custom(format!("Missing field: {}", field.name))
1474                    })?;
1475                    let static_field_name = environment.get_static_name(&field.name);
1476                    struct_variant.serialize_field(
1477                        static_field_name,
1478                        &SerializationContext {
1479                            value: field_value,
1480                            format: &field.value,
1481                            registry,
1482                            environment,
1483                        },
1484                    )?;
1485                }
1486                struct_variant.end()
1487            }
1488            _ => Err(serde::ser::Error::custom(
1489                "Expected object for struct variant",
1490            )),
1491        },
1492    }
1493}