Skip to content

Commit 72e91fc

Browse files
authored
[Variant] Consolidate examples for json writing (#7782)
# Which issue does this PR close? We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. - Follow on to #7670 from @carpecodeum - Part of #6736 # Rationale for this change I was going through the code and examples and I felt that there was some redundancy and that the example file was unlikely to be found as not many crates in this repo have examples I would like to propose moving the examples as close to the actual code as possible to give it the best chance to be discovered. # What changes are included in this PR? 1. Remove `parquet-variant/examples/variant_to_json_examples.rs` 2. Update some of the other examples and docs for the json functions with content from that example # Are these changes tested? The examples are covered by CI tests. # Are there any user-facing changes? Different docs
1 parent 1c397a9 commit 72e91fc

File tree

2 files changed

+35
-97
lines changed

2 files changed

+35
-97
lines changed

parquet-variant/examples/variant_to_json_examples.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

parquet-variant/src/to_json.rs

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717

1818
//! Module for converting Variant data to JSON format
19-
2019
use arrow_schema::ArrowError;
2120
use base64::{engine::general_purpose, Engine as _};
2221
use serde_json::Value;
@@ -86,14 +85,17 @@ fn write_decimal_i64(
8685
Ok(())
8786
}
8887

89-
/// Converts a Variant to JSON and writes it to the provided `Write`
88+
/// Converts a Variant to JSON and writes it to the provided [`Write`]
9089
///
9190
/// This function writes JSON directly to any type that implements [`Write`],
9291
/// making it efficient for streaming or when you want to control the output destination.
9392
///
93+
/// See [`variant_to_json_string`] for a convenience function that returns a
94+
/// JSON string.
95+
///
9496
/// # Arguments
9597
///
96-
/// * `json_buffer` - Writer to output JSON to
98+
/// * `writer` - Writer to output JSON to
9799
/// * `variant` - The Variant value to convert
98100
///
99101
/// # Returns
@@ -103,23 +105,34 @@ fn write_decimal_i64(
103105
///
104106
/// # Examples
105107
///
108+
///
106109
/// ```rust
107110
/// # use parquet_variant::{Variant, variant_to_json};
108111
/// # use arrow_schema::ArrowError;
109-
/// let variant = Variant::Int32(42);
112+
/// let variant = Variant::from("Hello, World!");
110113
/// let mut buffer = Vec::new();
111114
/// variant_to_json(&mut buffer, &variant)?;
112-
/// assert_eq!(String::from_utf8(buffer).unwrap(), "42");
115+
/// assert_eq!(String::from_utf8(buffer).unwrap(), "\"Hello, World!\"");
113116
/// # Ok::<(), ArrowError>(())
114117
/// ```
115118
///
119+
/// # Example: Create a [`Variant::Object`] and convert to JSON
116120
/// ```rust
117-
/// # use parquet_variant::{Variant, variant_to_json};
121+
/// # use parquet_variant::{Variant, VariantBuilder, variant_to_json};
118122
/// # use arrow_schema::ArrowError;
119-
/// let variant = Variant::String("Hello, World!");
120-
/// let mut buffer = Vec::new();
121-
/// variant_to_json(&mut buffer, &variant)?;
122-
/// assert_eq!(String::from_utf8(buffer).unwrap(), "\"Hello, World!\"");
123+
/// let mut builder = VariantBuilder::new();
124+
/// // Create an object builder that will write fields to the object
125+
/// let mut object_builder = builder.new_object();
126+
/// object_builder.insert("first_name", "Jiaying");
127+
/// object_builder.insert("last_name", "Li");
128+
/// object_builder.finish();
129+
/// // Finish the builder to get the metadata and value
130+
/// let (metadata, value) = builder.finish();
131+
/// // Create the Variant and convert to JSON
132+
/// let variant = Variant::try_new(&metadata, &value)?;
133+
/// let mut writer = Vec::new();
134+
/// variant_to_json(&mut writer, &variant,)?;
135+
/// assert_eq!(br#"{"first_name":"Jiaying","last_name":"Li"}"#, writer.as_slice());
123136
/// # Ok::<(), ArrowError>(())
124137
/// ```
125138
pub fn variant_to_json(json_buffer: &mut impl Write, variant: &Variant) -> Result<(), ArrowError> {
@@ -243,10 +256,10 @@ fn convert_array_to_json(buffer: &mut impl Write, arr: &VariantList) -> Result<(
243256
Ok(())
244257
}
245258

246-
/// Convert Variant to JSON string
259+
/// Convert [`Variant`] to JSON [`String`]
247260
///
248261
/// This is a convenience function that converts a Variant to a JSON string.
249-
/// This is the same as calling variant_to_json with a Vec
262+
/// This is the same as calling [`variant_to_json`] with a [`Vec`].
250263
/// It's the simplest way to get a JSON representation when you just need a String result.
251264
///
252265
/// # Arguments
@@ -269,15 +282,6 @@ fn convert_array_to_json(buffer: &mut impl Write, arr: &VariantList) -> Result<(
269282
/// # Ok::<(), ArrowError>(())
270283
/// ```
271284
///
272-
/// ```rust
273-
/// # use parquet_variant::{Variant, variant_to_json_string};
274-
/// # use arrow_schema::ArrowError;
275-
/// let variant = Variant::String("Hello, World!");
276-
/// let json = variant_to_json_string(&variant)?;
277-
/// assert_eq!(json, "\"Hello, World!\"");
278-
/// # Ok::<(), ArrowError>(())
279-
/// ```
280-
///
281285
/// # Example: Create a [`Variant::Object`] and convert to JSON
282286
///
283287
/// This example shows how to create an object with two fields and convert it to JSON:
@@ -302,8 +306,7 @@ fn convert_array_to_json(buffer: &mut impl Write, arr: &VariantList) -> Result<(
302306
/// // Create the Variant and convert to JSON
303307
/// let variant = Variant::try_new(&metadata, &value)?;
304308
/// let json = variant_to_json_string(&variant)?;
305-
/// assert!(json.contains("\"first_name\":\"Jiaying\""));
306-
/// assert!(json.contains("\"last_name\":\"Li\""));
309+
/// assert_eq!(r#"{"first_name":"Jiaying","last_name":"Li"}"#, json);
307310
/// # Ok::<(), ArrowError>(())
308311
/// ```
309312
pub fn variant_to_json_string(variant: &Variant) -> Result<String, ArrowError> {
@@ -313,7 +316,7 @@ pub fn variant_to_json_string(variant: &Variant) -> Result<String, ArrowError> {
313316
.map_err(|e| ArrowError::InvalidArgumentError(format!("UTF-8 conversion error: {}", e)))
314317
}
315318

316-
/// Convert Variant to serde_json::Value
319+
/// Convert [`Variant`] to [`serde_json::Value`]
317320
///
318321
/// This function converts a Variant to a [`serde_json::Value`], which is useful
319322
/// when you need to work with the JSON data programmatically or integrate with
@@ -334,17 +337,7 @@ pub fn variant_to_json_string(variant: &Variant) -> Result<String, ArrowError> {
334337
/// # use parquet_variant::{Variant, variant_to_json_value};
335338
/// # use serde_json::Value;
336339
/// # use arrow_schema::ArrowError;
337-
/// let variant = Variant::Int32(42);
338-
/// let json_value = variant_to_json_value(&variant)?;
339-
/// assert_eq!(json_value, Value::Number(42.into()));
340-
/// # Ok::<(), ArrowError>(())
341-
/// ```
342-
///
343-
/// ```rust
344-
/// # use parquet_variant::{Variant, variant_to_json_value};
345-
/// # use serde_json::Value;
346-
/// # use arrow_schema::ArrowError;
347-
/// let variant = Variant::String("hello");
340+
/// let variant = Variant::from("hello");
348341
/// let json_value = variant_to_json_value(&variant)?;
349342
/// assert_eq!(json_value, Value::String("hello".to_string()));
350343
/// # Ok::<(), ArrowError>(())
@@ -547,7 +540,7 @@ mod tests {
547540

548541
#[test]
549542
fn test_string_to_json() -> Result<(), ArrowError> {
550-
let variant = Variant::String("hello world");
543+
let variant = Variant::from("hello world");
551544
let json = variant_to_json_string(&variant)?;
552545
assert_eq!(json, "\"hello world\"");
553546

@@ -571,7 +564,7 @@ mod tests {
571564

572565
#[test]
573566
fn test_string_escaping() -> Result<(), ArrowError> {
574-
let variant = Variant::String("hello\nworld\t\"quoted\"");
567+
let variant = Variant::from("hello\nworld\t\"quoted\"");
575568
let json = variant_to_json_string(&variant)?;
576569
assert_eq!(json, "\"hello\\nworld\\t\\\"quoted\\\"\"");
577570

@@ -822,14 +815,14 @@ mod tests {
822815

823816
// Strings
824817
JsonTest {
825-
variant: Variant::String("hello world"),
818+
variant: Variant::from("hello world"),
826819
expected_json: "\"hello world\"",
827820
expected_value: Value::String("hello world".to_string()),
828821
}
829822
.run();
830823

831824
JsonTest {
832-
variant: Variant::String(""),
825+
variant: Variant::from(""),
833826
expected_json: "\"\"",
834827
expected_value: Value::String("".to_string()),
835828
}
@@ -877,14 +870,14 @@ mod tests {
877870
fn test_string_escaping_comprehensive() {
878871
// Test comprehensive string escaping scenarios
879872
JsonTest {
880-
variant: Variant::String("line1\nline2\ttab\"quote\"\\backslash"),
873+
variant: Variant::from("line1\nline2\ttab\"quote\"\\backslash"),
881874
expected_json: "\"line1\\nline2\\ttab\\\"quote\\\"\\\\backslash\"",
882875
expected_value: Value::String("line1\nline2\ttab\"quote\"\\backslash".to_string()),
883876
}
884877
.run();
885878

886879
JsonTest {
887-
variant: Variant::String("Hello 世界 🌍"),
880+
variant: Variant::from("Hello 世界 🌍"),
888881
expected_json: "\"Hello 世界 🌍\"",
889882
expected_value: Value::String("Hello 世界 🌍".to_string()),
890883
}
@@ -895,7 +888,7 @@ mod tests {
895888
fn test_buffer_writing_variants() -> Result<(), ArrowError> {
896889
use crate::variant_to_json;
897890

898-
let variant = Variant::String("test buffer writing");
891+
let variant = Variant::from("test buffer writing");
899892

900893
// Test writing to a Vec<u8>
901894
let mut buffer = Vec::new();

0 commit comments

Comments
 (0)