Skip to content

Commit 4f0897f

Browse files
committed
KTLN-746: Reading from a YAML File in Kotlin
1 parent 924e749 commit 4f0897f

File tree

9 files changed

+801
-0
lines changed

9 files changed

+801
-0
lines changed

kotlin-yaml/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>kotlin-yaml</artifactId>
7+
<name>kotlin-yaml</name>
8+
<packaging>jar</packaging>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>kotlin-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<properties>
18+
<kotlin.version>1.8.0</kotlin.version>
19+
<serialization.version>1.5.0</serialization.version>
20+
<serialization.jvm.version>1.5.0</serialization.jvm.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.jetbrains.kotlin</groupId>
26+
<artifactId>kotlin-test</artifactId>
27+
<version>${kotlin.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.charleskorn.kaml</groupId>
32+
<artifactId>kaml-jvm</artifactId>
33+
<version>0.58.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>net.mamoe.yamlkt</groupId>
37+
<artifactId>yamlkt-jvm</artifactId>
38+
<version>0.13.0</version>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<sourceDirectory>src/main/kotlin</sourceDirectory>
44+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
45+
46+
<plugins>
47+
<plugin>
48+
<groupId>org.jetbrains.kotlin</groupId>
49+
<artifactId>kotlin-maven-plugin</artifactId>
50+
<version>${kotlin.version}</version>
51+
<executions>
52+
<execution>
53+
<id>compile</id>
54+
<phase>compile</phase>
55+
<goals>
56+
<goal>compile</goal>
57+
</goals>
58+
</execution>
59+
<execution>
60+
<id>test-compile</id>
61+
<phase>test-compile</phase>
62+
<goals>
63+
<goal>test-compile</goal>
64+
</goals>
65+
</execution>
66+
</executions>
67+
<configuration>
68+
<compilerPlugins>
69+
<plugin>kotlinx-serialization</plugin>
70+
</compilerPlugins>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.yaml.kaml
2+
3+
import com.baeldung.yaml.model.Address
4+
import com.baeldung.yaml.model.User
5+
import com.baeldung.yaml.model.Users
6+
import com.charleskorn.kaml.*
7+
import kotlinx.serialization.decodeFromString
8+
9+
10+
fun getUsersUsingUsersSerializer(fileContent: String): Users {
11+
val yaml = Yaml(configuration = YamlConfiguration(strictMode = false))
12+
val data = yaml.decodeFromString<Users>(fileContent)
13+
return data
14+
}
15+
16+
fun getUsersUsingYamlMapSerializer(fileContent: String): Users {
17+
val yaml = Yaml(configuration = YamlConfiguration(strictMode = false))
18+
val result: MutableList<User> = mutableListOf()
19+
20+
val data: YamlMap = yaml.parseToYamlNode(fileContent).yamlMap
21+
val usersYamlList: YamlList = data.get<YamlList>("users")!!.yamlList
22+
23+
usersYamlList.items.forEach { userYamlListItem ->
24+
val name = (userYamlListItem as YamlMap).getScalar("name")!!.content
25+
val age = userYamlListItem.getScalar("age")!!.toInt()
26+
val addressYamlMap = userYamlListItem.get<YamlMap>("address")!!
27+
val city = addressYamlMap.getScalar("city")!!.content
28+
val country = addressYamlMap.getScalar("country")!!.content
29+
val user = User(name, age, Address(city, country))
30+
result.add(user)
31+
}
32+
33+
return Users(result)
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.yaml.model
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Address(
7+
val city: String,
8+
val country: String,
9+
)
10+
11+
@Serializable
12+
data class User(
13+
val name: String,
14+
val age: Int,
15+
val address: Address
16+
)
17+
18+
@Serializable
19+
data class Users(
20+
val users: List<User>
21+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.yaml.yamlkt
2+
3+
import com.baeldung.yaml.model.Users
4+
import com.baeldung.yaml.model.User
5+
import com.baeldung.yaml.model.Address
6+
import net.mamoe.yamlkt.*
7+
8+
fun getUsersUsingUsersSerializer(fileContent: String): Users {
9+
val users: Users = Yaml.Default.decodeFromString(Users.serializer(), fileContent)
10+
return users
11+
}
12+
13+
14+
fun getUsersUsingYamlMapSerializer(fileContent: String): Users {
15+
val result: MutableList<User> = mutableListOf()
16+
17+
val data: YamlMap = Yaml.Default.decodeFromString(YamlMap.serializer(), fileContent)
18+
val usersYamlList: YamlList = data.get("users") as YamlList
19+
20+
usersYamlList.forEach { userYamlListItem ->
21+
val name = (userYamlListItem as YamlMap).getString("name")
22+
val age = userYamlListItem.getInt("age")
23+
val addressYamlMap = (userYamlListItem.get("address") as YamlMap)
24+
val city = addressYamlMap.getString("city")
25+
val country = addressYamlMap.getString("country")
26+
val user = User(name, age, Address(city, country))
27+
result.add(user)
28+
}
29+
30+
return Users(result)
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.yaml
2+
3+
import com.baeldung.yaml.model.Users
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertNotNull
6+
7+
8+
fun verifyUsers(users: Users) {
9+
assertNotNull(users)
10+
11+
val user1 = users.users[0]
12+
assertNotNull(user1)
13+
assertEquals("Alice", user1.name)
14+
assertEquals(30, user1.age)
15+
assertEquals("New York", user1.address.city)
16+
assertEquals("USA", user1.address.country)
17+
18+
val user2 = users.users[1]
19+
assertNotNull(user2)
20+
assertEquals("Bob", user2.name)
21+
assertEquals(35, user2.age)
22+
assertEquals("London", user2.address.city)
23+
assertEquals("UK", user2.address.country)
24+
}
25+

0 commit comments

Comments
 (0)