Sample code for BAEL-1399 (#4165)

* Squashed commit of the following:

commit b31955df9638a6217a804e61faa230d8eacb293b
Author: Juan Moreno <earth001@gmail.com>
Date:   Wed Feb 21 22:45:52 2018 -0300

    Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com

* Squashed commit of the following:

commit 4e6e27c914a401ee6bc599c7ffe913384137646a
Author: Juan Moreno <earth001@gmail.com>
Date:   Wed Feb 21 23:26:20 2018 -0300

    Fix package names

commit b31955df9638a6217a804e61faa230d8eacb293b
Author: Juan Moreno <earth001@gmail.com>
Date:   Wed Feb 21 22:45:52 2018 -0300

    Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com

* Added sample with null fields

* Removed unused dependency

* BAEL-1159 - Simplified example

* Code sample to BAEL-1399

* Code sample to BAEL-1399

* Sample code for BAEL-1399

* Sample code for BAEL-1399

* Sample code for BAEL-1399

* Upgrade to Klaxon 3.0.2, polish test

* Added sample with annotations

* Upgrade dependencies

* Ugrade library version, added JSON Path sample

* Reverted changes
This commit is contained in:
Juan Moreno 2018-05-26 13:19:25 -03:00 committed by KevinGilmore
parent b6b060d0f5
commit 4308468c60
5 changed files with 195 additions and 10 deletions

View File

@ -98,13 +98,18 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>klaxon</artifactId>
<version>${klaxon.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin-maven-plugin.version}</version>
<executions>
<execution>
@ -200,20 +205,22 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin-maven-plugin.version>1.2.31</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.2.31</kotlin-test-junit.version>
<kotlin-stdlib.version>1.2.31</kotlin-stdlib.version>
<kotlin-reflect.version>1.2.31</kotlin-reflect.version>
<kotlinx.version>0.15</kotlinx.version>
<kotlin-maven-plugin.version>1.2.41</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.2.41</kotlin-test-junit.version>
<kotlin-stdlib.version>1.2.41</kotlin-stdlib.version>
<kotlin-reflect.version>1.2.41</kotlin-reflect.version>
<kotlinx.version>0.22.5</kotlinx.version>
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
<kodein.version>4.1.0</kodein.version>
<klaxon.version>3.0.4</klaxon.version>
<maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
<khttp.version>0.1.0</khttp.version>
<commons-math3.version>3.6.1</commons-math3.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.vintage.version>4.12.0</junit.vintage.version>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.2.0</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
<junit4.version>4.12</junit4.version>
<assertj.version>3.9.1</assertj.version>
<assertj.version>3.10.0</assertj.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

View File

@ -0,0 +1,9 @@
package com.baeldung.klaxon
import com.beust.klaxon.Json
class CustomProduct(
@Json(name = "productName")
val name: String,
@Json(ignored = true)
val id: Int)

View File

@ -0,0 +1,3 @@
package com.baeldung.klaxon
class Product(val name: String)

View File

@ -0,0 +1,3 @@
package com.baeldung.klaxon
data class ProductData(val name: String, val capacityInGb: Int)

View File

@ -0,0 +1,163 @@
package com.baeldung.klaxon
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.JsonReader
import com.beust.klaxon.Klaxon
import com.beust.klaxon.Parser
import com.beust.klaxon.PathMatcher
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions.assertSoftly
import org.junit.Assert
import org.junit.jupiter.api.Test
import java.io.StringReader
import java.util.regex.Pattern
class KlaxonUnitTest {
@Test
fun giveProduct_whenSerialize_thenGetJsonString() {
val product = Product("HDD")
val result = Klaxon().toJsonString(product)
assertThat(result).isEqualTo("""{"name" : "HDD"}""")
}
@Test
fun giveJsonString_whenDeserialize_thenGetProduct() {
val result = Klaxon().parse<Product>("""
{
"name" : "RAM"
}
""")
assertThat(result?.name).isEqualTo("RAM")
}
@Test
fun giveCustomProduct_whenSerialize_thenGetJsonString() {
val product = CustomProduct("HDD", 1)
val result = Klaxon().toJsonString(product)
assertThat(result).isEqualTo("""{"productName" : "HDD"}""")
}
@Test
fun giveJsonArray_whenStreaming_thenGetProductArray() {
val jsonArray = """
[
{ "name" : "HDD", "capacityInGb" : 512 },
{ "name" : "RAM", "capacityInGb" : 16 }
]"""
val expectedArray = arrayListOf(ProductData("HDD", 512),
ProductData("RAM", 16))
val klaxon = Klaxon()
val productArray = arrayListOf<ProductData>()
JsonReader(StringReader(jsonArray)).use { reader ->
reader.beginArray {
while (reader.hasNext()) {
val product = klaxon.parse<ProductData>(reader)
productArray.add(product!!)
}
}
}
assertThat(productArray).hasSize(2).isEqualTo(expectedArray)
}
@Test
fun giveJsonString_whenParser_thenGetJsonObject() {
val jsonString = StringBuilder("""
{
"name" : "HDD",
"capacityInGb" : 512,
"sizeInInch" : 2.5
}
""")
val parser = Parser()
val json = parser.parse(jsonString) as JsonObject
assertThat(json).hasSize(3).containsEntry("name", "HDD").containsEntry("capacityInGb", 512).containsEntry("sizeInInch", 2.5)
}
@Suppress("UNCHECKED_CAST")
@Test
fun giveJsonStringArray_whenParser_thenGetJsonArray() {
val jsonString = StringBuilder("""
[
{ "name" : "SDD" },
{ "madeIn" : "Taiwan" },
{ "warrantyInYears" : 5 }
]""")
val parser = Parser()
val json = parser.parse(jsonString) as JsonArray<JsonObject>
assertSoftly({ softly ->
softly.assertThat(json).hasSize(3)
softly.assertThat(json[0]["name"]).isEqualTo("SDD")
softly.assertThat(json[1]["madeIn"]).isEqualTo("Taiwan")
softly.assertThat(json[2]["warrantyInYears"]).isEqualTo(5)
})
}
@Test
fun givenJsonString_whenStreaming_thenProcess() {
val jsonString = """
{
"name" : "HDD",
"madeIn" : "Taiwan",
"warrantyInYears" : 5
"hasStock" : true
"capacitiesInTb" : [ 1, 2 ],
"features" : { "cacheInMb" : 64, "speedInRpm" : 7200 }
}"""
JsonReader(StringReader(jsonString)).use { reader ->
reader.beginObject {
while (reader.hasNext()) {
val readName = reader.nextName()
when (readName) {
"name" -> assertThat(reader.nextString()).isEqualTo("HDD")
"madeIn" -> assertThat(reader.nextString()).isEqualTo("Taiwan")
"warrantyInYears" -> assertThat(reader.nextInt()).isEqualTo(5)
"hasStock" -> assertThat(reader.nextBoolean()).isEqualTo(true)
"capacitiesInTb" -> assertThat(reader.nextArray()).contains(1, 2)
"features" -> assertThat(reader.nextObject()).containsEntry("cacheInMb", 64).containsEntry("speedInRpm", 7200)
else -> Assert.fail("Unexpected name: $readName")
}
}
}
}
}
@Test
fun givenDiskInventory_whenRegexMatches_thenGetTypes() {
val jsonString = """
{
"inventory" : {
"disks" : [
{
"type" : "HDD",
"sizeInGb" : 1000
},
{
"type" : "SDD",
"sizeInGb" : 512
}
]
}
}"""
val pathMatcher = object : PathMatcher {
override fun pathMatches(path: String) = Pattern.matches(".*inventory.*disks.*type.*", path)
override fun onMatch(path: String, value: Any) {
when (path) {
"$.inventory.disks[0].type" -> assertThat(value).isEqualTo("HDD")
"$.inventory.disks[1].type" -> assertThat(value).isEqualTo("SDD")
}
}
}
Klaxon().pathMatcher(pathMatcher).parseJsonObject(StringReader(jsonString))
}
}