If your project uses kotlin, you can use the Fastjson-Kotlin module, and use the characteristics of kotlin.
Maven:
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-kotlin</artifactId>
<version>2.0.61</version>
</dependency>Add standard library(kotlin-stdlib) and reflection library(kotlin-reflect) as appropriate. If the data class is used or the parameters are passed in through constructor, then add reflection library.
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin-version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin-version}</version>
</dependency>Kotlin Gradle:
dependencies {
implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.61")
}dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version")
}Whenever the function in the Fastjson-Kotlin, you need to complete the import functions work, otherwise it will be prompted that the corresponding function will not be found.
E.g:
import com.alibaba.fastjson2.to
import com.alibaba.fastjson2.intoIf you use a lot of functions, you can use batch import.
import com.alibaba.fastjson2.*We have unified function names to and into.
- Use
toto use::class.java, suitable for categories without generic Class. - Use
intoto use theTypeReference, which is suitable for genetic Class.
First define a User class
class User(
var id: Int,
var name: String
)val text = "..." // String
val data = text.parseObject()
val bytes = ... // ByteArray
val data = bytes.parseObject() // JSONObjectKotlin:
val text = "..." // String
val data = text.parseArray() // JSONArrayval refer = reference<User>()No generic:
val text = "..." // String
val data = text.to<User>() // UserIncluding generic:
val text = "..." // String
val data = text.into<List<User>>() // List<User>
val data = text.into<Map<String, User>>() // Map<String, User>Serialization as a string:
val data = "..." // Any
val text = data.toJSONString() // StringSerialization as a ByteArray:
val data = "..." // Any
val bytes = data.toJSONByteArray() // ByteArrayval text = "..."
val data = JSON.parseObject(text) // JSONObject
val id = data.getIntValue("id") // Int
val name = data.getString("name") // StringNo generic:
val obj = ... // JSONObject
val array = ... // JSONArray
val user = array.to<User>(0)
val user = obj.to<User>("key")Including generic:
val obj = ... // JSONObject
val array = ... // JSONArray
val user = array.into<List<User>>(0)
val user = obj.into<List<User>>("key")No generic:
val obj = ... // JSONObject
val array = ... // JSONArray
val user = obj.to<User>() // User
val users = array.toList<User>() // List<User>Including generic:
val obj = ... // JSONObject
val array = ... // JSONArray
val user = obj.into<HashMap<String, User>>() // HashMap<String, User>
val users = array.into<ArrayList<User>>() // ArrayList<User>No generic:
val url = ... // URL
val data = url.to<User>()val input = ... // InputStream
val data = input.to<User>()Including generic:
val url = ... // URL
val data = url.into<List<User>>()val input = ... // InputStream
val data = input.into<List<User>>()val text = "..."
val path = "$.id".toPath() // JSONPath
val parser = JSONReader.of(text)
val result = path.extract(parser)