Skip to content

Commit

Permalink
add JsonNode function for variant type
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-gmahadevan committed Aug 22, 2024
1 parent 172caca commit 6b11060
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/types/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ public String asJsonString() {
}
}

/**
* Converts the variant as valid JsonNode.
*
* @return A valid json Node
* @since 0.8.0
*/
public JsonNode asJsonNode() {
verify(VariantTypes.String);
return objectToJsonNode(value);
}

/**
* Converts the variant as binary value.
*
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.snowflake.snowpark_test;

import com.fasterxml.jackson.databind.JsonNode;
import com.snowflake.snowpark_java.types.Geography;
import com.snowflake.snowpark_java.types.InternalUtils;
import com.snowflake.snowpark_java.types.Variant;
Expand Down Expand Up @@ -365,4 +366,17 @@ public void equalsAndToString() {

assert v1.toString().equals("123");
}

@Test
public void javaJsonNodeVariantConverter() throws IllegalArgumentException {
// Scala variant to Java Variant
com.snowflake.snowpark.types.Variant sv =
new com.snowflake.snowpark.types.Variant(
"{\"name\": \"abc\",\"grade\": 2, \"Interest\": \"cricket\"}");
Variant jResult = InternalUtils.createVariant(sv);
JsonNode jsonNode = jResult.asJsonNode();

assert jsonNode.get("name").asText().equals("abc");
assert jsonNode.get("grade").asInt() == 2;
}
}

0 comments on commit 6b11060

Please sign in to comment.