This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val list = listOf(Student(), Student() ... ) | |
val gson = Gson() | |
val jsonString = gson.toJson(list) | |
val sType = object : TypeToken<List<Student>>() { }.type | |
val otherList = gson.fromJson<List<Student>>(jsonString, sType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val list = listOf("String1", "String2" ... ) | |
val gson = Gson() | |
val jsonString = gson.toJson(list) | |
val sType = object : TypeToken<List<String>>() { }.type | |
val otherList = gson.fromJson<List<String>>(jsonString, sType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val gSon = GsonBuilder().registerTypeAdapter(Student::class.java, StudentSerializer()).create() | |
val json = gSon.toJson(student) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StudentSerializer : JsonSerializer<Student> { | |
override fun serialize(src: Student?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { | |
val jsonObject = JsonObject() | |
try { | |
jsonObject.add("name", context?.serialize(src.name)) | |
jsonObject.add("address", JsonParser().parse(src.address)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val gSon = GsonBuilder().registerTypeAdapter(Student::class.java, StudentDeserializer()).create() | |
val student = gSon.fromJson<Student>(json2, Student::class.java) | |
/** Output student will be like below **/ | |
student.name // Mark | |
student.address // { "city": "London", "post": "12000" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Student( | |
var name: String? = null, | |
var address: String? = null) { | |
} | |
class StudentDeserializer : JsonDeserializer<Student> { | |
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Student { | |
json as JsonObject | |
val name = json.get("name").asString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val json2 = """ | |
{ | |
"name": "Mark", | |
"address": { | |
"city": "London", | |
"post": "12000" | |
} | |
} """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Address( | |
var city: String? = null, | |
var post: String? = null) { | |
} | |
data class Student( | |
var name: String? = null, | |
var address: Address? = null) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val json = """ | |
{ | |
"name": "Alex", | |
"address": { | |
"city": "Rome", | |
"post": "1000" | |
} | |
} """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Student( | |
@SerializedName("key_name") | |
var name: String? = null, | |
@SerializedName("key_address") | |
var address: String? = null) { | |
} | |
val student = Student("Mark", "London, 12000") // object | |
val json = Gson().toJson(student) // json string |
NewerOlder