Skip to main content

vortex_json/
dtype.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Extension dtype definition for the JSON type
5
6use vortex_array::EmptyMetadata;
7use vortex_array::dtype::extension::ExtDType;
8use vortex_array::dtype::extension::ExtId;
9use vortex_array::dtype::extension::ExtVTable;
10use vortex_array::scalar::ScalarValue;
11use vortex_error::VortexResult;
12use vortex_error::vortex_bail;
13use vortex_error::vortex_ensure;
14
15/// JSON logical type backed by UTF-8 string storage.
16#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
17pub struct Json;
18
19impl ExtVTable for Json {
20    type Metadata = EmptyMetadata;
21    type NativeValue<'a> = &'a str;
22
23    fn id(&self) -> ExtId {
24        ExtId::new("vortex.json")
25    }
26
27    fn serialize_metadata(&self, _metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
28        Ok(vec![])
29    }
30
31    fn deserialize_metadata(&self, metadata: &[u8]) -> VortexResult<Self::Metadata> {
32        vortex_ensure!(metadata.is_empty(), "JSON metadata must be empty");
33        Ok(EmptyMetadata)
34    }
35
36    fn validate_dtype(ext_dtype: &ExtDType<Self>) -> VortexResult<()> {
37        vortex_ensure!(
38            ext_dtype.storage_dtype().is_utf8(),
39            "JSON storage dtype must be utf8, got {}",
40            ext_dtype.storage_dtype()
41        );
42        Ok(())
43    }
44
45    fn unpack_native<'a>(
46        _ext_dtype: &'a ExtDType<Self>,
47        storage_value: &'a ScalarValue,
48    ) -> VortexResult<Self::NativeValue<'a>> {
49        let ScalarValue::Utf8(value) = storage_value else {
50            vortex_bail!("JSON storage scalar must be utf8, got {storage_value}");
51        };
52        Ok(value.as_str())
53    }
54}