Skip to content

Commit

Permalink
add JsonNode function for java and scala with test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-gmahadevan committed Aug 27, 2024
1 parent 0a83ef9 commit 6036ed3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ public String asJsonString() {
* @since 1.14.0
*/
public JsonNode asJsonNode() {
verify(VariantTypes.Object);
return objectToJsonNode(value);
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/com/snowflake/snowpark/types/Variant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private[snowpark] object Variant {

object Object extends VariantType

object Jsonnode extends VariantType

// internal used when converting from Java
def getType(name: String): VariantType = name match {
case "RealNumber" => RealNumber
Expand Down Expand Up @@ -381,6 +383,15 @@ class Variant private[snowpark] (
}
}

/**
* Converts the variant as valid JsonNode
*
* @since 1.14.0
*/
def asJsonNode(): JsonNode = {
objectToJsonNode(value)
}

/**
* Converts the variant as binary value
* @since 0.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void javaVariantConversion() {
assert (result.asMap().get("a").get().asString().equals("[1,2]"));
assert (result.asMap().get("a").get().asJsonString().equals("[1,2]"));

// Scala variant to Java Variant
// Scala variant to Java Variant
com.snowflake.snowpark.types.Variant sv =
new com.snowflake.snowpark.types.Variant("{\"a\": [1, 2], \"b\": \"c\"}");
assert InternalUtils.createVariant(null) == null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.snowflake.snowpark_test

import com.snowflake.snowpark.types.{Geography, Variant}
import com.snowflake.snowpark_java.types.InternalUtils
import org.scalatest.FunSuite

import java.io.UncheckedIOException
Expand Down Expand Up @@ -311,4 +312,11 @@ class ScalaVariantSuite extends FunSuite {

assert(v1.toString() == "123")
}

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"))
}
}

0 comments on commit 6036ed3

Please sign in to comment.