So here are the answers to the
last post:
Exercise 1: Json to Pojo and Back Again
import com.google.gson.JsonObject
import com.google.gson.JsonParser
fun main() {
val johnJson = """{
| "username": "john123",
| "firstName": "John",
| "lastName": "Smith",
| "email": "john@smith.com"
|}""".trimMargin()
val janeJson = """{
| "username": "janedoe5",
| "firstName": "Jane",
| "lastName": "Doe",
| "email": "jane.doe@gmail.com"
|}""".trimMargin()
val parser = JsonParser()
val johnJsonObject = parser.parse(johnJson).asJsonObject
val janeJsonObject = parser.parse(janeJson).asJsonObject
val john1 = User.fromJson(johnJsonObject)
val john2 = User.fromJson(johnJsonObject)
val jane = User.fromJson(janeJsonObject)
println(john1)
println(john2)
println(jane)
println("john1 = john2: " + (john1 == john2))
println("john1 = jane: " + (john1 == jane))
val usersSet = hashSetOf(john1, john2, jane)
println("HashSet size (expected 2): " + usersSet.size)
val johnFinal = john1.toJson()
val janeFinal = jane.toJson()
println(johnFinal)
println(janeFinal)
}
data class User(val username: String,
val firstName: String,
val lastName: String,
val email: String) {
companion object {
fun fromJson(jsonObject: JsonObject) : User =
User(jsonObject.get("username").asString,
jsonObject.get("firstName").asString,
jsonObject.get("lastName").asString,
jsonObject.get("email").asString)
}
fun toJson() : JsonObject {
val jsonObject = JsonObject()
jsonObject.addProperty("username", username)
jsonObject.addProperty("firstName", firstName)
jsonObject.addProperty("lastName", lastName)
jsonObject.addProperty("email", email)
return jsonObject
}
}
Or, alternatively, if you prefer to pull the json processing out into extension functions:
import com.google.gson.JsonObject
import com.google.gson.JsonParser
fun main() {
val johnJson = """{
| "username": "john123",
| "firstName": "John",
| "lastName": "Smith",
| "email": "john@smith.com"
|}""".trimMargin()
val janeJson = """{
| "username": "janedoe5",
| "firstName": "Jane",
| "lastName": "Doe",
| "email": "jane.doe@gmail.com"
|}""".trimMargin()
val parser = JsonParser()
val johnJsonObject = parser.parse(johnJson).asJsonObject
val janeJsonObject = parser.parse(janeJson).asJsonObject
val john1 = User.fromJson(johnJsonObject)
val john2 = User.fromJson(johnJsonObject)
val jane = User.fromJson(janeJsonObject)
println(john1)
println(john2)
println(jane)
println("john1 = john2: " + (john1 == john2))
println("john1 = jane: " + (john1 == jane))
val usersSet = hashSetOf(john1, john2, jane)
println("HashSet size (expected 2): " + usersSet.size)
val johnFinal = john1.toJson()
val janeFinal = jane.toJson()
println(johnFinal)
println(janeFinal)
}
data class User(val username: String,
val firstName: String,
val lastName: String,
val email: String) {
companion object
}
fun User.Companion.fromJson(jsonObject: JsonObject) : User =
User(jsonObject.get("username").asString,
jsonObject.get("firstName").asString,
jsonObject.get("lastName").asString,
jsonObject.get("email").asString)
fun User.toJson() : JsonObject {
val jsonObject = JsonObject()
jsonObject.addProperty("username", username)
jsonObject.addProperty("firstName", firstName)
jsonObject.addProperty("lastName", lastName)
jsonObject.addProperty("email", email)
return jsonObject
}