You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "liamzwbao (via GitHub)" <gi...@apache.org> on 2025/08/16 17:35:03 UTC

[PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

liamzwbao opened a new pull request, #8161:
URL: https://github.com/apache/arrow-rs/pull/8161

   # Which issue does this PR close?
   
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax.
   
   - Closes #8144.
   
   # Rationale for this change
   
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   
   # What changes are included in this PR?
   
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   
   # Are these changes tested?
   
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   
   # Are there any user-facing changes?
   
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   
   If there are any breaking changes to public APIs, please call them out.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #8161:
URL: https://github.com/apache/arrow-rs/pull/8161


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

Posted by "liamzwbao (via GitHub)" <gi...@apache.org>.
liamzwbao commented on code in PR #8161:
URL: https://github.com/apache/arrow-rs/pull/8161#discussion_r2280703553


##########
parquet-variant-json/src/to_json.rs:
##########
@@ -23,6 +23,304 @@ use parquet_variant::{Variant, VariantList, VariantObject};
 use serde_json::Value;

Review Comment:
   Changes in this file are basically moving the doc to trait and function body to impl



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8161:
URL: https://github.com/apache/arrow-rs/pull/8161#issuecomment-3201025379

   Thank you @liamzwbao and @scovich 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

Posted by "liamzwbao (via GitHub)" <gi...@apache.org>.
liamzwbao commented on code in PR #8161:
URL: https://github.com/apache/arrow-rs/pull/8161#discussion_r2280703264


##########
parquet-variant-json/src/from_json.rs:
##########
@@ -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.with_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 with_json(&mut self, json: &str) -> Result<(), ArrowError>;
 }
 
-fn build_json(json: &Value, builder: &mut impl VariantBuilderExt) -> Result<(), ArrowError> {

Review Comment:
   Removed redundant `build_json`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8161:
URL: https://github.com/apache/arrow-rs/pull/8161#discussion_r2283103947


##########
parquet-variant-json/src/from_json.rs:
##########
@@ -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.with_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 with_json(&mut self, json: &str) -> Result<(), ArrowError>;

Review Comment:
   Can we pleae call this `append_json` to be consistent with `VariantBuilderExt::append_value` ?
   
   Also I think the convention of using `with_*` in these crates is for builder / fluent style APIs that take `self` (rather than `&mut self)`



##########
parquet-variant-json/src/from_json.rs:
##########
@@ -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.with_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 {

Review Comment:
   This is quite clever



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Rename `batch_json_string_to_variant` and `batch_variant_to_json_string` [arrow-rs]

Posted by "liamzwbao (via GitHub)" <gi...@apache.org>.
liamzwbao commented on PR #8161:
URL: https://github.com/apache/arrow-rs/pull/8161#issuecomment-3200849195

   Resolved conflicts, should be good to go now


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org