Skip to content

Latest commit

 

History

History
199 lines (162 loc) · 16.5 KB

README.md

File metadata and controls

199 lines (162 loc) · 16.5 KB

github-header-image (2)

𝙺𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗🧞‍

Topics
𝚆𝚑𝚊𝚝 𝚒𝚜 𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚊𝚗𝚍 𝙴𝚗𝚌𝚘𝚍𝚒𝚗𝚐
𝚆𝚑𝚊𝚝 𝚒𝚜 𝙳𝚎-𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚊𝚗𝚍 𝙳𝚎𝚌𝚘𝚍𝚒𝚗𝚐
𝙺𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚜𝚞𝚙𝚙𝚘𝚛𝚝𝚜 𝚖𝚊𝚗𝚢 𝚝𝚢𝚙𝚎𝚜
𝙺𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚌𝚘𝚖𝚙𝚊𝚛𝚎𝚍 𝚠𝚒𝚝𝚑 𝙼𝚘𝚜𝚑𝚒 𝚊𝚗𝚍 𝙶𝚜𝚘𝚗
𝙲𝚘𝚗𝚟𝚎𝚛𝚝𝚒𝚗𝚐 𝚊 𝙼𝚘𝚍𝚎𝚕 𝚌𝚕𝚊𝚜𝚜 𝚒𝚗𝚝𝚘 𝚊 𝙹𝚂𝙾𝙽 𝚜𝚝𝚛𝚒𝚗𝚐
𝚃𝚛𝚊𝚗𝚜𝚏𝚘𝚛𝚖𝚒𝚗𝚐 𝚌𝚘𝚕𝚕𝚎𝚌𝚝𝚒𝚘𝚗 𝚝𝚘 𝙹𝚜𝚘𝚗 𝚜𝚝𝚛𝚒𝚗𝚐 𝚞𝚜𝚒𝚗𝚐 𝚔𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚋𝚕𝚎
𝚄𝚜𝚒𝚗𝚐 @𝚃𝚛𝚊𝚗𝚜𝚒𝚎𝚗𝚝 𝚊𝚗𝚗𝚘𝚝𝚊𝚝𝚒𝚘𝚗 𝚒𝚗 𝚖𝚘𝚍𝚎𝚕
𝙿𝚛𝚘𝚟𝚒𝚍𝚒𝚗𝚐 𝚝𝚑𝚎 𝚠𝚊𝚛𝚗𝚒𝚗𝚐 𝚏𝚘𝚛 𝚊 𝚙𝚛𝚘𝚙𝚎𝚛𝚝𝚢 𝚘𝚏 𝚖𝚘𝚍𝚎𝚕 𝚝𝚑𝚊𝚝 𝚒𝚜 𝚗𝚘𝚝 𝚜𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚎𝚍
𝙰𝚍𝚍𝚒𝚗𝚐 𝚊 𝚍𝚎𝚏𝚊𝚞𝚕𝚝 𝚟𝚊𝚕𝚞𝚎 𝚏𝚘𝚛 𝚊 𝚖𝚘𝚍𝚎𝚕 𝚙𝚛𝚘𝚙𝚎𝚛𝚝𝚢 𝚒𝚏 𝚝𝚑𝚎 𝚙𝚛𝚘𝚙𝚎𝚛𝚝𝚢 𝚒𝚜 𝚗𝚘𝚝 𝚙𝚛𝚎𝚜𝚎𝚗𝚝 𝚒𝚗 𝚝𝚑𝚎 𝙰𝙿𝙸 𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎
𝚄𝚜𝚎 𝚝𝚑𝚎 𝚊𝚙𝚙𝚕𝚒𝚌𝚊𝚝𝚒𝚘𝚗 𝚒𝚗 𝚝𝚑𝚎 𝚜𝚘𝚞𝚛𝚌𝚎 𝚌𝚘𝚍𝚎 𝚝𝚘 𝚜𝚎𝚎 𝚑𝚘𝚠 𝚒𝚝'𝚜 𝚒𝚗𝚝𝚎𝚐𝚛𝚊𝚝𝚎𝚍 𝚠𝚒𝚝𝚑 𝚛𝚎𝚝𝚛𝚘𝚏𝚒𝚝
Output

