style: 全项目统一 Tab 缩进并补全尾随逗号
This commit is contained in:
@@ -38,211 +38,211 @@ import java.lang.reflect.Type
|
||||
|
||||
class Jackson3JsonApi : JsonApi {
|
||||
|
||||
var get: () -> ObjectMapper = { _prObjectMapper!! }
|
||||
var set: (ObjectMapper) -> Unit = { _prObjectMapper = it }
|
||||
var get: () -> ObjectMapper = { _prObjectMapper!! }
|
||||
var set: (ObjectMapper) -> Unit = { _prObjectMapper = it }
|
||||
|
||||
private var _prObjectMapper: ObjectMapper? = null
|
||||
private var _prObjectMapper: ObjectMapper? = null
|
||||
|
||||
|
||||
var objectMapper: ObjectMapper
|
||||
get() = get()
|
||||
private set(value) = set(value)
|
||||
var objectMapper: ObjectMapper
|
||||
get() = get()
|
||||
private set(value) = set(value)
|
||||
|
||||
constructor(objectMapper: ObjectMapper) {
|
||||
_prObjectMapper = objectMapper
|
||||
}
|
||||
constructor(objectMapper: ObjectMapper) {
|
||||
_prObjectMapper = objectMapper
|
||||
}
|
||||
|
||||
constructor(
|
||||
get: () -> ObjectMapper,
|
||||
set: (ObjectMapper) -> Unit,
|
||||
) {
|
||||
this.get = get
|
||||
this.set = set
|
||||
}
|
||||
constructor(
|
||||
get: () -> ObjectMapper,
|
||||
set: (ObjectMapper) -> Unit,
|
||||
) {
|
||||
this.get = get
|
||||
this.set = set
|
||||
}
|
||||
|
||||
constructor() {
|
||||
_prObjectMapper = ObjectMapper()
|
||||
}
|
||||
constructor() {
|
||||
_prObjectMapper = ObjectMapper()
|
||||
}
|
||||
|
||||
override fun <T> parse(json: String, clazz: Class<T>): T {
|
||||
try {
|
||||
return objectMapper.readValue(json, clazz)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to parse JSON to ${clazz.simpleName}", e)
|
||||
}
|
||||
}
|
||||
override fun <T> parse(json: String, clazz: Class<T>): T {
|
||||
try {
|
||||
return objectMapper.readValue(json, clazz)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to parse JSON to ${clazz.simpleName}", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T> parse(json: String, type: JsonTypeReference<T>): T {
|
||||
try {
|
||||
return objectMapper.readValue(json, object : TypeReference<T>() {
|
||||
override fun getType(): Type = type.type
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to parse JSON to type: $type", e)
|
||||
}
|
||||
}
|
||||
override fun <T> parse(json: String, type: JsonTypeReference<T>): T {
|
||||
try {
|
||||
return objectMapper.readValue(json, object : TypeReference<T>() {
|
||||
override fun getType(): Type = type.type
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to parse JSON to type: $type", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun format(obj: Any): String {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(obj)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to format object to JSON: ${obj.javaClass.simpleName}", e)
|
||||
}
|
||||
}
|
||||
override fun format(obj: Any): String {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(obj)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to format object to JSON: ${obj.javaClass.simpleName}", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun formatPretty(obj: Any): String {
|
||||
try {
|
||||
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to format object to pretty JSON: ${obj.javaClass.simpleName}", e)
|
||||
}
|
||||
}
|
||||
override fun formatPretty(obj: Any): String {
|
||||
try {
|
||||
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to format object to pretty JSON: ${obj.javaClass.simpleName}", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun isValidJson(json: String): Boolean {
|
||||
return try {
|
||||
objectMapper.readTree(json)
|
||||
true
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
override fun isValidJson(json: String): Boolean {
|
||||
return try {
|
||||
objectMapper.readTree(json)
|
||||
true
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
override fun merge(vararg jsons: String): String {
|
||||
try {
|
||||
if (jsons.isEmpty()) return "{}"
|
||||
override fun merge(vararg jsons: String): String {
|
||||
try {
|
||||
if (jsons.isEmpty()) return "{}"
|
||||
|
||||
val resultNode = objectMapper.createObjectNode()
|
||||
val resultNode = objectMapper.createObjectNode()
|
||||
|
||||
for (json in jsons) {
|
||||
val node = objectMapper.readTree(json)
|
||||
if (node is ObjectNode) {
|
||||
resultNode.setAll(node)
|
||||
} else {
|
||||
throw JsonException("Cannot merge non-object JSON: $json")
|
||||
}
|
||||
}
|
||||
for (json in jsons) {
|
||||
val node = objectMapper.readTree(json)
|
||||
if (node is ObjectNode) {
|
||||
resultNode.setAll(node)
|
||||
} else {
|
||||
throw JsonException("Cannot merge non-object JSON: $json")
|
||||
}
|
||||
}
|
||||
|
||||
return objectMapper.writeValueAsString(resultNode)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to merge JSON objects", e)
|
||||
}
|
||||
}
|
||||
return objectMapper.writeValueAsString(resultNode)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to merge JSON objects", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getNodeValue(json: String, path: String): String? {
|
||||
try {
|
||||
val rootNode = objectMapper.readTree(json)
|
||||
val node = rootNode.at(path)
|
||||
override fun getNodeValue(json: String, path: String): String? {
|
||||
try {
|
||||
val rootNode = objectMapper.readTree(json)
|
||||
val node = rootNode.at(path)
|
||||
|
||||
return if (node.isMissingNode || node.isNull) {
|
||||
null
|
||||
} else {
|
||||
node.asString()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to get node value at path: $path", e)
|
||||
}
|
||||
}
|
||||
return if (node.isMissingNode || node.isNull) {
|
||||
null
|
||||
} else {
|
||||
node.asString()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to get node value at path: $path", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateNodeValue(json: String, path: String, newValue: Any): String {
|
||||
try {
|
||||
val rootNode = objectMapper.readTree(json)
|
||||
override fun updateNodeValue(json: String, path: String, newValue: Any): String {
|
||||
try {
|
||||
val rootNode = objectMapper.readTree(json)
|
||||
|
||||
// 处理 JSON Pointer 路径
|
||||
val pathParts = path.split("/").filter { it.isNotEmpty() }
|
||||
// 处理 JSON Pointer 路径
|
||||
val pathParts = path.split("/").filter { it.isNotEmpty() }
|
||||
|
||||
if (pathParts.isEmpty()) {
|
||||
// 替换整个根节点
|
||||
return objectMapper.writeValueAsString(newValue)
|
||||
}
|
||||
if (pathParts.isEmpty()) {
|
||||
// 替换整个根节点
|
||||
return objectMapper.writeValueAsString(newValue)
|
||||
}
|
||||
|
||||
when (val parentNode = findParentNode(rootNode, pathParts)) {
|
||||
is ObjectNode -> {
|
||||
val fieldName = pathParts.last()
|
||||
val valueNode = objectMapper.valueToTree<JsonNode>(newValue)
|
||||
parentNode.replace(fieldName, valueNode)
|
||||
}
|
||||
when (val parentNode = findParentNode(rootNode, pathParts)) {
|
||||
is ObjectNode -> {
|
||||
val fieldName = pathParts.last()
|
||||
val valueNode = objectMapper.valueToTree<JsonNode>(newValue)
|
||||
parentNode.replace(fieldName, valueNode)
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw JsonException("Cannot update node: parent is not an object node at path: $path")
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
throw JsonException("Cannot update node: parent is not an object node at path: $path")
|
||||
}
|
||||
}
|
||||
|
||||
return objectMapper.writeValueAsString(rootNode)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to update node value at path: $path", e)
|
||||
}
|
||||
}
|
||||
return objectMapper.writeValueAsString(rootNode)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to update node value at path: $path", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun <D> convert(source: Any, destinationClass: Class<D>): D {
|
||||
try {
|
||||
return objectMapper.convertValue(source, destinationClass)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException(
|
||||
"Failed to convert from ${source.javaClass.simpleName} to ${destinationClass.simpleName}", e
|
||||
)
|
||||
}
|
||||
}
|
||||
override fun <D> convert(source: Any, destinationClass: Class<D>): D {
|
||||
try {
|
||||
return objectMapper.convertValue(source, destinationClass)
|
||||
} catch (e: Exception) {
|
||||
throw JsonException(
|
||||
"Failed to convert from ${source.javaClass.simpleName} to ${destinationClass.simpleName}", e
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun <D> convert(
|
||||
source: Any,
|
||||
destinationType: JsonTypeReference<D>,
|
||||
): D {
|
||||
try {
|
||||
return objectMapper.convertValue(source, object : TypeReference<D>() {
|
||||
override fun getType(): Type = destinationType.type
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to convert from ${source.javaClass.simpleName} to type: $destinationType", e)
|
||||
}
|
||||
}
|
||||
override fun <D> convert(
|
||||
source: Any,
|
||||
destinationType: JsonTypeReference<D>,
|
||||
): D {
|
||||
try {
|
||||
return objectMapper.convertValue(source, object : TypeReference<D>() {
|
||||
override fun getType(): Type = destinationType.type
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to convert from ${source.javaClass.simpleName} to type: $destinationType", e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 辅助方法:查找父节点
|
||||
private fun findParentNode(root: JsonNode, pathParts: List<String>): JsonNode {
|
||||
var current: JsonNode = root
|
||||
// 辅助方法:查找父节点
|
||||
private fun findParentNode(root: JsonNode, pathParts: List<String>): JsonNode {
|
||||
var current: JsonNode = root
|
||||
|
||||
// 遍历到最后一个元素之前(即找到父节点)
|
||||
for (i in 0 until pathParts.size - 1) {
|
||||
val part = pathParts[i]
|
||||
current = when {
|
||||
part.matches(Regex("\\d+")) -> current.get(part.toInt())
|
||||
else -> current.get(part)
|
||||
} ?: throw JsonException("Path not found: ${pathParts.subList(0, i + 1).joinToString("/")}")
|
||||
}
|
||||
// 遍历到最后一个元素之前(即找到父节点)
|
||||
for (i in 0 until pathParts.size - 1) {
|
||||
val part = pathParts[i]
|
||||
current = when {
|
||||
part.matches(Regex("\\d+")) -> current.get(part.toInt())
|
||||
else -> current.get(part)
|
||||
} ?: throw JsonException("Path not found: ${pathParts.subList(0, i + 1).joinToString("/")}")
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
override fun addJsonConverter(c: BaseJsonConverter<*, *>) {
|
||||
val mapperBuilder = Jackson3ApiJavaHelper.rebuild(objectMapper)
|
||||
mapperBuilder.addModule(c.getJackson3Module())
|
||||
this.objectMapper = mapperBuilder.build()
|
||||
}
|
||||
override fun addJsonConverter(c: BaseJsonConverter<*, *>) {
|
||||
val mapperBuilder = Jackson3ApiJavaHelper.rebuild(objectMapper)
|
||||
mapperBuilder.addModule(c.getJackson3Module())
|
||||
this.objectMapper = mapperBuilder.build()
|
||||
}
|
||||
|
||||
override fun parseJsonItem(json: String): JsonItem {
|
||||
try {
|
||||
return getJsonItem(objectMapper.readTree(json))
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to parseJsonItem", e)
|
||||
}
|
||||
}
|
||||
override fun parseJsonItem(json: String): JsonItem {
|
||||
try {
|
||||
return getJsonItem(objectMapper.readTree(json))
|
||||
} catch (e: Exception) {
|
||||
throw JsonException("Failed to parseJsonItem", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class JackSon3JsonItem : JsonItem {
|
||||
companion object {
|
||||
fun getJsonItem(node: JsonNode): JsonItem {
|
||||
return when (node.nodeType) {
|
||||
JsonNodeType.OBJECT -> JackSon3JsonObject(node)
|
||||
JsonNodeType.ARRAY -> JackSon3JsonArray(node)
|
||||
JsonNodeType.BOOLEAN -> JackSon3JsonBoolean(node)
|
||||
JsonNodeType.NULL -> JsonNull()
|
||||
JsonNodeType.NUMBER -> JackSon3JsonNumber(node)
|
||||
JsonNodeType.STRING -> JackSon3JsonString(node)
|
||||
else -> throw IllegalStateException("unknown type ${node.nodeType}")
|
||||
}
|
||||
}
|
||||
}
|
||||
companion object {
|
||||
fun getJsonItem(node: JsonNode): JsonItem {
|
||||
return when (node.nodeType) {
|
||||
JsonNodeType.OBJECT -> JackSon3JsonObject(node)
|
||||
JsonNodeType.ARRAY -> JackSon3JsonArray(node)
|
||||
JsonNodeType.BOOLEAN -> JackSon3JsonBoolean(node)
|
||||
JsonNodeType.NULL -> JsonNull()
|
||||
JsonNodeType.NUMBER -> JackSon3JsonNumber(node)
|
||||
JsonNodeType.STRING -> JackSon3JsonString(node)
|
||||
else -> throw IllegalStateException("unknown type ${node.nodeType}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JackSon3JsonNumber(node: JsonNode) : JsonNumber(node.asString())
|
||||
@@ -251,47 +251,47 @@ class JackSon3JsonString(node: JsonNode) : JsonString(node.asString())
|
||||
|
||||
|
||||
class JackSon3JsonArray(val node: JsonNode) : JsonArray() {
|
||||
override fun getIndex(index: Int): JsonItem {
|
||||
return JackSon3JsonItem.getJsonItem(node.get(index))
|
||||
}
|
||||
override fun getIndex(index: Int): JsonItem {
|
||||
return JackSon3JsonItem.getJsonItem(node.get(index))
|
||||
}
|
||||
|
||||
override fun iteratorArray(): Iterator<IndexedValue<JsonItem>> {
|
||||
val iteet = node.iterator().withIndex()
|
||||
return object : Iterator<IndexedValue<JsonItem>> {
|
||||
override fun next(): IndexedValue<JsonItem> {
|
||||
val it = iteet.next()
|
||||
return IndexedValue(it.index, JackSon3JsonItem.getJsonItem(it.value))
|
||||
}
|
||||
override fun iteratorArray(): Iterator<IndexedValue<JsonItem>> {
|
||||
val iteet = node.iterator().withIndex()
|
||||
return object : Iterator<IndexedValue<JsonItem>> {
|
||||
override fun next(): IndexedValue<JsonItem> {
|
||||
val it = iteet.next()
|
||||
return IndexedValue(it.index, JackSon3JsonItem.getJsonItem(it.value))
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iteet.hasNext()
|
||||
}
|
||||
override fun hasNext(): Boolean {
|
||||
return iteet.hasNext()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JackSon3JsonObject(val node: JsonNode) : JsonObject() {
|
||||
override fun getField(name: String): JsonItem {
|
||||
return JackSon3JsonItem.getJsonItem(node.get(name))
|
||||
}
|
||||
override fun getField(name: String): JsonItem {
|
||||
return JackSon3JsonItem.getJsonItem(node.get(name))
|
||||
}
|
||||
|
||||
override fun iteratorObject(): Iterator<Map.Entry<String, JsonItem>> {
|
||||
return object : Iterator<Map.Entry<String, JsonItem>> {
|
||||
val it = node.properties().iterator()
|
||||
override fun next(): Map.Entry<String, JsonItem> {
|
||||
val utem = it.next()
|
||||
return object : Map.Entry<String, JsonItem> {
|
||||
override val key: String
|
||||
get() = utem.key
|
||||
override val value: JsonItem
|
||||
get() = JackSon3JsonItem.getJsonItem(utem.value)
|
||||
}
|
||||
}
|
||||
override fun iteratorObject(): Iterator<Map.Entry<String, JsonItem>> {
|
||||
return object : Iterator<Map.Entry<String, JsonItem>> {
|
||||
val it = node.properties().iterator()
|
||||
override fun next(): Map.Entry<String, JsonItem> {
|
||||
val utem = it.next()
|
||||
return object : Map.Entry<String, JsonItem> {
|
||||
override val key: String
|
||||
get() = utem.key
|
||||
override val value: JsonItem
|
||||
get() = JackSon3JsonItem.getJsonItem(utem.value)
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return it.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun hasNext(): Boolean {
|
||||
return it.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user