Skip to content

Commit

Permalink
[spring boot] Jackson 工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
SunYufei committed Dec 11, 2023
1 parent f83f282 commit 8b06c4b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions spring-boot-demo/common/src/main/java/ml/sun/common/ext/JsonExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import ml.sun.common.util.SpringUtil
import kotlin.reflect.KClass

object JsonExt {
private val mapper = SpringUtil.getBean(ObjectMapper::class)
Expand All @@ -26,5 +27,31 @@ object JsonExt {

fun Any.toJson(): String = if (this is String) this else mapper.writeValueAsString(this)

fun String.toObj(): JsonNode = mapper.readTree(this)

fun <T : Any> String.toObj(toValueType: KClass<T>): T = mapper.readValue(this, toValueType.java)

fun <T : Any> JsonNode.asObj(toValueType: KClass<T>): T = mapper.convertValue(this, toValueType.java)

fun <E : Any> String.toList(elementType: KClass<E>): List<E> =
mapper.readerForListOf(elementType.java).readValue(this)

fun <E : Any> JsonNode.asList(elementType: KClass<E>): List<E> =
mapper.readerForListOf(elementType.java).readValue(this)

fun <E : Any> JsonNode.getList(fieldName: String, elementType: KClass<E>): List<E> =
this.get(fieldName).asList(elementType)

fun <V : Any> String.toMap(valueType: KClass<V>): Map<String, V> =
mapper.readerForMapOf(valueType.java).readValue(this)

fun <V : Any> JsonNode.asMap(valueType: KClass<V>): Map<String, V> =
mapper.readerForMapOf(valueType.java).readValue(this)

fun <V : Any> JsonNode.getMap(fieldName: String, valueType: KClass<V>): Map<String, V> =
this.get(fieldName).asMap(valueType)

fun JsonNode.getText(fieldName: String): String = this.get(fieldName).asText()

fun JsonNode.getBoolean(fieldName: String): Boolean = this.get(fieldName).asBoolean()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ml.sun.common.ext

import com.fasterxml.jackson.databind.ObjectMapper
import ml.sun.common.ext.JsonExt.asList
import ml.sun.common.ext.JsonExt.asMap
import ml.sun.common.ext.JsonExt.asObj
import ml.sun.common.ext.JsonExt.getBoolean
import ml.sun.common.ext.JsonExt.toList
import ml.sun.common.ext.JsonExt.toMap
import ml.sun.common.ext.JsonExt.toObj
import ml.sun.common.util.SpringUtil
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig

@SpringJUnitConfig(SpringUtil::class, ObjectMapper::class)
class JsonExtTest {
@Test
fun obj() {
data class Test(val success: Boolean, val code: Int, val message: String)

val json = """{ "success": true, "code": 0, "message": "" }"""
val obj = json.toObj(Test::class)

assertEquals(obj.success, true)
assertEquals(obj.code, 0)
assertEquals(obj.message, "")

val node = json.toObj()
assertTrue(node.getBoolean("success"))

assertEquals(obj, node.asObj(Test::class))
}

@Test
fun list() {
val strList = """["first", "second", "third", "fourth"]""".toList(String::class)

assertEquals(strList[0], "first")
assertEquals(strList.size, 4)

val intList = "[0, 1, 2, 3]".toList(Int::class)

assertEquals(intList[0], 0)
assertEquals(intList.size, 4)

val boolList = """[true, false]""".toObj().asList(Boolean::class)
assertTrue(boolList[0])
}

@Test
fun map() {
val json = """{ "key": "value", "success": true }"""
val map = json.toMap(Any::class)

assertEquals(map["key"], "value")
assertEquals(map["success"], true)
assertEquals(json.toObj().asMap(Any::class), map)
}
}

0 comments on commit 8b06c4b

Please sign in to comment.