pub trait VariantToJson {
// Required methods
fn to_json(&self, buffer: &mut impl Write) -> Result<(), ArrowError>;
fn to_json_string(&self) -> Result<String, ArrowError>;
fn to_json_value(&self) -> Result<Value, ArrowError>;
}
Expand description
Extension trait for converting Variants to JSON
Required Methods§
Sourcefn to_json(&self, buffer: &mut impl Write) -> Result<(), ArrowError>
fn to_json(&self, buffer: &mut impl Write) -> Result<(), ArrowError>
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 VariantToJson::to_json_string
for a convenience function that returns a
JSON string.
§Arguments
writer
- Writer to output JSON tovariant
- The Variant value to convert
§Returns
Ok(())
if successfulErr
with error details if conversion fails
§Examples
let variant = Variant::from("Hello, World!");
let mut buffer = Vec::new();
variant.to_json(&mut buffer)?;
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)?;
assert_eq!(br#"{"first_name":"Jiaying","last_name":"Li"}"#, writer.as_slice());
Sourcefn to_json_string(&self) -> Result<String, ArrowError>
fn to_json_string(&self) -> Result<String, ArrowError>
Convert [Variant
] to JSON String
This is a convenience function that converts a Variant to a JSON string.
This is the same as calling VariantToJson::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()?;
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()?;
assert_eq!(r#"{"first_name":"Jiaying","last_name":"Li"}"#, json);
Sourcefn to_json_value(&self) -> Result<Value, ArrowError>
fn to_json_value(&self) -> Result<Value, ArrowError>
Convert [Variant
] to serde_json::Value
This function converts a Variant to a serde_json::Value
, which is useful
when you need to work with the JSON data programmatically or integrate with
other serde-based JSON processing.
§Arguments
variant
- The Variant value to convert
§Returns
Ok(Value)
containing the JSON valueErr
with error details if conversion fails
§Examples
let variant = Variant::from("hello");
let json_value = variant.to_json_value()?;
assert_eq!(json_value, Value::String("hello".to_string()));
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.