Last active
May 25, 2024 10:19
-
-
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/eff871c0407e6bc69433ec8e44490b6f56b7b597
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.4.2" | |
//> 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