Skip to content

Commit 87c06d2

Browse files
authored
Merge pull request #30 from cloudoptlab/3.x
3.0.0.0-BETA6
2 parents 0d44301 + 4e50bf2 commit 87c06d2

53 files changed

Lines changed: 914 additions & 353 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cloudopt-next-json/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
dependencies {
2-
implementation project(":cloudopt-next-utils")
3-
implementation project(":cloudopt-next-logging")
2+
api "io.vertx:vertx-core:${rootProject.property('vertx_version')}"
3+
api "com.fasterxml.jackson.core:jackson-core:${rootProject.property('jackson_version')}"
4+
api "com.fasterxml.jackson.core:jackson-databind:${rootProject.property('jackson_version')}"
5+
api "com.fasterxml.jackson.core:jackson-annotations:${rootProject.property('jackson_version')}"
6+
api "com.fasterxml.jackson.module:jackson-module-kotlin:${rootProject.property('jackson_version')}"
7+
api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${rootProject.property('jackson_version')}"
48
}
59

610
description = "Cloudopt Next Json"

cloudopt-next-json/src/main/kotlin/net/cloudopt/next/json/DefaultJSONProvider.kt

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,62 @@
1515
*/
1616
package net.cloudopt.next.json
1717

18-
import com.alibaba.fastjson.JSON
19-
import com.alibaba.fastjson.JSONObject
20-
import com.alibaba.fastjson.parser.ParserConfig
21-
import net.cloudopt.next.utils.Resourcer
18+
import com.fasterxml.jackson.databind.MapperFeature
19+
import io.vertx.core.json.Json
20+
import io.vertx.core.json.JsonArray
21+
import io.vertx.core.json.JsonObject
2222
import kotlin.reflect.KClass
23+
import com.fasterxml.jackson.databind.ObjectMapper
24+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
25+
import io.vertx.core.json.jackson.DatabindCodec
26+
2327

