Skip to content

Commit 671c0b4

Browse files
committed
[CHANGE][ISSUE#7426] Add support to convert variant to JSON
1 parent 1029974 commit 671c0b4

File tree

7 files changed

+661
-10
lines changed

7 files changed

+661
-10
lines changed

parquet-variant/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ rust-version = { workspace = true }
3333
[dependencies]
3434
arrow-schema = "55.1.0"
3535
chrono = { workspace = true }
36+
serde_json = "1.0"
3637

3738
[lib]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! Example showing how to convert Variant values to JSON
19+
20+
use parquet_variant::{
21+
variant_to_json, variant_to_json_string, variant_to_json_value, Variant,
22+
};
23+
24+
fn main() -> Result<(), Box<dyn std::error::Error>> {
25+
let variants = vec![
26+
("Null", Variant::Null),
27+
("Boolean True", Variant::BooleanTrue),
28+
("Boolean False", Variant::BooleanFalse),
29+
("Integer 42", Variant::Int8(42)),
30+
("Negative Integer", Variant::Int8(-123)),
31+
("String", Variant::String("Hello, World!")),
32+
("Short String", Variant::ShortString("Hi!")),
33+
];
34+
35+
for (name, variant) in variants {
36+
let json_string = variant_to_json_string(&variant)?;
37+
println!(" {} -> {}", name, json_string);
38+
}
39+
40+
// Example 2: String escaping
41+
println!("\n🔤 2. String Escaping:");
42+
43+
let special_string = Variant::String("Line 1\nLine 2\tTabbed\r\nWith \"quotes\" and \\backslashes");
44+
let escaped_json = variant_to_json_string(&special_string)?;
45+
println!(" Original: Line 1\\nLine 2\\tTabbed\\r\\nWith \"quotes\" and \\\\backslashes");
46+
println!(" JSON: {}", escaped_json);
47+
48+
// Example 3: Unicode support
49+
println!("\n🌍 3. Unicode Support:");
50+
51+
let unicode_variants = vec![
52+
Variant::String("Hello 世界 🌍"),
53+
Variant::String("Emoji: 💻"),
54+
Variant::String("Math: α + β = γ"),
55+
];
56+
57+
for variant in unicode_variants {
58+
let json_string = variant_to_json_string(&variant)?;
59+
println!(" {}", json_string);
60+
}
61+
62+
let test_variant = Variant::String("Buffer test");
63+
64+
// Write to Vec<u8>
65+
let mut vec_buffer = Vec::new();
66+
variant_to_json(&mut vec_buffer, &test_variant)?;
67+
println!(" Vec<u8> buffer: {}", String::from_utf8(vec_buffer)?);
68+
69+
// Write to String (through write! macro)
70+
let mut string_buffer = String::new();
71+
use std::fmt::Write;
72+
write!(string_buffer, "Prefix: ")?;
73+
74+
// Convert to bytes temporarily to use the Write trait
75+
let mut temp_buffer = Vec::new();
76+
variant_to_json(&mut temp_buffer, &test_variant)?;
77+
string_buffer.push_str(&String::from_utf8(temp_buffer)?);
78+
println!(" String buffer: {}", string_buffer);
79+
80+
let variants_for_value = vec![
81+
Variant::Null,
82+
Variant::BooleanTrue,
83+
Variant::Int8(100),
84+
Variant::String("Value conversion"),
85+
];
86+
87+
for variant in variants_for_value {
88+
let json_value = variant_to_json_value(&variant)?;
89+
println!(" {:?} -> {:?}", variant, json_value);
90+
}
91+
92+
let start = std::time::Instant::now();
93+
for i in 0..1000 {
94+
let variant = Variant::Int8((i % 128) as i8);
95+
let _json = variant_to_json_string(&variant)?;
96+
}
97+
let duration = start.elapsed();
98+
println!(" Converted 1000 variants in {:?}", duration);
99+
100+
// This would demonstrate error handling if we had invalid variants
101+
// For now, all our examples work, so we'll just show the pattern
102+
match variant_to_json_string(&Variant::String("Valid string")) {
103+
Ok(json) => println!(" Success: {}", json),
104+
Err(e) => println!(" Error: {}", e),
105+
}
106+
107+
Ok(())
108+
}

parquet-variant/src/encoder.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! Encoder module for converting Variant values to other formats
19+
20+
pub mod json;

0 commit comments

Comments
 (0)