Skip to main content

parquet_variant_json/
to_json.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Module for converting Variant data to JSON format
6use arrow_schema::ArrowError;
7use base64::{Engine as _, engine::general_purpose};
8use chrono::Timelike;
9use parquet_variant::{Variant, VariantList, VariantObject};
10use serde_json::Value;
11use std::io::Write;
12
13/// Extension trait for converting Variants to JSON
14pub trait VariantToJson {
15    ///
16    /// This function writes JSON directly to any type that implements [`Write`],
17    /// making it efficient for streaming or when you want to control the output destination.
18    ///
19    /// See [`VariantToJson::to_json_string`] for a convenience function that returns a
20    /// JSON string.
21    ///
22    /// # Arguments
23    ///
24    /// * `writer` - Writer to output JSON to
25    /// * `variant` - The Variant value to convert
26    ///
27    /// # Returns
28    ///
29    /// * `Ok(())` if successful
30    /// * `Err` with error details if conversion fails
31    ///
32    /// # Examples
33    ///
34    ///
35    /// ```rust
36    /// # use parquet_variant::{Variant};
37    /// # use parquet_variant_json::VariantToJson;
38    /// # use arrow_schema::ArrowError;
39    /// let variant = Variant::from("Hello, World!");
40    /// let mut buffer = Vec::new();
41    /// variant.to_json(&mut buffer)?;
42    /// assert_eq!(String::from_utf8(buffer).unwrap(), "\"Hello, World!\"");
43    /// # Ok::<(), ArrowError>(())
44    /// ```
45    ///
46    /// # Example: Create a [`Variant::Object`] and convert to JSON
47    /// ```rust
48    /// # use parquet_variant::{Variant, VariantBuilder};
49    /// # use parquet_variant_json::VariantToJson;
50    /// # use arrow_schema::ArrowError;
51    /// let mut builder = VariantBuilder::new();
52    /// // Create an object builder that will write fields to the object
53    /// let mut object_builder = builder.new_object();
54    /// object_builder.insert("first_name", "Jiaying");
55    /// object_builder.insert("last_name", "Li");
56    /// object_builder.finish();
57    /// // Finish the builder to get the metadata and value
58    /// let (metadata, value) = builder.finish();
59    /// // Create the Variant and convert to JSON
60    /// let variant = Variant::try_new(&metadata, &value)?;
61    /// let mut writer = Vec::new();
62    /// variant.to_json(&mut writer)?;
63    /// assert_eq!(br#"{"first_name":"Jiaying","last_name":"Li"}"#, writer.as_slice());
64    /// # Ok::<(), ArrowError>(())
65    /// ```
66    fn to_json(&self, buffer: &mut impl Write) -> Result<(), ArrowError>;
67
68    /// Convert [`Variant`] to JSON [`String`]
69    ///
70    /// This is a convenience function that converts a Variant to a JSON string.
71    /// This is the same as calling [`VariantToJson::to_json`] with a [`Vec`].
72    /// It's the simplest way to get a JSON representation when you just need a String result.
73    ///
74    /// # Arguments
75    ///
76    /// * `variant` - The Variant value to convert
77    ///
78    /// # Returns
79    ///
80    /// * `Ok(String)` containing the JSON representation
81    /// * `Err` with error details if conversion fails
82    ///
83    /// # Examples
84    ///
85    /// ```rust
86    /// # use parquet_variant::{Variant};
87    /// # use parquet_variant_json::VariantToJson;
88    /// # use arrow_schema::ArrowError;
89    /// let variant = Variant::Int32(42);
90    /// let json = variant.to_json_string()?;
91    /// assert_eq!(json, "42");
92    /// # Ok::<(), ArrowError>(())
93    /// ```
94    ///
95    /// # Example: Create a [`Variant::Object`] and convert to JSON
96    ///
97    /// This example shows how to create an object with two fields and convert it to JSON:
98    /// ```json
99    /// {
100    ///   "first_name": "Jiaying",
101    ///   "last_name": "Li"
102    /// }
103    /// ```
104    ///
105    /// ```rust
106    /// # use parquet_variant::{Variant, VariantBuilder};
107    /// # use parquet_variant_json::VariantToJson;
108    /// # use arrow_schema::ArrowError;
109    /// let mut builder = VariantBuilder::new();
110    /// // Create an object builder that will write fields to the object
111    /// let mut object_builder = builder.new_object();
112    /// object_builder.insert("first_name", "Jiaying");
113    /// object_builder.insert("last_name", "Li");
114    /// object_builder.finish();
115    /// // Finish the builder to get the metadata and value
116    /// let (metadata, value) = builder.finish();
117    /// // Create the Variant and convert to JSON
118    /// let variant = Variant::try_new(&metadata, &value)?;
119    /// let json = variant.to_json_string()?;
120    /// assert_eq!(r#"{"first_name":"Jiaying","last_name":"Li"}"#, json);
121    /// # Ok::<(), ArrowError>(())
122    /// ```
123    fn to_json_string(&self) -> Result<String, ArrowError>;
124
125    /// Convert [`Variant`] to [`serde_json::Value`]
126    ///
127    /// This function converts a Variant to a [`serde_json::Value`], which is useful
128    /// when you need to work with the JSON data programmatically or integrate with
129    /// other serde-based JSON processing.
130    ///
131    /// # Arguments
132    ///
133    /// * `variant` - The Variant value to convert
134    ///
135    /// # Returns
136    ///
137    /// * `Ok(Value)` containing the JSON value
138    /// * `Err` with error details if conversion fails
139    ///
140    /// # Examples
141    ///
142    /// ```rust
143    /// # use parquet_variant::{Variant};
144    /// # use parquet_variant_json::VariantToJson;
145    /// # use serde_json::Value;
146    /// # use arrow_schema::ArrowError;
147    /// let variant = Variant::from("hello");
148    /// let json_value = variant.to_json_value()?;
149    /// assert_eq!(json_value, Value::String("hello".to_string()));
150    /// # Ok::<(), ArrowError>(())
151    /// ```
152    fn to_json_value(&self) -> Result<Value, ArrowError>;
153}
154
155impl VariantToJson for Variant<'_, '_> {
156    fn to_json(&self, buffer: &mut impl Write) -> Result<(), ArrowError> {
157        match self {
158            Variant::Null => write!(buffer, "null")?,
159            Variant::BooleanTrue => write!(buffer, "true")?,
160            Variant::BooleanFalse => write!(buffer, "false")?,
161            Variant::Int8(i) => write!(buffer, "{i}")?,
162            Variant::Int16(i) => write!(buffer, "{i}")?,
163            Variant::Int32(i) => write!(buffer, "{i}")?,
164            Variant::Int64(i) => write!(buffer, "{i}")?,
165            Variant::Float(f) => write!(buffer, "{}", ryu::Buffer::new().format(*f))?,
166            Variant::Double(f) => write!(buffer, "{}", ryu::Buffer::new().format(*f))?,
167            Variant::Decimal4(decimal) => write!(buffer, "{decimal}")?,
168            Variant::Decimal8(decimal) => write!(buffer, "{decimal}")?,
169            Variant::Decimal16(decimal) => write!(buffer, "{decimal}")?,
170            Variant::Date(date) => write!(buffer, "\"{}\"", format_date_string(date))?,
171            Variant::TimestampMicros(ts) | Variant::TimestampNanos(ts) => {
172                write!(buffer, "\"{}\"", ts.to_rfc3339())?
173            }
174            Variant::TimestampNtzMicros(ts) => {
175                write!(buffer, "\"{}\"", format_timestamp_ntz_string(ts, 6))?
176            }
177            Variant::TimestampNtzNanos(ts) => {
178                write!(buffer, "\"{}\"", format_timestamp_ntz_string(ts, 9))?
179            }
180            Variant::Time(time) => write!(buffer, "\"{}\"", format_time_ntz_str(time))?,
181            Variant::Binary(bytes) => {
182                // Encode binary as base64 string and serialize it directly into the writer
183                let base64_str = format_binary_base64(bytes);
184                serde_json::to_writer(&mut *buffer, &base64_str)
185                    .map_err(|e| json_encoding_error("JSON encoding error", e))?
186            }
187            Variant::String(s) => {
188                // Serialize directly into the writer, escaping as needed
189                serde_json::to_writer(&mut *buffer, s)
190                    .map_err(|e| json_encoding_error("JSON encoding error", e))?
191            }
192            Variant::ShortString(s) => {
193                // Serialize directly into the writer, escaping as needed
194                serde_json::to_writer(&mut *buffer, s.as_str())
195                    .map_err(|e| json_encoding_error("JSON encoding error", e))?
196            }
197            Variant::Uuid(uuid) => {
198                write!(buffer, "\"{uuid}\"")?;
199            }
200            Variant::Object(obj) => {
201                convert_object_to_json(buffer, obj)?;
202            }
203            Variant::List(arr) => {
204                convert_array_to_json(buffer, arr)?;
205            }
206        }
207        Ok(())
208    }
209
210    fn to_json_string(&self) -> Result<String, ArrowError> {
211        let mut buffer = Vec::new();
212        self.to_json(&mut buffer)?;
213        String::from_utf8(buffer)
214            .map_err(|e| ArrowError::InvalidArgumentError(format!("UTF-8 conversion error: {e}")))
215    }
216
217    fn to_json_value(&self) -> Result<Value, ArrowError> {
218        match self {
219            Variant::Null => Ok(Value::Null),
220            Variant::BooleanTrue => Ok(Value::Bool(true)),
221            Variant::BooleanFalse => Ok(Value::Bool(false)),
222            Variant::Int8(i) => Ok(Value::Number((*i).into())),
223            Variant::Int16(i) => Ok(Value::Number((*i).into())),
224            Variant::Int32(i) => Ok(Value::Number((*i).into())),
225            Variant::Int64(i) => Ok(Value::Number((*i).into())),
226            Variant::Float(f) => serde_json::Number::from_f64((*f).into())
227                .map(Value::Number)
228                .ok_or_else(|| ArrowError::InvalidArgumentError("Invalid float value".to_string())),
229            Variant::Double(f) => serde_json::Number::from_f64(*f)
230                .map(Value::Number)
231                .ok_or_else(|| {
232                    ArrowError::InvalidArgumentError("Invalid double value".to_string())
233                }),
234            Variant::Decimal4(decimal4) => {
235                let scale = decimal4.scale();
236                let integer = decimal4.integer();
237
238                let integer = if scale == 0 {
239                    integer
240                } else {
241                    let divisor = 10_i32.pow(scale as u32);
242                    if integer % divisor != 0 {
243                        // fall back to floating point
244                        return Ok(Value::from(integer as f64 / divisor as f64));
245                    }
246                    integer / divisor
247                };
248                Ok(Value::from(integer))
249            }
250            Variant::Decimal8(decimal8) => {
251                let scale = decimal8.scale();
252                let integer = decimal8.integer();
253
254                let integer = if scale == 0 {
255                    integer
256                } else {
257                    let divisor = 10_i64.pow(scale as u32);
258                    if integer % divisor != 0 {
259                        // fall back to floating point
260                        return Ok(Value::from(integer as f64 / divisor as f64));
261                    }
262                    integer / divisor
263                };
264                Ok(Value::from(integer))
265            }
266            Variant::Decimal16(decimal16) => {
267                let scale = decimal16.scale();
268                let integer = decimal16.integer();
269
270                let integer = if scale == 0 {
271                    integer
272                } else {
273                    let divisor = 10_i128.pow(scale as u32);
274                    if integer % divisor != 0 {
275                        // fall back to floating point
276                        return Ok(Value::from(integer as f64 / divisor as f64));
277                    }
278                    integer / divisor
279                };
280                // i128 has higher precision than any 64-bit type. Try a lossless narrowing cast to
281                // i64 or u64 first, falling back to a lossy narrowing cast to f64 if necessary.
282                let value = i64::try_from(integer)
283                    .map(Value::from)
284                    .or_else(|_| u64::try_from(integer).map(Value::from))
285                    .unwrap_or_else(|_| Value::from(integer as f64));
286                Ok(value)
287            }
288            Variant::Date(date) => Ok(Value::String(format_date_string(date))),
289            Variant::TimestampMicros(ts) | Variant::TimestampNanos(ts) => {
290                Ok(Value::String(ts.to_rfc3339()))
291            }
292            Variant::TimestampNtzMicros(ts) => {
293                Ok(Value::String(format_timestamp_ntz_string(ts, 6)))
294            }
295            Variant::TimestampNtzNanos(ts) => Ok(Value::String(format_timestamp_ntz_string(ts, 9))),
296            Variant::Time(time) => Ok(Value::String(format_time_ntz_str(time))),
297            Variant::Binary(bytes) => Ok(Value::String(format_binary_base64(bytes))),
298            Variant::String(s) => Ok(Value::String(s.to_string())),
299            Variant::ShortString(s) => Ok(Value::String(s.to_string())),
300            Variant::Uuid(uuid) => Ok(Value::String(uuid.to_string())),
301            Variant::Object(obj) => {
302                let map = obj
303                    .iter()
304                    .map(|(k, v)| v.to_json_value().map(|json_val| (k.to_string(), json_val)))
305                    .collect::<Result<_, _>>()?;
306                Ok(Value::Object(map))
307            }
308            Variant::List(arr) => {
309                let vec = arr
310                    .iter()
311                    .map(|element| element.to_json_value())
312                    .collect::<Result<_, _>>()?;
313                Ok(Value::Array(vec))
314            }
315        }
316    }
317}
318
319// Format string constants to avoid duplication and reduce errors
320const DATE_FORMAT: &str = "%Y-%m-%d";
321
322// Helper functions for consistent formatting
323fn format_date_string(date: &chrono::NaiveDate) -> String {
324    date.format(DATE_FORMAT).to_string()
325}
326
327fn format_timestamp_ntz_string(ts: &chrono::NaiveDateTime, precision: usize) -> String {
328    let format_str = format!("{}", ts.format(&format!("%Y-%m-%dT%H:%M:%S%.{precision}f")));
329    ts.format(format_str.as_str()).to_string()
330}
331
332fn format_binary_base64(bytes: &[u8]) -> String {
333    general_purpose::STANDARD.encode(bytes)
334}
335
336fn format_time_ntz_str(time: &chrono::NaiveTime) -> String {
337    let base = time.format("%H:%M:%S").to_string();
338    let micros = time.nanosecond() / 1000;
339    match micros {
340        0 => format!("{}.{}", base, 0),
341        _ => {
342            let micros_str = format!("{micros:06}");
343            let micros_str_trimmed = micros_str.trim_end_matches('0');
344            format!("{base}.{micros_str_trimmed}")
345        }
346    }
347}
348
349/// Maps a [`serde_json`] serialization failure, keeping writer failures as
350/// [`ArrowError::IoError`] just like [`write!`] does
351fn json_encoding_error(context: &str, error: serde_json::Error) -> ArrowError {
352    if error.is_io() {
353        ArrowError::IoError(error.to_string(), error.into())
354    } else {
355        ArrowError::InvalidArgumentError(format!("{context}: {error}"))
356    }
357}
358
359/// Convert object fields to JSON
360fn convert_object_to_json(buffer: &mut impl Write, obj: &VariantObject) -> Result<(), ArrowError> {
361    write!(buffer, "{{")?;
362
363    // Get all fields from the object
364    let mut first = true;
365
366    for (key, value) in obj.iter() {
367        if !first {
368            write!(buffer, ",")?;
369        }
370        first = false;
371
372        // Write the key (properly escaped), directly into the writer
373        serde_json::to_writer(&mut *buffer, key)
374            .map_err(|e| json_encoding_error("JSON key encoding error", e))?;
375        write!(buffer, ":")?;
376
377        // Recursively convert the value
378        value.to_json(buffer)?;
379    }
380
381    write!(buffer, "}}")?;
382    Ok(())
383}
384
385/// Convert array elements to JSON
386fn convert_array_to_json(buffer: &mut impl Write, arr: &VariantList) -> Result<(), ArrowError> {
387    write!(buffer, "[")?;
388
389    let mut first = true;
390    for element in arr.iter() {
391        if !first {
392            write!(buffer, ",")?;
393        }
394        first = false;
395
396        element.to_json(buffer)?;
397    }
398
399    write!(buffer, "]")?;
400    Ok(())
401}
402
403#[cfg(test)]
404mod tests {
405    use super::*;
406    use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
407    use parquet_variant::{VariantDecimal4, VariantDecimal8, VariantDecimal16};
408
409    #[test]
410    fn test_decimal_edge_cases() -> Result<(), ArrowError> {
411        // Test negative decimal
412        let negative_variant = Variant::from(VariantDecimal4::try_new(-12345, 3)?);
413        let negative_json = negative_variant.to_json_string()?;
414        assert_eq!(negative_json, "-12.345");
415
416        // Test large scale decimal
417        let large_scale_variant = Variant::from(VariantDecimal8::try_new(123456789, 6)?);
418        let large_scale_json = large_scale_variant.to_json_string()?;
419        assert_eq!(large_scale_json, "123.456789");
420
421        Ok(())
422    }
423
424    #[test]
425    fn test_decimal16_to_json() -> Result<(), ArrowError> {
426        let variant = Variant::from(VariantDecimal16::try_new(123456789012345, 4)?);
427        let json = variant.to_json_string()?;
428        assert_eq!(json, "12345678901.2345");
429
430        let json_value = variant.to_json_value()?;
431        assert!(matches!(json_value, Value::Number(_)));
432
433        // Test very large number
434        let large_variant = Variant::from(VariantDecimal16::try_new(999999999999999999, 2)?);
435        let large_json = large_variant.to_json_string()?;
436        // Due to f64 precision limits, very large numbers may lose precision
437        assert!(
438            large_json.starts_with("9999999999999999")
439                || large_json.starts_with("10000000000000000")
440        );
441        Ok(())
442    }
443
444    #[test]
445    fn test_date_to_json() -> Result<(), ArrowError> {
446        let date = NaiveDate::from_ymd_opt(2023, 12, 25).unwrap();
447        let variant = Variant::Date(date);
448        let json = variant.to_json_string()?;
449        assert_eq!(json, "\"2023-12-25\"");
450
451        let json_value = variant.to_json_value()?;
452        assert_eq!(json_value, Value::String("2023-12-25".to_string()));
453
454        // Test leap year date
455        let leap_date = NaiveDate::from_ymd_opt(2024, 2, 29).unwrap();
456        let leap_variant = Variant::Date(leap_date);
457        let leap_json = leap_variant.to_json_string()?;
458        assert_eq!(leap_json, "\"2024-02-29\"");
459        Ok(())
460    }
461
462    #[test]
463    fn test_timestamp_micros_to_json() -> Result<(), ArrowError> {
464        let timestamp = DateTime::parse_from_rfc3339("2023-12-25T10:30:45Z")
465            .unwrap()
466            .with_timezone(&Utc);
467        let variant = Variant::TimestampMicros(timestamp);
468        let json = variant.to_json_string()?;
469        assert!(json.contains("2023-12-25T10:30:45"));
470        assert!(json.starts_with('"') && json.ends_with('"'));
471
472        let json_value = variant.to_json_value()?;
473        assert!(matches!(json_value, Value::String(_)));
474        Ok(())
475    }
476
477    #[test]
478    fn test_timestamp_ntz_micros_to_json() -> Result<(), ArrowError> {
479        let naive_timestamp = DateTime::from_timestamp(1703505045, 123456)
480            .unwrap()
481            .naive_utc();
482        let variant = Variant::TimestampNtzMicros(naive_timestamp);
483        let json = variant.to_json_string()?;
484        assert!(json.contains("2023-12-25"));
485        assert!(json.starts_with('"') && json.ends_with('"'));
486
487        let json_value = variant.to_json_value()?;
488        assert!(matches!(json_value, Value::String(_)));
489        Ok(())
490    }
491
492    #[test]
493    fn test_time_to_json() -> Result<(), ArrowError> {
494        let naive_time = NaiveTime::from_num_seconds_from_midnight_opt(12345, 123460708).unwrap();
495        let variant = Variant::Time(naive_time);
496        let json = variant.to_json_string()?;
497        assert_eq!("\"03:25:45.12346\"", json);
498
499        let expected = [
500            (10, "00:00:00.00001"),
501            (10010, "00:00:00.01001"),
502            (100000, "00:00:00.1"),
503            (123450, "00:00:00.12345"),
504        ];
505
506        for (micros, expected_json) in expected {
507            let naive_time =
508                NaiveTime::from_num_seconds_from_midnight_opt(0, micros * 1000).unwrap();
509            let variant = Variant::Time(naive_time);
510            let json = variant.to_json_string()?;
511            assert_eq!(format!("\"{expected_json}\""), json);
512        }
513
514        let json_value = variant.to_json_value()?;
515        assert!(matches!(json_value, Value::String(_)));
516        Ok(())
517    }
518
519    #[test]
520    fn test_timestamp_nanos_to_json() -> Result<(), ArrowError> {
521        let timestamp = DateTime::parse_from_rfc3339("2023-12-25T10:30:45.123456789Z")
522            .unwrap()
523            .with_timezone(&Utc);
524        let variant = Variant::TimestampNanos(timestamp);
525        let json = variant.to_json_string()?;
526        assert_eq!(json, "\"2023-12-25T10:30:45.123456789+00:00\"");
527
528        let json_value = variant.to_json_value()?;
529        assert!(matches!(json_value, Value::String(_)));
530        Ok(())
531    }
532
533    #[test]
534    fn test_timestamp_ntz_nanos_to_json() -> Result<(), ArrowError> {
535        let naive_timestamp = DateTime::from_timestamp(1703505045, 123456789)
536            .unwrap()
537            .naive_utc();
538        let variant = Variant::TimestampNtzNanos(naive_timestamp);
539        let json = variant.to_json_string()?;
540        assert_eq!(json, "\"2023-12-25T11:50:45.123456789\"");
541
542        let json_value = variant.to_json_value()?;
543        assert!(matches!(json_value, Value::String(_)));
544        Ok(())
545    }
546
547    #[test]
548    fn test_binary_to_json() -> Result<(), ArrowError> {
549        let binary_data = b"Hello, World!";
550        let variant = Variant::Binary(binary_data);
551        let json = variant.to_json_string()?;
552
553        // Should be base64 encoded and quoted
554        assert!(json.starts_with('"') && json.ends_with('"'));
555        assert!(json.len() > 2); // Should have content
556
557        let json_value = variant.to_json_value()?;
558        assert!(matches!(json_value, Value::String(_)));
559
560        // Test empty binary
561        let empty_variant = Variant::Binary(b"");
562        let empty_json = empty_variant.to_json_string()?;
563        assert_eq!(empty_json, "\"\"");
564
565        // Test binary with special bytes
566        let special_variant = Variant::Binary(&[0, 255, 128, 64]);
567        let special_json = special_variant.to_json_string()?;
568        assert!(special_json.starts_with('"') && special_json.ends_with('"'));
569        Ok(())
570    }
571
572    #[test]
573    fn test_string_to_json() -> Result<(), ArrowError> {
574        let variant = Variant::from("hello world");
575        let json = variant.to_json_string()?;
576        assert_eq!(json, "\"hello world\"");
577
578        let json_value = variant.to_json_value()?;
579        assert_eq!(json_value, Value::String("hello world".to_string()));
580        Ok(())
581    }
582
583    #[test]
584    fn test_short_string_to_json() -> Result<(), ArrowError> {
585        use parquet_variant::ShortString;
586        let short_string = ShortString::try_new("short")?;
587        let variant = Variant::ShortString(short_string);
588        let json = variant.to_json_string()?;
589        assert_eq!(json, "\"short\"");
590
591        let json_value = variant.to_json_value()?;
592        assert_eq!(json_value, Value::String("short".to_string()));
593        Ok(())
594    }
595
596    #[test]
597    fn test_uuid_to_json() -> Result<(), ArrowError> {
598        let uuid = uuid::Uuid::parse_str("123e4567-e89b-12d3-a456-426614174000").unwrap();
599        let variant = Variant::Uuid(uuid);
600        let json = variant.to_json_string()?;
601        assert_eq!(json, "\"123e4567-e89b-12d3-a456-426614174000\"");
602
603        let json_value = variant.to_json_value()?;
604        assert_eq!(
605            json_value,
606            Value::String("123e4567-e89b-12d3-a456-426614174000".to_string())
607        );
608        Ok(())
609    }
610
611    #[test]
612    fn test_string_escaping() -> Result<(), ArrowError> {
613        let variant = Variant::from("hello\nworld\t\"quoted\"");
614        let json = variant.to_json_string()?;
615        assert_eq!(json, "\"hello\\nworld\\t\\\"quoted\\\"\"");
616
617        let json_value = variant.to_json_value()?;
618        assert_eq!(
619            json_value,
620            Value::String("hello\nworld\t\"quoted\"".to_string())
621        );
622        Ok(())
623    }
624
625    #[test]
626    fn test_json_buffer_writing() -> Result<(), ArrowError> {
627        let variant = Variant::Int8(123);
628        let mut buffer = Vec::new();
629        variant.to_json(&mut buffer)?;
630
631        let result = String::from_utf8(buffer)
632            .map_err(|e| ArrowError::InvalidArgumentError(e.to_string()))?;
633        assert_eq!(result, "123");
634        Ok(())
635    }
636
637    /// Reusable test structure for JSON conversion testing
638    struct JsonTest {
639        variant: Variant<'static, 'static>,
640        expected_json: &'static str,
641        expected_value: Value,
642    }
643
644    impl JsonTest {
645        fn run(self) {
646            let json_string = self
647                .variant
648                .to_json_string()
649                .expect("variant_to_json_string should succeed");
650            assert_eq!(
651                json_string, self.expected_json,
652                "JSON string mismatch for variant: {:?}",
653                self.variant
654            );
655
656            let json_value = self
657                .variant
658                .to_json_value()
659                .expect("variant_to_json_value should succeed");
660
661            // For floating point numbers, we need special comparison due to JSON number representation
662            match (&json_value, &self.expected_value) {
663                (Value::Number(actual), Value::Number(expected)) => {
664                    let actual_f64 = actual.as_f64().unwrap_or(0.0);
665                    let expected_f64 = expected.as_f64().unwrap_or(0.0);
666                    assert!(
667                        (actual_f64 - expected_f64).abs() < f64::EPSILON,
668                        "JSON value mismatch for variant: {:?}, got {}, expected {}",
669                        self.variant,
670                        actual_f64,
671                        expected_f64
672                    );
673                }
674                _ => {
675                    assert_eq!(
676                        json_value, self.expected_value,
677                        "JSON value mismatch for variant: {:?}",
678                        self.variant
679                    );
680                }
681            }
682
683            // Verify roundtrip: JSON string should parse to same value
684            let parsed: Value =
685                serde_json::from_str(&json_string).expect("Generated JSON should be valid");
686            // Same floating point handling for roundtrip
687            match (&parsed, &self.expected_value) {
688                (Value::Number(actual), Value::Number(expected)) => {
689                    let actual_f64 = actual.as_f64().unwrap_or(0.0);
690                    let expected_f64 = expected.as_f64().unwrap_or(0.0);
691                    assert!(
692                        (actual_f64 - expected_f64).abs() < f64::EPSILON,
693                        "Parsed JSON mismatch for variant: {:?}, got {}, expected {}",
694                        self.variant,
695                        actual_f64,
696                        expected_f64
697                    );
698                }
699                _ => {
700                    assert_eq!(
701                        parsed, self.expected_value,
702                        "Parsed JSON mismatch for variant: {:?}",
703                        self.variant
704                    );
705                }
706            }
707        }
708    }
709
710    #[test]
711    fn test_primitive_json_conversion() {
712        use parquet_variant::ShortString;
713
714        // Null
715        JsonTest {
716            variant: Variant::Null,
717            expected_json: "null",
718            expected_value: Value::Null,
719        }
720        .run();
721
722        // Booleans
723        JsonTest {
724            variant: Variant::BooleanTrue,
725            expected_json: "true",
726            expected_value: Value::Bool(true),
727        }
728        .run();
729
730        JsonTest {
731            variant: Variant::BooleanFalse,
732            expected_json: "false",
733            expected_value: Value::Bool(false),
734        }
735        .run();
736
737        // Integers - positive and negative edge cases
738        JsonTest {
739            variant: Variant::Int8(42),
740            expected_json: "42",
741            expected_value: Value::Number(42.into()),
742        }
743        .run();
744
745        JsonTest {
746            variant: Variant::Int8(-128),
747            expected_json: "-128",
748            expected_value: Value::Number((-128).into()),
749        }
750        .run();
751
752        JsonTest {
753            variant: Variant::Int16(32767),
754            expected_json: "32767",
755            expected_value: Value::Number(32767.into()),
756        }
757        .run();
758
759        JsonTest {
760            variant: Variant::Int16(-32768),
761            expected_json: "-32768",
762            expected_value: Value::Number((-32768).into()),
763        }
764        .run();
765
766        JsonTest {
767            variant: Variant::Int32(2147483647),
768            expected_json: "2147483647",
769            expected_value: Value::Number(2147483647.into()),
770        }
771        .run();
772
773        JsonTest {
774            variant: Variant::Int32(-2147483648),
775            expected_json: "-2147483648",
776            expected_value: Value::Number((-2147483648).into()),
777        }
778        .run();
779
780        JsonTest {
781            variant: Variant::Int64(9223372036854775807),
782            expected_json: "9223372036854775807",
783            expected_value: Value::Number(9223372036854775807i64.into()),
784        }
785        .run();
786
787        JsonTest {
788            variant: Variant::Int64(-9223372036854775808),
789            expected_json: "-9223372036854775808",
790            expected_value: Value::Number((-9223372036854775808i64).into()),
791        }
792        .run();
793
794        // Floats
795        JsonTest {
796            variant: Variant::Float(3.5),
797            expected_json: "3.5",
798            expected_value: serde_json::Number::from_f64(3.5)
799                .map(Value::Number)
800                .unwrap(),
801        }
802        .run();
803
804        JsonTest {
805            variant: Variant::Float(0.0),
806            expected_json: "0.0",
807            expected_value: serde_json::Number::from_f64(0.0)
808                .map(Value::Number)
809                .unwrap(),
810        }
811        .run();
812
813        JsonTest {
814            variant: Variant::Float(-1.5),
815            expected_json: "-1.5",
816            expected_value: serde_json::Number::from_f64(-1.5)
817                .map(Value::Number)
818                .unwrap(),
819        }
820        .run();
821
822        JsonTest {
823            variant: Variant::Double(std::f64::consts::E),
824            expected_json: "2.718281828459045",
825            expected_value: serde_json::Number::from_f64(std::f64::consts::E)
826                .map(Value::Number)
827                .unwrap(),
828        }
829        .run();
830
831        JsonTest {
832            variant: Variant::Double(2.0),
833            expected_json: "2.0",
834            expected_value: serde_json::Number::from_f64(2.0)
835                .map(Value::Number)
836                .unwrap(),
837        }
838        .run();
839
840        // Decimals
841        JsonTest {
842            variant: Variant::from(VariantDecimal4::try_new(12345, 2).unwrap()),
843            expected_json: "123.45",
844            expected_value: serde_json::Number::from_f64(123.45)
845                .map(Value::Number)
846                .unwrap(),
847        }
848        .run();
849
850        JsonTest {
851            variant: Variant::from(VariantDecimal4::try_new(42, 0).unwrap()),
852            expected_json: "42",
853            expected_value: serde_json::Number::from_f64(42.0)
854                .map(Value::Number)
855                .unwrap(),
856        }
857        .run();
858
859        JsonTest {
860            variant: Variant::from(VariantDecimal8::try_new(1234567890, 3).unwrap()),
861            expected_json: "1234567.89",
862            expected_value: serde_json::Number::from_f64(1234567.89)
863                .map(Value::Number)
864                .unwrap(),
865        }
866        .run();
867
868        JsonTest {
869            variant: Variant::from(VariantDecimal16::try_new(123456789012345, 4).unwrap()),
870            expected_json: "12345678901.2345",
871            expected_value: serde_json::Number::from_f64(12345678901.2345)
872                .map(Value::Number)
873                .unwrap(),
874        }
875        .run();
876
877        // Strings
878        JsonTest {
879            variant: Variant::from("hello world"),
880            expected_json: "\"hello world\"",
881            expected_value: Value::String("hello world".to_string()),
882        }
883        .run();
884
885        JsonTest {
886            variant: Variant::from(""),
887            expected_json: "\"\"",
888            expected_value: Value::String(String::new()),
889        }
890        .run();
891
892        JsonTest {
893            variant: Variant::ShortString(ShortString::try_new("test").unwrap()),
894            expected_json: "\"test\"",
895            expected_value: Value::String("test".to_string()),
896        }
897        .run();
898
899        // Date and timestamps
900        JsonTest {
901            variant: Variant::Date(NaiveDate::from_ymd_opt(2023, 12, 25).unwrap()),
902            expected_json: "\"2023-12-25\"",
903            expected_value: Value::String("2023-12-25".to_string()),
904        }
905        .run();
906
907        // Binary data (base64 encoded)
908        JsonTest {
909            variant: Variant::Binary(b"test"),
910            expected_json: "\"dGVzdA==\"", // base64 encoded "test"
911            expected_value: Value::String("dGVzdA==".to_string()),
912        }
913        .run();
914
915        JsonTest {
916            variant: Variant::Binary(b""),
917            expected_json: "\"\"", // empty base64
918            expected_value: Value::String(String::new()),
919        }
920        .run();
921
922        JsonTest {
923            variant: Variant::Binary(b"binary data"),
924            expected_json: "\"YmluYXJ5IGRhdGE=\"", // base64 encoded "binary data"
925            expected_value: Value::String("YmluYXJ5IGRhdGE=".to_string()),
926        }
927        .run();
928    }
929
930    #[test]
931    fn test_string_escaping_comprehensive() {
932        // Test comprehensive string escaping scenarios
933        JsonTest {
934            variant: Variant::from("line1\nline2\ttab\"quote\"\\backslash"),
935            expected_json: "\"line1\\nline2\\ttab\\\"quote\\\"\\\\backslash\"",
936            expected_value: Value::String("line1\nline2\ttab\"quote\"\\backslash".to_string()),
937        }
938        .run();
939
940        JsonTest {
941            variant: Variant::from("Hello δΈ–η•Œ 🌍"),
942            expected_json: "\"Hello δΈ–η•Œ 🌍\"",
943            expected_value: Value::String("Hello δΈ–η•Œ 🌍".to_string()),
944        }
945        .run();
946    }
947
948    #[test]
949    fn test_buffer_writing_variants() -> Result<(), ArrowError> {
950        let variant = Variant::from("test buffer writing");
951
952        // Test writing to a Vec<u8>
953        let mut buffer = Vec::new();
954        variant.to_json(&mut buffer)?;
955        let result = String::from_utf8(buffer)
956            .map_err(|e| ArrowError::InvalidArgumentError(e.to_string()))?;
957        assert_eq!(result, "\"test buffer writing\"");
958
959        // Test writing to vec![]
960        let mut buffer = vec![];
961        variant.to_json(&mut buffer)?;
962        let result = String::from_utf8(buffer)
963            .map_err(|e| ArrowError::InvalidArgumentError(e.to_string()))?;
964        assert_eq!(result, "\"test buffer writing\"");
965
966        Ok(())
967    }
968
969    #[test]
970    fn test_simple_object_to_json() -> Result<(), ArrowError> {
971        use parquet_variant::VariantBuilder;
972
973        // Create a simple object with various field types
974        let mut builder = VariantBuilder::new();
975
976        builder
977            .new_object()
978            .with_field("name", "Alice")
979            .with_field("age", 30i32)
980            .with_field("active", true)
981            .with_field("score", 95.5f64)
982            .finish();
983
984        let (metadata, value) = builder.finish();
985        let variant = Variant::try_new(&metadata, &value)?;
986        let json = variant.to_json_string()?;
987
988        // Parse the JSON to verify structure - handle JSON parsing errors manually
989        let parsed: Value = serde_json::from_str(&json).unwrap();
990        let obj = parsed.as_object().expect("expected JSON object");
991        assert_eq!(obj.get("name"), Some(&Value::String("Alice".to_string())));
992        assert_eq!(obj.get("age"), Some(&Value::Number(30.into())));
993        assert_eq!(obj.get("active"), Some(&Value::Bool(true)));
994        assert!(matches!(obj.get("score"), Some(Value::Number(_))));
995        assert_eq!(obj.len(), 4);
996
997        // Test variant_to_json_value as well
998        let json_value = variant.to_json_value()?;
999        assert!(matches!(json_value, Value::Object(_)));
1000
1001        Ok(())
1002    }
1003
1004    #[test]
1005    fn test_empty_object_to_json() -> Result<(), ArrowError> {
1006        use parquet_variant::VariantBuilder;
1007
1008        let mut builder = VariantBuilder::new();
1009
1010        {
1011            let obj = builder.new_object();
1012            obj.finish();
1013        }
1014
1015        let (metadata, value) = builder.finish();
1016        let variant = Variant::try_new(&metadata, &value)?;
1017        let json = variant.to_json_string()?;
1018        assert_eq!(json, "{}");
1019
1020        let json_value = variant.to_json_value()?;
1021        assert_eq!(json_value, Value::Object(serde_json::Map::new()));
1022
1023        Ok(())
1024    }
1025
1026    #[test]
1027    fn test_object_with_special_characters_to_json() -> Result<(), ArrowError> {
1028        use parquet_variant::VariantBuilder;
1029
1030        let mut builder = VariantBuilder::new();
1031
1032        builder
1033            .new_object()
1034            .with_field("message", "Hello \"World\"\nWith\tTabs")
1035            .with_field("path", "C:\\Users\\Alice\\Documents")
1036            .with_field("unicode", "πŸ˜€ Smiley")
1037            .finish();
1038
1039        let (metadata, value) = builder.finish();
1040        let variant = Variant::try_new(&metadata, &value)?;
1041        let json = variant.to_json_string()?;
1042
1043        // Verify that special characters are properly escaped
1044        assert!(json.contains("Hello \\\"World\\\"\\nWith\\tTabs"));
1045        assert!(json.contains("C:\\\\Users\\\\Alice\\\\Documents"));
1046        assert!(json.contains("πŸ˜€ Smiley"));
1047
1048        // Verify that the JSON can be parsed back
1049        let parsed: Value = serde_json::from_str(&json).unwrap();
1050        assert!(matches!(parsed, Value::Object(_)));
1051
1052        Ok(())
1053    }
1054
1055    #[test]
1056    fn test_simple_list_to_json() -> Result<(), ArrowError> {
1057        use parquet_variant::VariantBuilder;
1058
1059        let mut builder = VariantBuilder::new();
1060
1061        builder
1062            .new_list()
1063            .with_value(1i32)
1064            .with_value(2i32)
1065            .with_value(3i32)
1066            .with_value(4i32)
1067            .with_value(5i32)
1068            .finish();
1069
1070        let (metadata, value) = builder.finish();
1071        let variant = Variant::try_new(&metadata, &value)?;
1072        let json = variant.to_json_string()?;
1073        assert_eq!(json, "[1,2,3,4,5]");
1074
1075        let json_value = variant.to_json_value()?;
1076        let arr = json_value.as_array().expect("expected JSON array");
1077        assert_eq!(arr.len(), 5);
1078        assert_eq!(arr[0], Value::Number(1.into()));
1079        assert_eq!(arr[4], Value::Number(5.into()));
1080
1081        Ok(())
1082    }
1083
1084    #[test]
1085    fn test_empty_list_to_json() -> Result<(), ArrowError> {
1086        use parquet_variant::VariantBuilder;
1087
1088        let mut builder = VariantBuilder::new();
1089
1090        {
1091            let list = builder.new_list();
1092            list.finish();
1093        }
1094
1095        let (metadata, value) = builder.finish();
1096        let variant = Variant::try_new(&metadata, &value)?;
1097        let json = variant.to_json_string()?;
1098        assert_eq!(json, "[]");
1099
1100        let json_value = variant.to_json_value()?;
1101        assert_eq!(json_value, Value::Array(vec![]));
1102
1103        Ok(())
1104    }
1105
1106    #[test]
1107    fn test_mixed_type_list_to_json() -> Result<(), ArrowError> {
1108        use parquet_variant::VariantBuilder;
1109
1110        let mut builder = VariantBuilder::new();
1111
1112        builder
1113            .new_list()
1114            .with_value("hello")
1115            .with_value(42i32)
1116            .with_value(true)
1117            .with_value(()) // null
1118            .with_value(std::f64::consts::PI)
1119            .finish();
1120
1121        let (metadata, value) = builder.finish();
1122        let variant = Variant::try_new(&metadata, &value)?;
1123        let json = variant.to_json_string()?;
1124
1125        let parsed: Value = serde_json::from_str(&json).unwrap();
1126        let arr = parsed.as_array().expect("expected JSON array");
1127        assert_eq!(arr.len(), 5);
1128        assert_eq!(arr[0], Value::String("hello".to_string()));
1129        assert_eq!(arr[1], Value::Number(42.into()));
1130        assert_eq!(arr[2], Value::Bool(true));
1131        assert_eq!(arr[3], Value::Null);
1132        assert!(matches!(arr[4], Value::Number(_)));
1133
1134        Ok(())
1135    }
1136
1137    #[test]
1138    fn test_object_field_ordering_in_json() -> Result<(), ArrowError> {
1139        use parquet_variant::VariantBuilder;
1140
1141        let mut builder = VariantBuilder::new();
1142
1143        {
1144            let mut obj = builder.new_object();
1145            // Add fields in non-alphabetical order
1146            obj.insert("zebra", "last");
1147            obj.insert("alpha", "first");
1148            obj.insert("beta", "second");
1149            obj.finish();
1150        }
1151
1152        let (metadata, value) = builder.finish();
1153        let variant = Variant::try_new(&metadata, &value)?;
1154        let json = variant.to_json_string()?;
1155
1156        // Parse and verify all fields are present
1157        let parsed: Value = serde_json::from_str(&json).unwrap();
1158        let obj = parsed.as_object().expect("expected JSON object");
1159        assert_eq!(obj.len(), 3);
1160        assert_eq!(obj.get("alpha"), Some(&Value::String("first".to_string())));
1161        assert_eq!(obj.get("beta"), Some(&Value::String("second".to_string())));
1162        assert_eq!(obj.get("zebra"), Some(&Value::String("last".to_string())));
1163
1164        Ok(())
1165    }
1166
1167    #[test]
1168    fn test_list_with_various_primitive_types_to_json() -> Result<(), ArrowError> {
1169        use parquet_variant::VariantBuilder;
1170
1171        let mut builder = VariantBuilder::new();
1172
1173        builder
1174            .new_list()
1175            .with_value("string_value")
1176            .with_value(42i32)
1177            .with_value(true)
1178            .with_value(std::f64::consts::PI)
1179            .with_value(false)
1180            .with_value(()) // null
1181            .with_value(100i64)
1182            .finish();
1183
1184        let (metadata, value) = builder.finish();
1185        let variant = Variant::try_new(&metadata, &value)?;
1186        let json = variant.to_json_string()?;
1187
1188        let parsed: Value = serde_json::from_str(&json).unwrap();
1189        let arr = parsed.as_array().expect("expected JSON array");
1190        assert_eq!(arr.len(), 7);
1191        assert_eq!(arr[0], Value::String("string_value".to_string()));
1192        assert_eq!(arr[1], Value::Number(42.into()));
1193        assert_eq!(arr[2], Value::Bool(true));
1194        assert!(matches!(arr[3], Value::Number(_))); // float
1195        assert_eq!(arr[4], Value::Bool(false));
1196        assert_eq!(arr[5], Value::Null);
1197        assert_eq!(arr[6], Value::Number(100.into()));
1198
1199        Ok(())
1200    }
1201
1202    #[test]
1203    fn test_object_with_various_primitive_types_to_json() -> Result<(), ArrowError> {
1204        use parquet_variant::VariantBuilder;
1205
1206        let mut builder = VariantBuilder::new();
1207
1208        {
1209            let mut obj = builder.new_object();
1210            obj.insert("string_field", "test_string");
1211            obj.insert("int_field", 123i32);
1212            obj.insert("bool_field", true);
1213            obj.insert("float_field", 2.0f64);
1214            obj.insert("null_field", ());
1215            obj.insert("long_field", 999i64);
1216            obj.finish();
1217        }
1218
1219        let (metadata, value) = builder.finish();
1220        let variant = Variant::try_new(&metadata, &value)?;
1221        let json = variant.to_json_string()?;
1222        assert!(json.contains(r#""float_field":2.0"#));
1223
1224        let parsed: Value = serde_json::from_str(&json).unwrap();
1225        let obj = parsed.as_object().expect("expected JSON object");
1226        assert_eq!(obj.len(), 6);
1227        assert_eq!(
1228            obj.get("string_field"),
1229            Some(&Value::String("test_string".to_string()))
1230        );
1231        assert_eq!(obj.get("int_field"), Some(&Value::Number(123.into())));
1232        assert_eq!(obj.get("bool_field"), Some(&Value::Bool(true)));
1233        assert!(matches!(obj.get("float_field"), Some(Value::Number(_))));
1234        assert_eq!(obj.get("null_field"), Some(&Value::Null));
1235        assert_eq!(obj.get("long_field"), Some(&Value::Number(999.into())));
1236
1237        Ok(())
1238    }
1239
1240    #[test]
1241    fn test_decimal_precision_behavior() -> Result<(), ArrowError> {
1242        // Test case that demonstrates f64 precision limits
1243        // This is a 63-bit precision decimal8 value that f64 cannot represent exactly
1244        let high_precision_decimal8 = Variant::from(VariantDecimal8::try_new(
1245            9007199254740993, // 2^53 + 1, exceeds f64 precision
1246            6,
1247        )?);
1248
1249        let json_string = high_precision_decimal8.to_json_string()?;
1250        let json_value = high_precision_decimal8.to_json_value()?;
1251
1252        // Due to f64 precision limits, we expect precision loss for values > 2^53
1253        // Both functions should produce consistent results (even if not exact)
1254        let parsed: Value = serde_json::from_str(&json_string).unwrap();
1255        assert_eq!(parsed, json_value);
1256
1257        // Test a case that can be exactly represented (integer result)
1258        let exact_decimal = Variant::from(VariantDecimal8::try_new(
1259            1234567890000, // Should result in 1234567.89 (trailing zeros trimmed)
1260            6,
1261        )?);
1262
1263        let json_string_exact = exact_decimal.to_json_string()?;
1264        assert_eq!(json_string_exact, "1234567.89");
1265
1266        // Test integer case (should be exact)
1267        let integer_decimal = Variant::from(VariantDecimal8::try_new(
1268            42000000, // Should result in 42 (integer)
1269            6,
1270        )?);
1271
1272        let json_string_integer = integer_decimal.to_json_string()?;
1273        assert_eq!(json_string_integer, "42");
1274
1275        Ok(())
1276    }
1277
1278    #[test]
1279    fn test_float_nan_inf_handling() -> Result<(), ArrowError> {
1280        // Test NaN handling - should return an error since JSON doesn't support NaN
1281        let nan_variant = Variant::Float(f32::NAN);
1282        let nan_result = nan_variant.to_json_value();
1283        assert!(nan_result.is_err());
1284        assert!(
1285            nan_result
1286                .unwrap_err()
1287                .to_string()
1288                .contains("Invalid float value")
1289        );
1290
1291        // Test positive infinity - should return an error since JSON doesn't support Infinity
1292        let pos_inf_variant = Variant::Float(f32::INFINITY);
1293        let pos_inf_result = pos_inf_variant.to_json_value();
1294        assert!(pos_inf_result.is_err());
1295        assert!(
1296            pos_inf_result
1297                .unwrap_err()
1298                .to_string()
1299                .contains("Invalid float value")
1300        );
1301
1302        // Test negative infinity - should return an error since JSON doesn't support -Infinity
1303        let neg_inf_variant = Variant::Float(f32::NEG_INFINITY);
1304        let neg_inf_result = neg_inf_variant.to_json_value();
1305        assert!(neg_inf_result.is_err());
1306        assert!(
1307            neg_inf_result
1308                .unwrap_err()
1309                .to_string()
1310                .contains("Invalid float value")
1311        );
1312
1313        // Test the same for Double variants
1314        let nan_double_variant = Variant::Double(f64::NAN);
1315        let nan_double_result = nan_double_variant.to_json_value();
1316        assert!(nan_double_result.is_err());
1317        assert!(
1318            nan_double_result
1319                .unwrap_err()
1320                .to_string()
1321                .contains("Invalid double value")
1322        );
1323
1324        let pos_inf_double_variant = Variant::Double(f64::INFINITY);
1325        let pos_inf_double_result = pos_inf_double_variant.to_json_value();
1326        assert!(pos_inf_double_result.is_err());
1327        assert!(
1328            pos_inf_double_result
1329                .unwrap_err()
1330                .to_string()
1331                .contains("Invalid double value")
1332        );
1333
1334        let neg_inf_double_variant = Variant::Double(f64::NEG_INFINITY);
1335        let neg_inf_double_result = neg_inf_double_variant.to_json_value();
1336        assert!(neg_inf_double_result.is_err());
1337        assert!(
1338            neg_inf_double_result
1339                .unwrap_err()
1340                .to_string()
1341                .contains("Invalid double value")
1342        );
1343
1344        // Test normal float values still work
1345        let normal_float = Variant::Float(std::f32::consts::PI);
1346        let normal_result = normal_float.to_json_value()?;
1347        assert!(matches!(normal_result, Value::Number(_)));
1348
1349        let normal_double = Variant::Double(std::f64::consts::E);
1350        let normal_double_result = normal_double.to_json_value()?;
1351        assert!(matches!(normal_double_result, Value::Number(_)));
1352
1353        Ok(())
1354    }
1355}