Skip to content

Commit

Permalink
update asJsonNode impl for scala and java
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-gmahadevan committed Aug 28, 2024
1 parent a569e41 commit a946cee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
9 changes: 4 additions & 5 deletions src/main/java/com/snowflake/snowpark_java/types/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,13 @@ public String asJsonString() {
}

/**
* Converts the variant as valid JsonNode. This function allows to read the JSON object directly
* rather parsing it as String and then extract the desired value
* Return the variant value as a JsonNode. This function allows to read the JSON object directly
* as JsonNode from variant column rather parsing it as String
*
* <pre>{@code - to get the first value from array for key "a"
*
* Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
* JsonNode jNode = jv.asJsonNode();
* System.out.println(jNode.get("a").get(0));
* System.out.println(jv.asJsonNode().get("a").get(0));
*
* output
* 1
Expand All @@ -394,7 +393,7 @@ public String asJsonString() {
* @since 1.14.0
*/
public JsonNode asJsonNode() {
return objectToJsonNode(value);
return value;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/com/snowflake/snowpark/types/Variant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,20 @@ class Variant private[snowpark] (
}

/**
* Converts the variant as valid JsonNode
* This function allows to read the JSON object directly rather parsing it as String
* and then extract the desired value
* Return the variant value as a JsonNode. This function allows to read the JSON object directly
* as JsonNode from variant column rather parsing it as String
* Example - to get the first value from array for key "a"
* {{{
* val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}")
* println(sv.asMap.get("a").get.asJsonNode().get(0))
* println(sv.asJsonNode().get("a").get(0))
* output
* 1
* }}}
*
* @since 1.14.0
*/
def asJsonNode(): JsonNode = {
objectToJsonNode(value)
value
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ public void equalsAndToString() {
@Test
public void javaJsonNodeVariantConverter() throws IllegalArgumentException {
Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
JsonNode jNode = jv.asJsonNode();
assert (jNode.get("a").isArray());
assert (jNode.get("b").asText().equals("c"));
assert (jv.asJsonNode().get("a").isArray());
assert (jv.asJsonNode().get("b").asText().equals("c"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ class ScalaVariantSuite extends FunSuite {

test("JsonNode") {
val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}")
assert(sv.asMap.get("a").get.asJsonNode().isArray)
assert(sv.asMap.get("b").get.asJsonNode().asInt().equals(3))
assert(sv.asMap.get("c").get.asJsonNode().asText().equals("xyz"))
assert(sv.asJsonNode().get("a").isArray)
assert(sv.asJsonNode().get("b").asInt().equals(3))
assert(sv.asJsonNode().get("c").asText().equals("xyz"))
}
}

0 comments on commit a946cee

Please sign in to comment.