Gson is a library that helps convert JSON strings into Kotlin objects and vice versa. It makes handling JSON easy and automatic.
Click to reveal answer
beginner
What is Moshi and how is it different from Gson?
Moshi is another JSON library for Android that is modern and supports Kotlin features better, like null safety and Kotlin data classes. It is often preferred for Kotlin projects.
Click to reveal answer
beginner
How do you parse a JSON string into a Kotlin data class using Gson?
You create a Gson object and call fromJson(jsonString, YourDataClass::class.java). This converts the JSON string into your Kotlin object.
Click to reveal answer
intermediate
What annotation does Moshi use to map JSON keys to Kotlin properties?
Moshi uses the @Json(name = "json_key") annotation to map JSON keys to Kotlin properties when the names differ.
Click to reveal answer
beginner
Why is it important to use data classes with Moshi or Gson?
Data classes automatically provide useful functions like equals(), hashCode(), and toString(), and they work well with JSON libraries for easy parsing and serialization.
Click to reveal answer
Which library is known for better Kotlin support and null safety in JSON parsing?
AMoshi
BRetrofit
CJackson
DGson
✗ Incorrect
Moshi is designed with Kotlin in mind and supports Kotlin features like null safety better than Gson.
How do you convert a JSON string to a Kotlin object using Gson?
The fromJson() method of Gson converts JSON string to Kotlin object.
Which annotation is used in Moshi to rename JSON keys?
A@SerializedName
B@Json(name = "key")
C@JsonKey
D@JsonProperty
✗ Incorrect
Moshi uses @Json(name = "key") to map JSON keys to Kotlin properties.
What Kotlin feature helps Moshi parse JSON easily?
AExtension functions
BSealed classes
CCompanion objects
DData classes
✗ Incorrect
Data classes provide a simple structure that Moshi can easily map JSON data into.
Which of these is NOT a function provided automatically by Kotlin data classes?
AparseJson()
Bequals()
CtoString()
DhashCode()
✗ Incorrect
parseJson() is not automatically provided by data classes; it's a function from JSON libraries.
Explain how to parse a JSON string into a Kotlin object using Gson.
Think about the method that converts JSON text to an object.
You got /4 concepts.
Describe the advantages of using Moshi over Gson in Kotlin projects.
Focus on Kotlin-specific features and annotations.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using Gson or Moshi in Android Kotlin apps?
easy
A. To convert JSON text into Kotlin objects
B. To create user interfaces
C. To manage app navigation
D. To handle database queries
Solution
Step 1: Understand JSON parsing role
Gson and Moshi are libraries that help convert JSON strings into Kotlin objects for easier data handling.
Step 2: Identify the correct purpose
They do not create UI, manage navigation, or handle databases directly.
Final Answer:
To convert JSON text into Kotlin objects -> Option A
Quick Check:
JSON parsing = Convert JSON to objects [OK]
Hint: Gson/Moshi = JSON to Kotlin objects [OK]
Common Mistakes:
Thinking Gson/Moshi create UI components
Confusing JSON parsing with database handling
Assuming they manage app navigation
2. Which of the following is the correct way to define a Kotlin data class for this JSON? {"name":"Alice","age":30}
easy
A. class Person(var name: String, var age: String)
B. data class Person(val name: String, val age: Int)
C. data class Person(name: String, age: Int)
D. data class Person(val name: String, val age: String)
Solution
Step 1: Match JSON keys and types
The JSON has keys "name" (String) and "age" (Int).
Step 2: Check Kotlin data class syntax
Correct syntax uses 'data class' with val properties and types matching JSON.
Final Answer:
data class Person(val name: String, val age: Int) -> Option B
Quick Check:
Data class with val and correct types = data class Person(val name: String, val age: Int) [OK]
Hint: Use val and exact types matching JSON keys [OK]
Common Mistakes:
Using var instead of val unnecessarily
Wrong property types (e.g., age as String)
Missing 'val' keyword in data class
3. Given this code snippet using Gson:
val json = "{\"name\":\"Bob\", \"age\":25}"
val person = Gson().fromJson(json, Person::class.java)
println(person.name)
What will be printed?
medium
A. null
B. 25
C. Person
D. Bob
Solution
Step 1: Parse JSON to Person object
Gson converts the JSON string into a Person object with name="Bob" and age=25.
Step 2: Print the name property
println(person.name) outputs the name value, which is "Bob".
Final Answer:
Bob -> Option D
Quick Check:
person.name = "Bob" [OK]
Hint: fromJson maps JSON keys to object properties [OK]
Common Mistakes:
Printing age instead of name
Expecting class name instead of property value
Assuming null if parsing fails
4. What is wrong with this Moshi parsing code?
val json = "{\"name\":\"Eve\", \"age\":28}"
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(Person::class.java)
val person = adapter.fromJson(json)
println(person?.name)
Assuming Person is a correct data class, why might this code fail at runtime?
medium
A. Person class is not a data class
B. JSON string is malformed
C. Missing Moshi Kotlin adapter for data class support
D. Using println instead of Log
Solution
Step 1: Check Moshi setup for Kotlin data classes
Moshi requires adding the KotlinJsonAdapterFactory to support Kotlin features like default values and nullability.
Step 2: Identify missing adapter
The code builds Moshi without .add(KotlinJsonAdapterFactory()), so parsing may fail or produce null.
Final Answer:
Missing Moshi Kotlin adapter for data class support -> Option C
Quick Check:
Moshi needs KotlinJsonAdapterFactory for data classes [OK]
Hint: Add KotlinJsonAdapterFactory to Moshi builder [OK]