Kotlin/kotlinx.serialization là dự án Mobile trên GitHub, viết chủ yếu bằng Kotlin, với 6.0k sao. Kotlin multiplatform / multi-format serialization
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Kotlin serialization consists of a compiler plugin, that generates visitor code for serializable classes,
runtime library with core serialization API and support libraries with various serialization formats.
Supports Kotlin classes marked as @Serializable and standard collections.
Provides JSON, Protobuf, CBOR, Hocon and Properties formats.
Complete multiplatform support: JVM, JS and Native.
Table of contents
Introduction and references
Setup
Gradle
1) Setting up the serialization plugin
2) Dependency on the JSON library
Android
Multiplatform (Common, JS, Native)
Maven
Bazel
Additional links
Kotlin serialization documentation
Full API reference
Submitting issues and PRs
Building this library
Introduction and references
Here is a small example.
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable
data class Project(val name: String, val language: String)
fun main() {
// Serializing objects
val data = Project("kotlinx.serialization", "Kotlin")
val string = Json.encodeToString(data)
println(string) // {"name":"kotlinx.serialization","language":"Kotlin"}
// Deserializing back into objects
val obj = Json.decodeFromString<Project>(string)
println(obj) // Project(name=kotlinx.serialization, language=Kotlin)
}
You can get the full code here.
For detailed guides and examples, see the Kotlin serialization documentation.
You can find auto-generated documentation website on kotlinlang.org.
Setup
New versions of the serialization plugin are released in tandem with each new Kotlin compiler version.
Make sure you have the corresponding Kotlin plugin installed in the IDE, no additional plugins for IDE are required.
Gradle
To set up kotlinx.serialization, you have to do two things:
Add the serialization plugin.
Add the serialization library dependency.
1) Setting up the serialization plugin
You can set up the serialization plugin with the Kotlin plugin using the
Gradle plugins DSL:
Kotlin DSL:
plugins {
kotlin("jvm") version "2.4.10" // or kotlin("multiplatform") or any other kotlin plugin
kotlin("plugin.serialization") version "2.4.10"
}
Groovy DSL:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '2.4.10'
id 'org.jetbrains.kotlin.plugin.serialization' version '2.4.10'
}
Kotlin versions before 1.4.0 are not supported by the stable release of Kotlin serialization.
Using apply plugin (the old way)
First, you have to add the serialization plugin to your classpath as the other compiler plugins:
Kotlin DSL:
buildscript {
repositories { mavenCentral() }
dependencies {
val kotlinVersion = "2.4.10"
classpath(kotlin("gradle-plugin", version = kotlinVersion))
classpath(kotlin("serialization", version = kotlinVersion))
}
}
apply plugin: 'kotlin' // or 'kotlin-multiplatform' for multiplatform projects
apply plugin: 'kotlinx-serialization'
2) Dependency on the JSON library
After setting up the plugin, you have to add a dependency on the serialization library.
Note that while the plugin has version the same as the compiler one, runtime library has different coordinates, repository and versioning.
We also provide kotlinx-serialization-core artifact that contains all serialization API but does not have a bundled serialization format with it
Android
By default, proguard rules are supplied with the library.
These rules keep serializers for all serializable classes that are retained after shrinking,
so you don't need additional setup.
However, these rules do not affect serializable classes if they have named companion objects.
If you want to serialize classes with named companion objects, you need to add and edit rules below to your proguard-rules.pro configuration.
Note that the rules for R8 differ depending on the compatibility mode used.
Example of named companion rules for ProGuard and R8 compatibility mode
# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
# If you have any, replace classes with those containing named companion objects.
-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
-if @kotlinx.serialization.Serializable class
com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
com.example.myapplication.HasNamedCompanion2
{
static **$* *;
}
-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
static <1>$$serializer INSTANCE;
}
Example of named companion rules for R8 full mode
# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
# If you have any, replace classes with those containing named companion objects.
-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
-if @kotlinx.serialization.Serializable class
com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
com.example.myapplication.HasNamedCompanion2
{
static **$* *;
}
-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
static <1>$$serializer INSTANCE;
}
# Keep both serializer and serializable classes to save the attribute InnerClasses
-keepclasseswithmembers, allowshrinking, allowobfuscation, allowaccessmodification class
com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
com.example.myapplication.HasNamedCompanion2
{
*;
}
In case you want to exclude serializable classes that are used, but never serialized at runtime,
you will need to write custom rules with narrower class specifications.
Multiplatform (Common, JS, Native)
Most of the modules are also available for Kotlin/JS and Kotlin/Native.
You can add dependency to the required module right to the common source set:
commonMain {
dependencies {
// Works as common dependency as well as the platform one
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
}
}
The same artifact coordinates can be used to depend on platform-specific artifact in platform-specific source-set.
Maven
Ensure the proper version of Kotlin and serialization version:
Kotlin/kotlinx.serialization thuộc nhóm Mobile trên TopGit, cùng 5 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về Kotlin/kotlinx.serialization ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Kotlin/kotlinx.serialization là nguồn chính thức.
Kotlin/kotlinx.serialization có bao nhiêu sao?
Kotlin/kotlinx.serialization có 6.0k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/Kotlin/kotlinx.serialization. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
Kotlin/kotlinx.serialization có những chủ đề gì?
GitHub topics của Kotlin/kotlinx.serialization: "cbor", "json", "kotlin", "protobuf", "serialization". TopGit xếp repo vào nhóm Mobile.
Kotlin/kotlinx.serialization còn đang phát triển không?
Commit gần nhất trên Kotlin/kotlinx.serialization là 4 ngày trước (theo timestamp GitHub). Repo có 691 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Kotlin/kotlinx.serialization viết bằng ngôn ngữ gì?
Kotlin/kotlinx.serialization chủ yếu viết bằng Kotlin. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao Kotlin/kotlinx.serialization được xếp vào nhóm Mobile?
TopGit xếp Kotlin/kotlinx.serialization vào nhóm Mobile dựa trên GitHub topics và mô tả của repo (gắn thẻ: "cbor", "json", "kotlin"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về kotlinx.serialization?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về kotlinx.serialization.