scovich commented on code in PR #7862: URL: https://github.com/apache/arrow-rs/pull/7862#discussion_r2192882471
########## parquet-variant-json/src/from_json.rs: ########## @@ -0,0 +1,690 @@ +// 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 parsing JSON strings as Variant + +use arrow_schema::ArrowError; +use parquet_variant::{ListBuilder, ObjectBuilder, Variant, VariantBuilder, VariantBuilderExt}; +use serde_json::{Number, Value}; + +/// Converts a JSON string to Variant using [`VariantBuilder`]. The resulting `value` and `metadata` +/// buffers can be extracted using `builder.finish()` +/// +/// # Arguments +/// * `json` - The JSON string to parse as Variant. +/// * `variant_builder` - Object of type `VariantBuilder` used to build the vatiant from the JSON +/// string +/// +/// # Returns +/// +/// * `Ok(())` if successful +/// * `Err` with error details if the conversion fails +/// +/// ```rust +/// # use parquet_variant::VariantBuilder; +/// # use parquet_variant_json::{ +/// # json_to_variant, variant_to_json_string, variant_to_json, variant_to_json_value +/// # }; +/// +/// let mut variant_builder = VariantBuilder::new(); +/// let person_string = "{\"name\":\"Alice\", \"age\":30, ".to_string() +/// + "\"email\":\"al...@example.com\", \"is_active\": true, \"score\": 95.7," +/// + "\"additional_info\": null}"; +/// json_to_variant(&person_string, &mut variant_builder)?; +/// +/// let (metadata, value) = variant_builder.finish(); +/// +/// let variant = parquet_variant::Variant::try_new(&metadata, &value)?; +/// +/// let json_result = variant_to_json_string(&variant)?; +/// let json_value = variant_to_json_value(&variant)?; +/// +/// let mut buffer = Vec::new(); +/// variant_to_json(&mut buffer, &variant)?; Review Comment: Hmm. I didn't notice before, but the to-string logic relies on [serde_json::to_string](https://docs.rs/serde_json/latest/serde_json/fn.to_string.html) for proper JSON string escaping. So the code really is tied to `serde_json` crate. Further, `serde_json::to_string` is fallible, which would not play well with [std::fmt::Error](https://doc.rust-lang.org/std/fmt/struct.Error.html) used by `impl Display`: > This type does not support transmission of an error other than that an error occurred. This is because, despite the existence of this error, string formatting is considered an infallible operation. `fmt()` implementors should not return this `Error` unless they received it from their [Formatter](https://doc.rust-lang.org/std/fmt/struct.Formatter.html). The only time your code should create a new instance of this error is when implementing `fmt::Write`, in order to cancel the formatting operation when writing to the underlying stream fails. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org