In this article we'll be taking a look at how to read and write JSON files in Kotlin, specifically, using the Jackson library. Jackson Dependency To utilize Jackson, we'll want to add its jackson-module-kotlin dependency to our project. If you're using Maven, you can simply add: <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-kotlin</artifactId> <version>2.12.1</version> </dependency> Or, if you're using Gradle, you can add: implementatio
In this article we'll be taking a look at how to read and write JSON files in Kotlin, specifically, using the Jackson library.
To utilize Jackson, we'll want to add its jackson-module-kotlin
dependency to our project. If you're using Maven, you can simply add:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.12.1</version>
</dependency>
Or, if you're using Gradle, you can add:
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.12.1'
With the dependency in place, let's define a JSON object we'll want to read:
{
"id":101,
"username":"admin",
"password":"Admin123",
"fullName":"Best Admin"
}
Let's take a look at how we can deserialize a JSON object into a Kotlin object. Since we'll want to convert the JSON contents into a Kotlin object, let's define a User
data class:
data class User (
val id: Int,
val username: String,
val password: String,
val fullName: String
)
Then, we'll want to instantiate the object mapper, which can easily be done with:
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
// Registering the Kotlin module with the ObjectMpper instance
val mapper = jacksonObjectMapper()
// JSON String
val jsonString = """{
"id":101,
"username":"admin",
"password":"Admin123",
"fullName":"Best Admin"
}"""
With that done, we can pass JSON contents into our ObjectMapper
methods, such as readValue()
, as usual for Jackson:
// Read data from a JSON string
val userFromJson = mapper.readValue<User>(jsonString)
// Or
val userFromJsonWithType: User = mapper.readValue(jsonString)
If we print the value of userFromJson
we'll see something like this:
User(id=101, username=admin, password=Admin123, fullName=Best Admin)
The readValue()
function can be used with or without the Class
parameter, as you saw a little earlier with the two variables (userFromJson
and userFromJsonWithType
).
Note: Using the function without the Class
parameter will materialize the type and automatically create a TypeReference
for Jackson.
AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!
AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!
AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!
This blog provides quick analysis to decide whether to opt for Kotlin or React Native to build robust and secure mobile application.
In this Kotlin tutorial, the lead Kotlin language designer will show you how you can write more idiomatic Kotlin, what the benefits are, and help you discover some of the most powerful yet lesser known features of Kotlin. Kotlin is similar to the Java programming language, so it's natural that your Kotlin code looks very much like Java code when you are first start to use the language.