Function variant_to_json

Source
pub fn variant_to_json(
    json_buffer: &mut impl Write,
    variant: &Variant<'_, '_>,
) -> Result<(), ArrowError>
Expand description

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

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!\"");

§Example: Create a [Variant::Object] and convert to JSON

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());