pub fn variant_to_json_string(
variant: &Variant<'_, '_>,
) -> Result<String, ArrowError>
Expand description
Convert [Variant
] to JSON String
This is a convenience function that converts a Variant to a JSON string.
This is the same as calling variant_to_json
with a Vec
.
It’s the simplest way to get a JSON representation when you just need a String result.
§Arguments
variant
- The Variant value to convert
§Returns
Ok(String)
containing the JSON representationErr
with error details if conversion fails
§Examples
let variant = Variant::Int32(42);
let json = variant_to_json_string(&variant)?;
assert_eq!(json, "42");
§Example: Create a [Variant::Object
] and convert to JSON
This example shows how to create an object with two fields and convert it to JSON:
{
"first_name": "Jiaying",
"last_name": "Li"
}
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 json = variant_to_json_string(&variant)?;
assert_eq!(r#"{"first_name":"Jiaying","last_name":"Li"}"#, json);