forked from apache/arrow-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_json.rs
More file actions
1256 lines (1093 loc) · 44 KB
/
to_json.rs
File metadata and controls
1256 lines (1093 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Module for converting Variant data to JSON format
use arrow_schema::ArrowError;
use base64::{engine::general_purpose, Engine as _};
use chrono::Timelike;
use parquet_variant::{Variant, VariantList, VariantObject};
use serde_json::Value;
use std::io::Write;
// Format string constants to avoid duplication and reduce errors
const DATE_FORMAT: &str = "%Y-%m-%d";
const TIMESTAMP_NTZ_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.6f";
// Helper functions for consistent formatting
fn format_date_string(date: &chrono::NaiveDate) -> String {
date.format(DATE_FORMAT).to_string()
}
fn format_timestamp_ntz_string(ts: &chrono::NaiveDateTime) -> String {
ts.format(TIMESTAMP_NTZ_FORMAT).to_string()
}
fn format_binary_base64(bytes: &[u8]) -> String {
general_purpose::STANDARD.encode(bytes)
}
fn format_time_ntz_str(time: &chrono::NaiveTime) -> String {
let base = time.format("%H:%M:%S").to_string();
let micros = time.nanosecond() / 1000;
match micros {
0 => format!("{}.{}", base, 0),
_ => {
let micros_str = format!("{:06}", micros);
let micros_str_trimmed = micros_str.trim_matches('0');
format!("{}.{}", base, micros_str_trimmed)
}
}
}
///
/// This function writes JSON directly to any type that implements [`Write`],
/// making it efficient for streaming or when you want to control the output destination.
///
/// See [`variant_to_json_string`] for a convenience function that returns a
/// JSON string.
///
/// # Arguments
///
/// * `writer` - Writer to output JSON to
/// * `variant` - The Variant value to convert
///
/// # Returns
///
/// * `Ok(())` if successful
/// * `Err` with error details if conversion fails
///
/// # Examples
///
///
/// ```rust
/// # use parquet_variant::{Variant};
/// # use parquet_variant_json::variant_to_json;
/// # use arrow_schema::ArrowError;
/// let variant = Variant::from("Hello, World!");
/// let mut buffer = Vec::new();
/// variant_to_json(&mut buffer, &variant)?;
/// assert_eq!(String::from_utf8(buffer).unwrap(), "\"Hello, World!\"");
/// # Ok::<(), ArrowError>(())
/// ```
///
/// # Example: Create a [`Variant::Object`] and convert to JSON
/// ```rust
/// # use parquet_variant::{Variant, VariantBuilder};
/// # use parquet_variant_json::variant_to_json;
/// # use arrow_schema::ArrowError;
/// let mut builder = VariantBuilder::new();
/// // Create an object builder that will write fields to the object
/// let mut object_builder = builder.new_object();
/// object_builder.insert("first_name", "Jiaying");
/// object_builder.insert("last_name", "Li");
/// object_builder.finish();
/// // Finish the builder to get the metadata and value
/// let (metadata, value) = builder.finish();
/// // Create the Variant and convert to JSON
/// let variant = Variant::try_new(&metadata, &value)?;
/// let mut writer = Vec::new();
/// variant_to_json(&mut writer, &variant,)?;
/// assert_eq!(br#"{"first_name":"Jiaying","last_name":"Li"}"#, writer.as_slice());
/// # Ok::<(), ArrowError>(())
/// ```
pub fn variant_to_json(json_buffer: &mut impl Write, variant: &Variant) -> Result<(), ArrowError> {
match variant {
Variant::Null => write!(json_buffer, "null")?,
Variant::BooleanTrue => write!(json_buffer, "true")?,
Variant::BooleanFalse => write!(json_buffer, "false")?,
Variant::Int8(i) => write!(json_buffer, "{i}")?,
Variant::Int16(i) => write!(json_buffer, "{i}")?,
Variant::Int32(i) => write!(json_buffer, "{i}")?,
Variant::Int64(i) => write!(json_buffer, "{i}")?,
Variant::Float(f) => write!(json_buffer, "{f}")?,
Variant::Double(f) => write!(json_buffer, "{f}")?,
Variant::Decimal4(decimal) => write!(json_buffer, "{decimal}")?,
Variant::Decimal8(decimal) => write!(json_buffer, "{decimal}")?,
Variant::Decimal16(decimal) => write!(json_buffer, "{decimal}")?,
Variant::Date(date) => write!(json_buffer, "\"{}\"", format_date_string(date))?,
Variant::TimestampMicros(ts) => write!(json_buffer, "\"{}\"", ts.to_rfc3339())?,
Variant::TimestampNtzMicros(ts) => {
write!(json_buffer, "\"{}\"", format_timestamp_ntz_string(ts))?
}
Variant::Time(time) => write!(json_buffer, "\"{}\"", format_time_ntz_str(time))?,
Variant::Binary(bytes) => {
// Encode binary as base64 string
let base64_str = format_binary_base64(bytes);
let json_str = serde_json::to_string(&base64_str).map_err(|e| {
ArrowError::InvalidArgumentError(format!("JSON encoding error: {e}"))
})?;
write!(json_buffer, "{json_str}")?
}
Variant::String(s) => {
// Use serde_json to properly escape the string
let json_str = serde_json::to_string(s).map_err(|e| {
ArrowError::InvalidArgumentError(format!("JSON encoding error: {e}"))
})?;
write!(json_buffer, "{json_str}")?
}
Variant::ShortString(s) => {
// Use serde_json to properly escape the string
let json_str = serde_json::to_string(s.as_str()).map_err(|e| {
ArrowError::InvalidArgumentError(format!("JSON encoding error: {e}"))
})?;
write!(json_buffer, "{json_str}")?
}
Variant::Object(obj) => {
convert_object_to_json(json_buffer, obj)?;
}
Variant::List(arr) => {
convert_array_to_json(json_buffer, arr)?;
}
}
Ok(())
}
/// Convert object fields to JSON
fn convert_object_to_json(buffer: &mut impl Write, obj: &VariantObject) -> Result<(), ArrowError> {
write!(buffer, "{{")?;
// Get all fields from the object
let mut first = true;
for (key, value) in obj.iter() {
if !first {
write!(buffer, ",")?;
}
first = false;
// Write the key (properly escaped)
let json_key = serde_json::to_string(key).map_err(|e| {
ArrowError::InvalidArgumentError(format!("JSON key encoding error: {e}"))
})?;
write!(buffer, "{json_key}:")?;
// Recursively convert the value
variant_to_json(buffer, &value)?;
}
write!(buffer, "}}")?;
Ok(())
}
/// Convert array elements to JSON
fn convert_array_to_json(buffer: &mut impl Write, arr: &VariantList) -> Result<(), ArrowError> {
write!(buffer, "[")?;
let mut first = true;
for element in arr.iter() {
if !first {
write!(buffer, ",")?;
}
first = false;
variant_to_json(buffer, &element)?;
}
write!(buffer, "]")?;
Ok(())
}
/// Convert [`Variant`] to JSON [`String`]
///
/// This is a convenience function that converts a Variant to a JSON string.
/// This is the same as calling [`variant_to_json`] with a [`Vec`].
/// It's the simplest way to get a JSON representation when you just need a String result.
///
/// # Arguments
///
/// * `variant` - The Variant value to convert
///
/// # Returns
///
/// * `Ok(String)` containing the JSON representation
/// * `Err` with error details if conversion fails
///
/// # Examples
///
/// ```rust
/// # use parquet_variant::{Variant};
/// # use parquet_variant_json::variant_to_json_string;
/// # use arrow_schema::ArrowError;
/// let variant = Variant::Int32(42);
/// let json = variant_to_json_string(&variant)?;
/// assert_eq!(json, "42");
/// # Ok::<(), ArrowError>(())
/// ```
///
/// # Example: Create a [`Variant::Object`] and convert to JSON
///
/// This example shows how to create an object with two fields and convert it to JSON:
/// ```json
/// {
/// "first_name": "Jiaying",
/// "last_name": "Li"
/// }
/// ```
///
/// ```rust
/// # use parquet_variant::{Variant, VariantBuilder};
/// # use parquet_variant_json::variant_to_json_string;
/// # use arrow_schema::ArrowError;
/// let mut builder = VariantBuilder::new();
/// // Create an object builder that will write fields to the object
/// let mut object_builder = builder.new_object();
/// object_builder.insert("first_name", "Jiaying");
/// object_builder.insert("last_name", "Li");
/// object_builder.finish();
/// // Finish the builder to get the metadata and value
/// let (metadata, value) = builder.finish();
/// // Create the Variant and convert to JSON
/// let variant = Variant::try_new(&metadata, &value)?;
/// let json = variant_to_json_string(&variant)?;
/// assert_eq!(r#"{"first_name":"Jiaying","last_name":"Li"}"#, json);
/// # Ok::<(), ArrowError>(())
/// ```
pub fn variant_to_json_string(variant: &Variant) -> Result<String, ArrowError> {
let mut buffer = Vec::new();
variant_to_json(&mut buffer, variant)?;
String::from_utf8(buffer)
.map_err(|e| ArrowError::InvalidArgumentError(format!("UTF-8 conversion error: {e}")))
}
/// Convert [`Variant`] to [`serde_json::Value`]
///
/// This function converts a Variant to a [`serde_json::Value`], which is useful
/// when you need to work with the JSON data programmatically or integrate with
/// other serde-based JSON processing.
///
/// # Arguments
///
/// * `variant` - The Variant value to convert
///
/// # Returns
///
/// * `Ok(Value)` containing the JSON value
/// * `Err` with error details if conversion fails
///
/// # Examples
///
/// ```rust
/// # use parquet_variant::{Variant};
/// # use parquet_variant_json::variant_to_json_value;
/// # use serde_json::Value;
/// # use arrow_schema::ArrowError;
/// let variant = Variant::from("hello");
/// let json_value = variant_to_json_value(&variant)?;
/// assert_eq!(json_value, Value::String("hello".to_string()));
/// # Ok::<(), ArrowError>(())
/// ```
pub fn variant_to_json_value(variant: &Variant) -> Result<Value, ArrowError> {
match variant {
Variant::Null => Ok(Value::Null),
Variant::BooleanTrue => Ok(Value::Bool(true)),
Variant::BooleanFalse => Ok(Value::Bool(false)),
Variant::Int8(i) => Ok(Value::Number((*i).into())),
Variant::Int16(i) => Ok(Value::Number((*i).into())),
Variant::Int32(i) => Ok(Value::Number((*i).into())),
Variant::Int64(i) => Ok(Value::Number((*i).into())),
Variant::Float(f) => serde_json::Number::from_f64((*f).into())
.map(Value::Number)
.ok_or_else(|| ArrowError::InvalidArgumentError("Invalid float value".to_string())),
Variant::Double(f) => serde_json::Number::from_f64(*f)
.map(Value::Number)
.ok_or_else(|| ArrowError::InvalidArgumentError("Invalid double value".to_string())),
Variant::Decimal4(decimal4) => {
let scale = decimal4.scale();
let integer = decimal4.integer();
let integer = if scale == 0 {
integer
} else {
let divisor = 10_i32.pow(scale as u32);
if integer % divisor != 0 {
// fall back to floating point
return Ok(Value::from(integer as f64 / divisor as f64));
}
integer / divisor
};
Ok(Value::from(integer))
}
Variant::Decimal8(decimal8) => {
let scale = decimal8.scale();
let integer = decimal8.integer();
let integer = if scale == 0 {
integer
} else {
let divisor = 10_i64.pow(scale as u32);
if integer % divisor != 0 {
// fall back to floating point
return Ok(Value::from(integer as f64 / divisor as f64));
}
integer / divisor
};
Ok(Value::from(integer))
}
Variant::Decimal16(decimal16) => {
let scale = decimal16.scale();
let integer = decimal16.integer();
let integer = if scale == 0 {
integer
} else {
let divisor = 10_i128.pow(scale as u32);
if integer % divisor != 0 {
// fall back to floating point
return Ok(Value::from(integer as f64 / divisor as f64));
}
integer / divisor
};
// i128 has higher precision than any 64-bit type. Try a lossless narrowing cast to
// i64 or u64 first, falling back to a lossy narrowing cast to f64 if necessary.
let value = i64::try_from(integer)
.map(Value::from)
.or_else(|_| u64::try_from(integer).map(Value::from))
.unwrap_or_else(|_| Value::from(integer as f64));
Ok(value)
}
Variant::Date(date) => Ok(Value::String(format_date_string(date))),
Variant::TimestampMicros(ts) => Ok(Value::String(ts.to_rfc3339())),
Variant::TimestampNtzMicros(ts) => Ok(Value::String(format_timestamp_ntz_string(ts))),
Variant::Time(time) => Ok(Value::String(format_time_ntz_str(time))),
Variant::Binary(bytes) => Ok(Value::String(format_binary_base64(bytes))),
Variant::String(s) => Ok(Value::String(s.to_string())),
Variant::ShortString(s) => Ok(Value::String(s.to_string())),
Variant::Object(obj) => {
let map = obj
.iter()
.map(|(k, v)| variant_to_json_value(&v).map(|json_val| (k.to_string(), json_val)))
.collect::<Result<_, _>>()?;
Ok(Value::Object(map))
}
Variant::List(arr) => {
let vec = arr
.iter()
.map(|element| variant_to_json_value(&element))
.collect::<Result<_, _>>()?;
Ok(Value::Array(vec))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
use parquet_variant::{VariantDecimal16, VariantDecimal4, VariantDecimal8};
#[test]
fn test_decimal_edge_cases() -> Result<(), ArrowError> {
// Test negative decimal
let negative_variant = Variant::from(VariantDecimal4::try_new(-12345, 3)?);
let negative_json = variant_to_json_string(&negative_variant)?;
assert_eq!(negative_json, "-12.345");
// Test large scale decimal
let large_scale_variant = Variant::from(VariantDecimal8::try_new(123456789, 6)?);
let large_scale_json = variant_to_json_string(&large_scale_variant)?;
assert_eq!(large_scale_json, "123.456789");
Ok(())
}
#[test]
fn test_decimal16_to_json() -> Result<(), ArrowError> {
let variant = Variant::from(VariantDecimal16::try_new(123456789012345, 4)?);
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "12345678901.2345");
let json_value = variant_to_json_value(&variant)?;
assert!(matches!(json_value, Value::Number(_)));
// Test very large number
let large_variant = Variant::from(VariantDecimal16::try_new(999999999999999999, 2)?);
let large_json = variant_to_json_string(&large_variant)?;
// Due to f64 precision limits, very large numbers may lose precision
assert!(
large_json.starts_with("9999999999999999")
|| large_json.starts_with("10000000000000000")
);
Ok(())
}
#[test]
fn test_date_to_json() -> Result<(), ArrowError> {
let date = NaiveDate::from_ymd_opt(2023, 12, 25).unwrap();
let variant = Variant::Date(date);
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "\"2023-12-25\"");
let json_value = variant_to_json_value(&variant)?;
assert_eq!(json_value, Value::String("2023-12-25".to_string()));
// Test leap year date
let leap_date = NaiveDate::from_ymd_opt(2024, 2, 29).unwrap();
let leap_variant = Variant::Date(leap_date);
let leap_json = variant_to_json_string(&leap_variant)?;
assert_eq!(leap_json, "\"2024-02-29\"");
Ok(())
}
#[test]
fn test_timestamp_micros_to_json() -> Result<(), ArrowError> {
let timestamp = DateTime::parse_from_rfc3339("2023-12-25T10:30:45Z")
.unwrap()
.with_timezone(&Utc);
let variant = Variant::TimestampMicros(timestamp);
let json = variant_to_json_string(&variant)?;
assert!(json.contains("2023-12-25T10:30:45"));
assert!(json.starts_with('"') && json.ends_with('"'));
let json_value = variant_to_json_value(&variant)?;
assert!(matches!(json_value, Value::String(_)));
Ok(())
}
#[test]
fn test_timestamp_ntz_micros_to_json() -> Result<(), ArrowError> {
let naive_timestamp = DateTime::from_timestamp(1703505045, 123456)
.unwrap()
.naive_utc();
let variant = Variant::TimestampNtzMicros(naive_timestamp);
let json = variant_to_json_string(&variant)?;
assert!(json.contains("2023-12-25"));
assert!(json.starts_with('"') && json.ends_with('"'));
let json_value = variant_to_json_value(&variant)?;
assert!(matches!(json_value, Value::String(_)));
Ok(())
}
#[test]
fn test_time_to_json() -> Result<(), ArrowError> {
let naive_time = NaiveTime::from_num_seconds_from_midnight_opt(12345, 123460708).unwrap();
let variant = Variant::Time(naive_time);
let json = variant_to_json_string(&variant)?;
assert_eq!("\"03:25:45.12346\"", json);
let json_value = variant_to_json_value(&variant)?;
assert!(matches!(json_value, Value::String(_)));
Ok(())
}
#[test]
fn test_binary_to_json() -> Result<(), ArrowError> {
let binary_data = b"Hello, World!";
let variant = Variant::Binary(binary_data);
let json = variant_to_json_string(&variant)?;
// Should be base64 encoded and quoted
assert!(json.starts_with('"') && json.ends_with('"'));
assert!(json.len() > 2); // Should have content
let json_value = variant_to_json_value(&variant)?;
assert!(matches!(json_value, Value::String(_)));
// Test empty binary
let empty_variant = Variant::Binary(b"");
let empty_json = variant_to_json_string(&empty_variant)?;
assert_eq!(empty_json, "\"\"");
// Test binary with special bytes
let special_variant = Variant::Binary(&[0, 255, 128, 64]);
let special_json = variant_to_json_string(&special_variant)?;
assert!(special_json.starts_with('"') && special_json.ends_with('"'));
Ok(())
}
#[test]
fn test_string_to_json() -> Result<(), ArrowError> {
let variant = Variant::from("hello world");
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "\"hello world\"");
let json_value = variant_to_json_value(&variant)?;
assert_eq!(json_value, Value::String("hello world".to_string()));
Ok(())
}
#[test]
fn test_short_string_to_json() -> Result<(), ArrowError> {
use parquet_variant::ShortString;
let short_string = ShortString::try_new("short")?;
let variant = Variant::ShortString(short_string);
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "\"short\"");
let json_value = variant_to_json_value(&variant)?;
assert_eq!(json_value, Value::String("short".to_string()));
Ok(())
}
#[test]
fn test_string_escaping() -> Result<(), ArrowError> {
let variant = Variant::from("hello\nworld\t\"quoted\"");
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "\"hello\\nworld\\t\\\"quoted\\\"\"");
let json_value = variant_to_json_value(&variant)?;
assert_eq!(
json_value,
Value::String("hello\nworld\t\"quoted\"".to_string())
);
Ok(())
}
#[test]
fn test_json_buffer_writing() -> Result<(), ArrowError> {
let variant = Variant::Int8(123);
let mut buffer = Vec::new();
variant_to_json(&mut buffer, &variant)?;
let result = String::from_utf8(buffer)
.map_err(|e| ArrowError::InvalidArgumentError(e.to_string()))?;
assert_eq!(result, "123");
Ok(())
}
/// Reusable test structure for JSON conversion testing
struct JsonTest {
variant: Variant<'static, 'static>,
expected_json: &'static str,
expected_value: Value,
}
impl JsonTest {
fn run(self) {
let json_string = variant_to_json_string(&self.variant)
.expect("variant_to_json_string should succeed");
assert_eq!(
json_string, self.expected_json,
"JSON string mismatch for variant: {:?}",
self.variant
);
let json_value =
variant_to_json_value(&self.variant).expect("variant_to_json_value should succeed");
// For floating point numbers, we need special comparison due to JSON number representation
match (&json_value, &self.expected_value) {
(Value::Number(actual), Value::Number(expected)) => {
let actual_f64 = actual.as_f64().unwrap_or(0.0);
let expected_f64 = expected.as_f64().unwrap_or(0.0);
assert!(
(actual_f64 - expected_f64).abs() < f64::EPSILON,
"JSON value mismatch for variant: {:?}, got {}, expected {}",
self.variant,
actual_f64,
expected_f64
);
}
_ => {
assert_eq!(
json_value, self.expected_value,
"JSON value mismatch for variant: {:?}",
self.variant
);
}
}
// Verify roundtrip: JSON string should parse to same value
let parsed: Value =
serde_json::from_str(&json_string).expect("Generated JSON should be valid");
// Same floating point handling for roundtrip
match (&parsed, &self.expected_value) {
(Value::Number(actual), Value::Number(expected)) => {
let actual_f64 = actual.as_f64().unwrap_or(0.0);
let expected_f64 = expected.as_f64().unwrap_or(0.0);
assert!(
(actual_f64 - expected_f64).abs() < f64::EPSILON,
"Parsed JSON mismatch for variant: {:?}, got {}, expected {}",
self.variant,
actual_f64,
expected_f64
);
}
_ => {
assert_eq!(
parsed, self.expected_value,
"Parsed JSON mismatch for variant: {:?}",
self.variant
);
}
}
}
}
#[test]
fn test_primitive_json_conversion() {
use parquet_variant::ShortString;
// Null
JsonTest {
variant: Variant::Null,
expected_json: "null",
expected_value: Value::Null,
}
.run();
// Booleans
JsonTest {
variant: Variant::BooleanTrue,
expected_json: "true",
expected_value: Value::Bool(true),
}
.run();
JsonTest {
variant: Variant::BooleanFalse,
expected_json: "false",
expected_value: Value::Bool(false),
}
.run();
// Integers - positive and negative edge cases
JsonTest {
variant: Variant::Int8(42),
expected_json: "42",
expected_value: Value::Number(42.into()),
}
.run();
JsonTest {
variant: Variant::Int8(-128),
expected_json: "-128",
expected_value: Value::Number((-128).into()),
}
.run();
JsonTest {
variant: Variant::Int16(32767),
expected_json: "32767",
expected_value: Value::Number(32767.into()),
}
.run();
JsonTest {
variant: Variant::Int16(-32768),
expected_json: "-32768",
expected_value: Value::Number((-32768).into()),
}
.run();
JsonTest {
variant: Variant::Int32(2147483647),
expected_json: "2147483647",
expected_value: Value::Number(2147483647.into()),
}
.run();
JsonTest {
variant: Variant::Int32(-2147483648),
expected_json: "-2147483648",
expected_value: Value::Number((-2147483648).into()),
}
.run();
JsonTest {
variant: Variant::Int64(9223372036854775807),
expected_json: "9223372036854775807",
expected_value: Value::Number(9223372036854775807i64.into()),
}
.run();
JsonTest {
variant: Variant::Int64(-9223372036854775808),
expected_json: "-9223372036854775808",
expected_value: Value::Number((-9223372036854775808i64).into()),
}
.run();
// Floats
JsonTest {
variant: Variant::Float(3.5),
expected_json: "3.5",
expected_value: serde_json::Number::from_f64(3.5)
.map(Value::Number)
.unwrap(),
}
.run();
JsonTest {
variant: Variant::Float(0.0),
expected_json: "0",
expected_value: Value::Number(0.into()), // Use integer 0 to match JSON parsing
}
.run();
JsonTest {
variant: Variant::Float(-1.5),
expected_json: "-1.5",
expected_value: serde_json::Number::from_f64(-1.5)
.map(Value::Number)
.unwrap(),
}
.run();
JsonTest {
variant: Variant::Double(std::f64::consts::E),
expected_json: "2.718281828459045",
expected_value: serde_json::Number::from_f64(std::f64::consts::E)
.map(Value::Number)
.unwrap(),
}
.run();
// Decimals
JsonTest {
variant: Variant::from(VariantDecimal4::try_new(12345, 2).unwrap()),
expected_json: "123.45",
expected_value: serde_json::Number::from_f64(123.45)
.map(Value::Number)
.unwrap(),
}
.run();
JsonTest {
variant: Variant::from(VariantDecimal4::try_new(42, 0).unwrap()),
expected_json: "42",
expected_value: serde_json::Number::from_f64(42.0)
.map(Value::Number)
.unwrap(),
}
.run();
JsonTest {
variant: Variant::from(VariantDecimal8::try_new(1234567890, 3).unwrap()),
expected_json: "1234567.89",
expected_value: serde_json::Number::from_f64(1234567.89)
.map(Value::Number)
.unwrap(),
}
.run();
JsonTest {
variant: Variant::from(VariantDecimal16::try_new(123456789012345, 4).unwrap()),
expected_json: "12345678901.2345",
expected_value: serde_json::Number::from_f64(12345678901.2345)
.map(Value::Number)
.unwrap(),
}
.run();
// Strings
JsonTest {
variant: Variant::from("hello world"),
expected_json: "\"hello world\"",
expected_value: Value::String("hello world".to_string()),
}
.run();
JsonTest {
variant: Variant::from(""),
expected_json: "\"\"",
expected_value: Value::String("".to_string()),
}
.run();
JsonTest {
variant: Variant::ShortString(ShortString::try_new("test").unwrap()),
expected_json: "\"test\"",
expected_value: Value::String("test".to_string()),
}
.run();
// Date and timestamps
JsonTest {
variant: Variant::Date(NaiveDate::from_ymd_opt(2023, 12, 25).unwrap()),
expected_json: "\"2023-12-25\"",
expected_value: Value::String("2023-12-25".to_string()),
}
.run();
// Binary data (base64 encoded)
JsonTest {
variant: Variant::Binary(b"test"),
expected_json: "\"dGVzdA==\"", // base64 encoded "test"
expected_value: Value::String("dGVzdA==".to_string()),
}
.run();
JsonTest {
variant: Variant::Binary(b""),
expected_json: "\"\"", // empty base64
expected_value: Value::String("".to_string()),
}
.run();
JsonTest {
variant: Variant::Binary(b"binary data"),
expected_json: "\"YmluYXJ5IGRhdGE=\"", // base64 encoded "binary data"
expected_value: Value::String("YmluYXJ5IGRhdGE=".to_string()),
}
.run();
}
#[test]
fn test_string_escaping_comprehensive() {
// Test comprehensive string escaping scenarios
JsonTest {
variant: Variant::from("line1\nline2\ttab\"quote\"\\backslash"),
expected_json: "\"line1\\nline2\\ttab\\\"quote\\\"\\\\backslash\"",
expected_value: Value::String("line1\nline2\ttab\"quote\"\\backslash".to_string()),
}
.run();
JsonTest {
variant: Variant::from("Hello 世界 🌍"),
expected_json: "\"Hello 世界 🌍\"",
expected_value: Value::String("Hello 世界 🌍".to_string()),
}
.run();
}
#[test]
fn test_buffer_writing_variants() -> Result<(), ArrowError> {
use crate::variant_to_json;
let variant = Variant::from("test buffer writing");
// Test writing to a Vec<u8>
let mut buffer = Vec::new();
variant_to_json(&mut buffer, &variant)?;
let result = String::from_utf8(buffer)
.map_err(|e| ArrowError::InvalidArgumentError(e.to_string()))?;
assert_eq!(result, "\"test buffer writing\"");
// Test writing to vec![]
let mut buffer = vec![];
variant_to_json(&mut buffer, &variant)?;
let result = String::from_utf8(buffer)
.map_err(|e| ArrowError::InvalidArgumentError(e.to_string()))?;
assert_eq!(result, "\"test buffer writing\"");
Ok(())
}
#[test]
fn test_simple_object_to_json() -> Result<(), ArrowError> {
use parquet_variant::VariantBuilder;
// Create a simple object with various field types
let mut builder = VariantBuilder::new();
builder
.new_object()
.with_field("name", "Alice")
.with_field("age", 30i32)
.with_field("active", true)
.with_field("score", 95.5f64)
.finish()
.unwrap();
let (metadata, value) = builder.finish();
let variant = Variant::try_new(&metadata, &value)?;
let json = variant_to_json_string(&variant)?;
// Parse the JSON to verify structure - handle JSON parsing errors manually
let parsed: Value = serde_json::from_str(&json).unwrap();
let obj = parsed.as_object().expect("expected JSON object");
assert_eq!(obj.get("name"), Some(&Value::String("Alice".to_string())));
assert_eq!(obj.get("age"), Some(&Value::Number(30.into())));
assert_eq!(obj.get("active"), Some(&Value::Bool(true)));
assert!(matches!(obj.get("score"), Some(Value::Number(_))));
assert_eq!(obj.len(), 4);
// Test variant_to_json_value as well
let json_value = variant_to_json_value(&variant)?;
assert!(matches!(json_value, Value::Object(_)));
Ok(())
}
#[test]
fn test_empty_object_to_json() -> Result<(), ArrowError> {
use parquet_variant::VariantBuilder;
let mut builder = VariantBuilder::new();
{
let obj = builder.new_object();
obj.finish().unwrap();
}
let (metadata, value) = builder.finish();
let variant = Variant::try_new(&metadata, &value)?;
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "{}");
let json_value = variant_to_json_value(&variant)?;
assert_eq!(json_value, Value::Object(serde_json::Map::new()));
Ok(())
}
#[test]
fn test_object_with_special_characters_to_json() -> Result<(), ArrowError> {
use parquet_variant::VariantBuilder;
let mut builder = VariantBuilder::new();
builder
.new_object()
.with_field("message", "Hello \"World\"\nWith\tTabs")
.with_field("path", "C:\\Users\\Alice\\Documents")
.with_field("unicode", "😀 Smiley")
.finish()
.unwrap();
let (metadata, value) = builder.finish();
let variant = Variant::try_new(&metadata, &value)?;
let json = variant_to_json_string(&variant)?;
// Verify that special characters are properly escaped
assert!(json.contains("Hello \\\"World\\\"\\nWith\\tTabs"));
assert!(json.contains("C:\\\\Users\\\\Alice\\\\Documents"));
assert!(json.contains("😀 Smiley"));
// Verify that the JSON can be parsed back
let parsed: Value = serde_json::from_str(&json).unwrap();
assert!(matches!(parsed, Value::Object(_)));
Ok(())
}
#[test]
fn test_simple_list_to_json() -> Result<(), ArrowError> {
use parquet_variant::VariantBuilder;
let mut builder = VariantBuilder::new();
builder
.new_list()
.with_value(1i32)
.with_value(2i32)
.with_value(3i32)
.with_value(4i32)
.with_value(5i32)
.finish();
let (metadata, value) = builder.finish();
let variant = Variant::try_new(&metadata, &value)?;
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "[1,2,3,4,5]");
let json_value = variant_to_json_value(&variant)?;
let arr = json_value.as_array().expect("expected JSON array");
assert_eq!(arr.len(), 5);
assert_eq!(arr[0], Value::Number(1.into()));
assert_eq!(arr[4], Value::Number(5.into()));
Ok(())
}
#[test]
fn test_empty_list_to_json() -> Result<(), ArrowError> {
use parquet_variant::VariantBuilder;