24-
/*
25-
* @author: Cloudopt
26-
* @Time: 2018/1/9
27-
* @Description: Default JsonProvider.
28-
*/
2928
class DefaultJSONProvider : JsonProvider {
3029

3130
init {
32-
val cfg = ParserConfig.getGlobalInstance()
33-
cfg.addAccept("net.cloudopt.next.")
31+
DatabindCodec.mapper().registerModule(JavaTimeModule())
3432
}
3533

3634
override fun toJsonString(obj: Any): String {
37-
return JSON.toJSONString(obj)
35+
return Json.encode(obj)
3836
}
3937

4038
override fun toJsonMap(jsonString: String): MutableMap<String, Any> {
41-
return JSON.parseObject(jsonString).toMutableMap()
39+
return (Json.decodeValue(jsonString) as JsonObject).map.toMutableMap()
4240
}
4341

4442
override fun toObject(jsonString: String, clazz: KClass<*>): Any {
45-
return JSON.parseObject(jsonString, clazz.java)
43+
return Json.decodeValue(jsonString, clazz.java)
4644
}
4745

48-
override fun toJsonMapList(s: String): MutableList<MutableMap<String, Any>> {
49-
return JSON.parseArray(s).toMutableList() as MutableList<MutableMap<String, Any>>
46+
override fun toJsonMapList(jsonString: String): MutableList<MutableMap<String, Any>> {
47+
val value = Json.decodeValue(jsonString) as JsonArray
48+
val mapList = mutableListOf<MutableMap<String, Any>>()
49+
for (it in value.withIndex()) {
50+
mapList.add(value.getJsonObject(it.index).map)
51+
}
52+
return mapList
5053
}
5154

52-
override fun toObjectList(jsonString: String, clazz: KClass<*>): MutableList<Any> {
53-
return JSON.parseArray(jsonString, clazz.java).toMutableList()
55+
override fun <T> toObjectList(jsonString: String, clazz: KClass<*>): MutableList<T> {
56+
val value = Json.decodeValue(jsonString) as JsonArray
57+
val list = mutableListOf<T>()
58+
for (it in value.withIndex()) {
59+
list.add(value.getJsonObject(it.index).mapTo(clazz.java) as T)
60+
}
61+
return list
5462
}
5563

5664
override fun toList(jsonString: String): MutableList<Any> {
57-
return JSON.parseArray(jsonString).toMutableList()
65+
return (Json.decodeValue(jsonString) as JsonArray).toMutableList()
5866
}
5967

60-
override fun read(filePath: String): MutableMap<String, Any> {
61-
var jsonString = Resourcer.inputStreamToString(Resourcer.getFileInputStream(filePath))
62-
jsonString = cleanText(jsonString)
63-
return toJsonMap(jsonString)
68+
override fun toJsonObject(jsonString: String): JsonObject {
69+
return Json.decodeValue(jsonString) as JsonObject
6470
}
6571

66-
override fun read(filePath: String, prefix: String): MutableMap<String, Any> {
67-
var jsonString = Resourcer.inputStreamToString(Resourcer.getFileInputStream(filePath))
68-
jsonString = cleanText(jsonString)
69-
var jsonObj = JSON.parseObject(jsonString)
70-
var list = prefix.split(".")
71-
for (key in list) {
72-
if (jsonObj.getJSONObject(key) != null) {
73-
jsonObj = jsonObj.getJSONObject(key)
74-
}
75-
}
76-
return jsonObj.toMutableMap()
72+
override fun toJsonArray(jsonString: String): JsonArray {
73+
return Json.decodeValue(jsonString) as JsonArray
7774
}
7875

79-
override fun read(filePath: String, prefix: String, clazz: KClass<*>): Any {
80-
var jsonObj = read(filePath, prefix)
81-
return JSONObject(jsonObj).toJavaObject(clazz.java)!!
82-
}
83-
84-
private fun cleanText(jsonString: String): String {
85-
return jsonString.replace("/n", "")
86-
}
87-
88-
8976
}

cloudopt-next-json/src/main/kotlin/net/cloudopt/next/json/JsonProvider.kt

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
*/
1616
package net.cloudopt.next.json
1717

18+
import io.vertx.core.json.JsonArray
19+
import io.vertx.core.json.JsonObject
1820
import kotlin.reflect.KClass
1921

20-
/*
21-
* @author: Cloudopt
22-
* @Time: 2018/1/9
23-
* @Description: JsonProvider Interface.
24-
*/
2522
interface JsonProvider {
2623

2724
/**
@@ -59,7 +56,7 @@ interface JsonProvider {
5956
* @param clazz Java class
6057
* @return MutableList<Any>
6158
*/
62-
fun toObjectList(jsonString: String, clazz: KClass<*>): MutableList<Any>
59+
fun<T> toObjectList(jsonString: String, clazz: KClass<*>): MutableList<T>
6360

6461
/**
6562
* Output any list.
@@ -68,30 +65,20 @@ interface JsonProvider {
6865
*/
6966
fun toList(jsonString: String): MutableList<Any>
7067

71-
7268
/**
73-
* Read Json file to json string
74-
* @param filePath File path
75-
* @return MutableMap<String,Any>
69+
* Decode a given JSON string to JSON Object.
70+
* @see JsonObject
71+
* @param jsonString String
72+
* @return JsonObject
7673
*/
77-
fun read(filePath: String): MutableMap<String, Any>
78-
79-
/**
80-
* Read Json file to Map
81-
* @param filePath File path
82-
* @param prefix Attribute prefix in json file
83-
* @return map
84-
*/
85-
fun read(filePath: String, prefix: String): MutableMap<String, Any>
86-
74+
fun toJsonObject(jsonString: String):JsonObject
8775

8876
/**
89-
* Read Json file into a specific instance of the object
90-
* @param filePath File path
91-
* @param prefix Attribute prefix in json file
92-
* @param clazz Class name
93-
* @return Object
77+
* Decode a given JSON string to JSON Array.
78+
* @see JsonArray
79+
* @param jsonString String
80+
* @return JsonArray
9481
*/
95-
fun read(filePath: String, prefix: String, clazz: KClass<*>): Any
82+
fun toJsonArray(jsonString: String):JsonArray
9683

9784
}

cloudopt-next-json/src/main/kotlin/net/cloudopt/next/json/Jsoner.kt

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,108 +15,81 @@
1515
*/
1616
package net.cloudopt.next.json
1717

18-
import net.cloudopt.next.utils.Classer
18+
import io.vertx.core.json.JsonArray
19+
import io.vertx.core.json.JsonObject
1920
import kotlin.reflect.KClass
20-
import kotlin.reflect.full.createInstance
21-
22-
23-
/*
24-
* @author: Cloudopt
25-
* @Time: 2018/1/9
26-
* @Description: Json's tool class.
27-
*/
2821

2922
object Jsoner {
3023

3124
@JvmStatic
32-
var jsonProvider: JsonProvider = Classer.loadClass("net.cloudopt.next.json.DefaultJSONProvider")
33-
.createInstance() as JsonProvider
25+
var jsonProvider: JsonProvider = DefaultJSONProvider()
3426

3527
/**
3628
* Output json string.
37-
* @param obj Json Object
3829
* @return Json string
3930
*/
40-
fun toJsonString(obj: Any): String {
41-
return jsonProvider.toJsonString(obj)
31+
fun Any.toJsonString(): String {
32+
return jsonProvider.toJsonString(this)
4233
}
4334

4435
/**
4536
* Output MutableMap
46-
* @param jsonString Json string
4737
* @return MutableMap
4838
*/
49-
fun toJsonMap(jsonString: String): MutableMap<String, Any> {
50-
return jsonProvider.toJsonMap(jsonString)
39+
fun String.jsontoMutableMap(): MutableMap<String, Any> {
40+
return jsonProvider.toJsonMap(this)
5141
}
5242

5343
/**
5444
* Output json object.
55-
* @param jsonString Json string
5645
* @param clazz Java class
5746
* @return Json object
5847
*/
59-
fun toObject(jsonString: String, clazz: KClass<*>): Any {
60-
return jsonProvider.toObject(jsonString, clazz)
48+
fun String.jsonToObject(clazz: KClass<*>): Any {
49+
return jsonProvider.toObject(this, clazz)
6150
}
6251

6352
/**
6453
* Output json map list.
65-
* @param jsonString Json string
6654
* @return MutableList<MutableMap<String,Any>>
6755
*/
68-
fun toJsonMapList(jsonString: String): MutableList<MutableMap<String, Any>> {
69-
return jsonProvider.toJsonMapList(jsonString)
56+
fun String.jsontoMutableMapList(): MutableList<MutableMap<String, Any>> {
57+
return jsonProvider.toJsonMapList(this)
7058
}
7159

7260
/**
7361
* Output obj array.
74-
* @param jsonString Json string
7562
* @param clazz Java class
7663
* @return MutableList<Any>
7764
*/
78-
fun toObjectList(jsonString: String, clazz: KClass<*>): MutableList<Any> {
79-
return jsonProvider.toObjectList(jsonString, clazz)
65+
fun String.jsonToObjectList(clazz: KClass<*>): MutableList<Any> {
66+
return jsonProvider.toObjectList(this, clazz)
8067
}
8168

8269
/**
8370
* Output any list.
84-
* @param jsonString Json string
8571
* @return MutableList<Any>
8672
*/
87-
fun toList(jsonString: String): MutableList<Any> {
88-
return jsonProvider.toList(jsonString)
73+
fun String.jsontoObjectList(): MutableList<Any> {
74+
return jsonProvider.toList(this)
8975
}
9076

9177
/**
92-
* Read Json file to json string
93-
* @param filePath File path
94-
* @return MutableMap<String,Any>
78+
* Decode a given JSON string to JSON Object.
79+
* @see JsonObject
80+
* @return JsonObject
9581
*/
96-
fun read(filePath: String): MutableMap<String, Any> {
97-
return jsonProvider.read(filePath)
82+
fun String.toJsonObject(): JsonObject {
83+
return jsonProvider.toJsonObject(this)
9884
}
9985

10086
/**
101-
* Read Json file to Map
102-
* @param filePath File path
103-
* @param prefix Attribute prefix in json file
104-
* @return map
87+
* Decode a given JSON string to JSON Array.
88+
* @see JsonArray
89+
* @return JsonArray
10590
*/
106-
fun read(filePath: String, prefix: String): MutableMap<String, Any> {
107-
return jsonProvider.read(filePath, prefix)
91+
fun String.toJsonArray(): JsonArray {
92+
return jsonProvider.toJsonArray(this)
10893
}
10994

110-
/**
111-
* Read Json file into a specific instance of the object
112-
* @param filePath File path
113-
* @param prefix Attribute prefix in json file
114-
* @param clazz Class name
115-
* @return Object
116-
*/
117-
fun read(filePath: String, prefix: String, clazz: KClass<*>): Any {
118-
return jsonProvider.read(filePath, prefix, clazz)
119-
}
120-
121-
12295
}

cloudopt-next-json/src/test/kotlin/net/cloudopt/next/json/Student.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)