𝚆𝚑𝚊𝚝 𝚒𝚜 𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚊𝚗𝚍 𝙴𝚗𝚌𝚘𝚍𝚒𝚗𝚐

  • An Object in a computer memory consists of binary data that a computer can directly use it.
  • Serialization is the process of reducing the object into its primitive structure like integer,array,etc in a manner that preserves that structure.
  • Encoding is a process of converting a serialized object into a output format by following a specific set of rules.
  • Class Object -----serialization----> Machine readable binary structure -----encoding----> Json/Xml structure
  • Class { int x, int y } ----> {11011101,10011111} ----> {"x":12312,"y":12233}
  • After the serialization, We view it as a human redable format.

𝚆𝚑𝚊𝚝 𝚒𝚜 𝙳𝚎-𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚊𝚗𝚍 𝙳𝚎𝚌𝚘𝚍𝚒𝚗𝚐

  • The input data which is in human redable form which is json/xml is deserialized into primitives and then decoded into objects.
  • Json/Xml structure -----deserialization----> Primitives -----decoding----> Class Object.

𝙺𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚜𝚞𝚙𝚙𝚘𝚛𝚝𝚜 𝚖𝚊𝚗𝚢 𝚝𝚢𝚙𝚎𝚜

  • There are support for primitive data types Boolean,Byte,Short,Int,Long,String,Double,Char
  • Other types include List,enum,set,pair etc

𝙺𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚝𝚒𝚘𝚗 𝚌𝚘𝚖𝚙𝚊𝚛𝚎𝚍 𝚠𝚒𝚝𝚑 𝙼𝚘𝚜𝚑𝚒 𝚊𝚗𝚍 𝙶𝚜𝚘𝚗

Moshi Gson KotlinSerialization
Uses code genaration Uses reflection Uses code generation, compiler plugin is used instead of annotation
Faster runtime performance Slow runtime performance Good runtime performance
Increased build time build time is not increased Fast build time
  • Moshi has more advanced features and provides a lower-level JSON API for maximum control
  • Kotlin Serialization is also used in KMP(Kotlin Multi Platform)
  • Kotlin Serialization is also compatible with unsigned integer types, value classes and sealed classes out-of-the-box.
  • Kotlin Serialization is slightly better than Moshi and much better than GSON, But the due to teh extra kotlin powered language syntax sugar, this library is a good choice to use in development.

𝙲𝚘𝚗𝚟𝚎𝚛𝚝𝚒𝚗𝚐 𝚊 𝙼𝚘𝚍𝚎𝚕 𝚌𝚕𝚊𝚜𝚜 𝚒𝚗𝚝𝚘 𝚊 𝙹𝚂𝙾𝙽 𝚜𝚝𝚛𝚒𝚗𝚐

One of the normal usecases we face is converting Model objects into JSON string

  • Model object
@Serializable
data class StudentInfo(
    @SerialName("name")
    val studentName: String,
    @SerialName("age")
    val studentAge: Int,
)
  • Converting the model object to json string
fun main() {
    val studentModel = StudentInfo("Ramesh", 21)
    println(studentModel)

    val json = Json.encodeToString(StudentInfo.serializer(), studentModel)
    println(json)
}
  • Output
StudentInfo(studentName=Ramesh, studentAge=21) //Printing the student model
{"name":"Ramesh","age":21} //Printing the json string

𝚄𝚜𝚒𝚗𝚐 @𝚃𝚛𝚊𝚗𝚜𝚒𝚎𝚗𝚝 𝚊𝚗𝚗𝚘𝚝𝚊𝚝𝚒𝚘𝚗 𝚒𝚗 𝚖𝚘𝚍𝚎𝚕

  • Marks this property invisible for the whole serialization process, including serial descriptors. Transient properties should have default values.

  • Model object

@Serializable
data class StudentInfo(
    @SerialName("name")
    val studentName: String,
    @SerialName("age")
    val studentAge: Int,
    @Transient
    val isMale: Boolean = false
)
  • Converting the model object to json
fun main() {
    val data = StudentInfo("Ramesh", 21)
    println(data)

    val json = Json.encodeToString(StudentInfo.serializer(), data)
    println(json)
}
  • Output
// Observe the isMale property is not serialized and printed as string
StudentInfo(studentName=Ramesh, studentAge=21) //Printing the student model
{"name":"Ramesh","age":21} //Printing the json string

