|
| 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, VariantBuilder, |
| 22 | +}; |
| 23 | + |
| 24 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 25 | + let mut builder = VariantBuilder::new(); |
| 26 | + |
| 27 | + { |
| 28 | + let mut person = builder.new_object(); |
| 29 | + person.append_value("name", "Alice"); |
| 30 | + person.append_value("age", 30i32); |
| 31 | + person .append_value("email", "[email protected]"); |
| 32 | + person.append_value("is_active", true); |
| 33 | + person.append_value("score", 95.7f64); |
| 34 | + person.append_value("department", "Engineering"); |
| 35 | + person.finish(); |
| 36 | + } |
| 37 | + |
| 38 | + let (metadata, value) = builder.finish(); |
| 39 | + let variant = parquet_variant::Variant::try_new(&metadata, &value)?; |
| 40 | + |
| 41 | + let json_string = variant_to_json_string(&variant)?; |
| 42 | + let json_value = variant_to_json_value(&variant)?; |
| 43 | + let pretty_json = serde_json::to_string_pretty(&json_value)?; |
| 44 | + println!("{}", pretty_json); |
| 45 | + |
| 46 | + let mut buffer = Vec::new(); |
| 47 | + variant_to_json(&mut buffer, &variant)?; |
| 48 | + let buffer_result = String::from_utf8(buffer)?; |
| 49 | + |
| 50 | + // Verify all methods produce the same result |
| 51 | + assert_eq!(json_string, buffer_result); |
| 52 | + assert_eq!(json_string, serde_json::to_string(&json_value)?); |
| 53 | + |
| 54 | + Ok(()) |
| 55 | +} |
0 commit comments