-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Variant] Rename batch_json_string_to_variant and batch_variant_to_json_string
#8161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
856cc47
[Variant] Rename `batch_json_string_to_variant` and `batch_variant_to…
liamzwbao df3a13f
Replace all the usages
liamzwbao 28eaa29
Rename batch functions
liamzwbao cc1d02b
Rename `from_json` to `with_json`
liamzwbao 6e18fe5
Refactor `from_json`
liamzwbao 0042eb1
Refactor `to_json`
liamzwbao 4383f2b
Rename tests
liamzwbao 3608f2f
Merge remote-tracking branch 'apache/main' into issue-8144-func-rename
alamb 7bc6c8f
rename `with_json` to `append_json`
alamb eb94139
Merge branch 'main' into issue-8144-func-rename
liamzwbao 2f00e87
Rename tests
liamzwbao aa0cd18
Merge branch 'main' into issue-8144-func-rename
liamzwbao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,17 +21,14 @@ use arrow_schema::ArrowError; | |
| use parquet_variant::{ListBuilder, ObjectBuilder, Variant, VariantBuilderExt}; | ||
| use serde_json::{Number, Value}; | ||
|
|
||
| /// Converts a JSON string to Variant to a [`VariantBuilderExt`], such as | ||
| /// Converts a JSON string to Variant using a [`VariantBuilderExt`], such as | ||
| /// [`VariantBuilder`]. | ||
| /// | ||
| /// The resulting `value` and `metadata` buffers can be | ||
| /// extracted using `builder.finish()` | ||
| /// | ||
| /// # Arguments | ||
| /// * `json` - The JSON string to parse as Variant. | ||
| /// * `variant_builder` - Object of type `VariantBuilder` used to build the variant from the JSON | ||
| /// string | ||
| /// | ||
| /// | ||
| /// # Returns | ||
| /// | ||
|
|
@@ -42,43 +39,43 @@ use serde_json::{Number, Value}; | |
| /// | ||
| /// ```rust | ||
| /// # use parquet_variant::VariantBuilder; | ||
| /// # use parquet_variant_json::{ | ||
| /// # json_to_variant, variant_to_json_string, variant_to_json, variant_to_json_value | ||
| /// # }; | ||
| /// # use parquet_variant_json::{JsonToVariant, VariantToJson}; | ||
| /// | ||
| /// let mut variant_builder = VariantBuilder::new(); | ||
| /// let person_string = "{\"name\":\"Alice\", \"age\":30, ".to_string() | ||
| /// + "\"email\":\"alice@example.com\", \"is_active\": true, \"score\": 95.7," | ||
| /// + "\"additional_info\": null}"; | ||
| /// json_to_variant(&person_string, &mut variant_builder)?; | ||
| /// variant_builder.append_json(&person_string)?; | ||
| /// | ||
| /// let (metadata, value) = variant_builder.finish(); | ||
| /// | ||
| /// let variant = parquet_variant::Variant::try_new(&metadata, &value)?; | ||
| /// | ||
| /// let json_result = variant_to_json_string(&variant)?; | ||
| /// let json_value = variant_to_json_value(&variant)?; | ||
| /// let json_result = variant.to_json_string()?; | ||
| /// let json_value = variant.to_json_value()?; | ||
| /// | ||
| /// let mut buffer = Vec::new(); | ||
| /// variant_to_json(&mut buffer, &variant)?; | ||
| /// variant.to_json(&mut buffer)?; | ||
| /// let buffer_result = String::from_utf8(buffer)?; | ||
| /// assert_eq!(json_result, "{\"additional_info\":null,\"age\":30,".to_string() + | ||
| /// "\"email\":\"alice@example.com\",\"is_active\":true,\"name\":\"Alice\",\"score\":95.7}"); | ||
| /// assert_eq!(json_result, buffer_result); | ||
| /// assert_eq!(json_result, serde_json::to_string(&json_value)?); | ||
| /// # Ok::<(), Box<dyn std::error::Error>>(()) | ||
| /// ``` | ||
| pub fn json_to_variant(json: &str, builder: &mut impl VariantBuilderExt) -> Result<(), ArrowError> { | ||
| let json: Value = serde_json::from_str(json) | ||
| .map_err(|e| ArrowError::InvalidArgumentError(format!("JSON format error: {e}")))?; | ||
|
|
||
| build_json(&json, builder)?; | ||
| Ok(()) | ||
| pub trait JsonToVariant { | ||
| /// Create a Variant from a JSON string | ||
| fn append_json(&mut self, json: &str) -> Result<(), ArrowError>; | ||
| } | ||
|
|
||
| fn build_json(json: &Value, builder: &mut impl VariantBuilderExt) -> Result<(), ArrowError> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed redundant |
||
| append_json(json, builder)?; | ||
| Ok(()) | ||
| impl<T: VariantBuilderExt> JsonToVariant for T { | ||
| fn append_json(&mut self, json: &str) -> Result<(), ArrowError> { | ||
| let json: Value = serde_json::from_str(json) | ||
| .map_err(|e| ArrowError::InvalidArgumentError(format!("JSON format error: {e}")))?; | ||
|
|
||
| append_json(&json, self)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn variant_from_number<'m, 'v>(n: &Number) -> Result<Variant<'m, 'v>, ArrowError> { | ||
|
|
@@ -157,7 +154,7 @@ impl VariantBuilderExt for ObjectFieldBuilder<'_, '_, '_> { | |
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
| use crate::variant_to_json_string; | ||
| use crate::VariantToJson; | ||
| use arrow_schema::ArrowError; | ||
| use parquet_variant::{ | ||
| ShortString, Variant, VariantBuilder, VariantDecimal16, VariantDecimal4, VariantDecimal8, | ||
|
|
@@ -171,7 +168,7 @@ mod test { | |
| impl JsonToVariantTest<'_> { | ||
| fn run(self) -> Result<(), ArrowError> { | ||
| let mut variant_builder = VariantBuilder::new(); | ||
| json_to_variant(self.json, &mut variant_builder)?; | ||
| variant_builder.append_json(self.json)?; | ||
| let (metadata, value) = variant_builder.finish(); | ||
| let variant = Variant::try_new(&metadata, &value)?; | ||
| assert_eq!(variant, self.expected); | ||
|
|
@@ -622,10 +619,10 @@ mod test { | |
| ); | ||
| // Manually verify raw JSON value size | ||
| let mut variant_builder = VariantBuilder::new(); | ||
| json_to_variant(&json, &mut variant_builder)?; | ||
| variant_builder.append_json(&json)?; | ||
| let (metadata, value) = variant_builder.finish(); | ||
| let v = Variant::try_new(&metadata, &value)?; | ||
| let output_string = variant_to_json_string(&v)?; | ||
| let output_string = v.to_json_string()?; | ||
| assert_eq!(output_string, json); | ||
| // Verify metadata size = 1 + 2 + 2 * 497 + 3 * 496 | ||
| assert_eq!(metadata.len(), 2485); | ||
|
|
@@ -663,10 +660,10 @@ mod test { | |
| fn test_json_to_variant_unicode() -> Result<(), ArrowError> { | ||
| let json = "{\"爱\":\"अ\",\"a\":1}"; | ||
| let mut variant_builder = VariantBuilder::new(); | ||
| json_to_variant(json, &mut variant_builder)?; | ||
| variant_builder.append_json(json)?; | ||
| let (metadata, value) = variant_builder.finish(); | ||
| let v = Variant::try_new(&metadata, &value)?; | ||
| let output_string = variant_to_json_string(&v)?; | ||
| let output_string = v.to_json_string()?; | ||
| assert_eq!(output_string, "{\"a\":1,\"爱\":\"अ\"}"); | ||
| let mut variant_builder = VariantBuilder::new(); | ||
| let mut object_builder = variant_builder.new_object(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quite clever