𝚃𝚛𝚊𝚗𝚜𝚏𝚘𝚛𝚖𝚒𝚗𝚐 𝚌𝚘𝚕𝚕𝚎𝚌𝚝𝚒𝚘𝚗 𝚝𝚘 𝙹𝚜𝚘𝚗 𝚜𝚝𝚛𝚒𝚗𝚐 𝚞𝚜𝚒𝚗𝚐 𝚔𝚘𝚝𝚕𝚒𝚗𝚂𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚊𝚋𝚕𝚎

  • We have used list collection as example, But we can use for many other collections similarly.
@Serializable
data class StudentInfo(
    val studentName: String,
    val studentAge: Int = 21,
)

fun main() {
    val student1 = StudentInfo("Ramesh", 21)
    val student2 = StudentInfo("Suresh", 21)
    val student3 = StudentInfo("Monica", 21)
    val student4 = StudentInfo("Peter", 21)
    val student5 = StudentInfo("Antony", 21)
    val studentList = listOf(student1,student2,student3,student4,student5)

    val studentsSerializer = ListSerializer(StudentInfo.serializer())
    val json = Json.encodeToString(studentsSerializer, studentList)
    println(json)
}
[{"studentName":"Ramesh"},{"studentName":"Suresh"},{"studentName":"Monica"},{"studentName":"Peter"},{"studentName":"Antony"}]

𝙿𝚛𝚘𝚟𝚒𝚍𝚒𝚗𝚐 𝚝𝚑𝚎 𝚠𝚊𝚛𝚗𝚒𝚗𝚐 𝚏𝚘𝚛 𝚊 𝚙𝚛𝚘𝚙𝚎𝚛𝚝𝚢 𝚘𝚏 𝚖𝚘𝚍𝚎𝚕 𝚝𝚑𝚊𝚝 𝚒𝚜 𝚗𝚘𝚝 𝚜𝚎𝚛𝚒𝚊𝚕𝚒𝚣𝚎𝚍

  • Sometimes in a nested model hierarcy, There comes the scenario that one of the property is not initilized.
  • A useful property of Kotlinx is if you have nested data classes and haven’t defined any of them with @Serializable, the IDE will give you an error.
  • Sometimes its good to have this warning, but in other case it forces the class inferred to be annotated as serializable.

𝙰𝚍𝚍𝚒𝚗𝚐 𝚊 𝚍𝚎𝚏𝚊𝚞𝚕𝚝 𝚟𝚊𝚕𝚞𝚎 𝚏𝚘𝚛 𝚊 𝚖𝚘𝚍𝚎𝚕 𝚙𝚛𝚘𝚙𝚎𝚛𝚝𝚢 𝚒𝚏 𝚝𝚑𝚎 𝚙𝚛𝚘𝚙𝚎𝚛𝚝𝚢 𝚒𝚜 𝚗𝚘𝚝 𝚙𝚛𝚎𝚜𝚎𝚗𝚝 𝚒𝚗 𝚝𝚑𝚎 𝙰𝙿𝙸 𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎

  • If you have a field that’s in the data class but not in the API, it simply returns null… OR if you set a default value on it, it will remain that default value.
  • This is one of the defining traits of Kotlinx Serialization. With most of the other json parsers, even if you set a default value like with the above code, if name were not to be found in the API, they would be null. In Kotlinx, it defaults back to the default value.
@Serializable
data class StudentInfo(
    val studentName: String,
    val studentAge: Int = 21,
)

𝚂𝚞𝚙𝚙𝚘𝚛𝚝

If you feel like support me a coffee for my efforts, I would greatly appreciate it.
Buy Me A Coffee

𝙲𝚘𝚗𝚝𝚛𝚒𝚋𝚞𝚝𝚎 🙋‍♂️

Read contribution guidelines for more information regarding contribution.

𝙵𝚎𝚎𝚍𝚋𝚊𝚌𝚔 ✍️

Feature requests are always welcome, File an issue here.

𝙵𝚒𝚗𝚍 𝚝𝚑𝚒𝚜 𝚙𝚛𝚘𝚓𝚎𝚌𝚝 𝚞𝚜𝚎𝚏𝚞𝚕 ? ❤️

Support it by clicking the ⭐ button on the upper right of this page. ✌️

𝙻𝚒𝚌𝚎𝚗𝚜𝚎 Licence 💳

This project is licensed under the Apache License 2.0 - see the LICENSE file for details