Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/8cc7faeb395974125b70748f2d1da85f to your computer and use it in GitHub Desktop.
Save dacr/8cc7faeb395974125b70748f2d1da85f to your computer and use it in GitHub Desktop.
genson java json API cookbook as unit test cases. / published by https://github.com/dacr/code-examples-manager #398573c4-cdea-4db3-86f2-308f60d68cef/e7eb9b8e0610a87ce7ee1825a522799766a1266c
// summary : genson java json API cookbook as unit test cases.
// keywords : scala, scalatest, genson, json, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 398573c4-cdea-4db3-86f2-308f60d68cef
// created-on : 2019-09-27T15:07:21Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
//> using dep "org.scalatest::scalatest:3.2.10"
//> using dep "com.owlike:genson:1.6"
//// using lib "com.owlike::genson-scala:1.6" // not available yet for scala 2.13
// ---------------------
import org.scalatest.*, flatspec.*, matchers.*
import com.owlike.genson.*
import java.text.SimpleDateFormat
import scala.jdk.CollectionConverters._
case class Person(age:Int, name:String)
object JsonGensonCookBook extends AnyFlatSpec with should.Matchers {
override def suiteName = "JsonGensonCookBook"
"genson" should "deserialize json array strings as java/scala array" in {
val genson = new Genson()
val decoded = genson.deserialize("""[1,2,3]""", classOf[Array[Int]])
decoded shouldBe Array(1,2,3)
}
it should "deserialize string as string" in {
val genson = new Genson()
val decoded = genson.deserialize(""" "truc" """, classOf[String])
decoded shouldBe "truc"
}
it should "deserialize json array strings as java list" in {
val genson = new Genson()
val decoded = genson.deserialize("""[1,2,3]""", classOf[java.util.ArrayList[Int]])
val expected = new java.util.ArrayList[Int]()
expected.addAll(List(1,2,3).asJava)
decoded.asScala shouldBe expected.asScala
}
it should "be possible to add meta class information in serialized json" in {
val genson =
new GensonBuilder()
.useClassMetadata(true)
.setSkipNull(true)
.useRuntimeType(true)
.useConstructorWithArguments(true) // Take care, with true you may encounter IllegalArgumentException within asm.ClassReader
.create()
val point = new java.awt.Point(42, 24)
val encoded = genson.serialize(point)
encoded shouldBe """{"x":42, "y":24}"""
}
// This is a java API, friendly suitable for scala...
it should "deserialize scala case classes works fine !" in {
val dateFormatPattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"
val builder =
new GensonBuilder()
.useDateFormat(new SimpleDateFormat(dateFormatPattern))
.setSkipNull(true)
.useIndentation(true)
.useConstructorWithArguments(true) // Take care, with true you may encounter IllegalArgumentException within asm.ClassReader
.useRuntimeType(true)
.useDateAsTimestamp(true)
//.withBundle(ScalaBundle().useOnlyConstructorFields(false)) // requires the genson-scala dependency but not avail yet for scala 2.13
val genson = builder.create()
val decoded = genson.deserialize("""{"age":42, "name":"John"}""", classOf[Person])
decoded shouldBe Person(42, "John")
}
}
JsonGensonCookBook.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment