Skip to content

Commit d4d4f64

Browse files
committed
Fixed collection settings serialization
1 parent 5c9ed4a commit d4d4f64

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
package com.lambda.config.settings.collections
1919

20+
import com.google.gson.JsonElement
21+
import com.google.gson.reflect.TypeToken
22+
import com.lambda.Lambda.gson
2023
import com.lambda.config.AbstractSetting
2124
import com.lambda.gui.dsl.ImGuiBuilder
2225
import imgui.flag.ImGuiSelectableFlags.DontClosePopups
2326
import java.lang.reflect.Type
27+
import kotlin.jvm.java
2428

2529
/**
2630
* @see [com.lambda.config.Configurable]
@@ -38,6 +42,23 @@ class ListSetting<T : Any>(
3842
description,
3943
visibility
4044
) {
45+
private val strListType =
46+
TypeToken.getParameterized(MutableList::class.java, String::class.java).type
47+
48+
// When serializing the list to json we do not want to serialize the elements' classes, but
49+
// their stringified representation.
50+
// If we do serialize the classes we'll run into missing type adapters errors by Gson.
51+
override fun toJson(): JsonElement =
52+
gson.toJsonTree(value.map { it.toString() })
53+
54+
override fun loadFromJson(serialized: JsonElement) {
55+
val strList = gson.fromJson<MutableList<String>>(serialized, strListType)
56+
.mapNotNull { str -> immutableList.find { it.toString() == str } }
57+
.toMutableList()
58+
59+
value = strList
60+
}
61+
4162
override val layout: ImGuiBuilder.() -> Unit
4263
get() =
4364
{

src/main/kotlin/com/lambda/config/settings/collections/SetSetting.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package com.lambda.config.settings.collections
1919

20+
import com.google.gson.JsonElement
21+
import com.google.gson.reflect.TypeToken
22+
import com.lambda.Lambda.gson
2023
import com.lambda.config.AbstractSetting
2124
import com.lambda.gui.dsl.ImGuiBuilder
2225
import imgui.flag.ImGuiSelectableFlags.DontClosePopups
@@ -38,6 +41,24 @@ class SetSetting<T : Any>(
3841
description,
3942
visibility
4043
) {
44+
private val strSetType =
45+
TypeToken.getParameterized(Set::class.java, String::class.java).type
46+
47+
// When serializing the list to json we do not want to serialize the elements' classes, but
48+
// their stringified representation.
49+
// If we do serialize the classes we'll run into missing type adapters errors by Gson.
50+
override fun toJson(): JsonElement =
51+
gson.toJsonTree(value.map { it.toString() })
52+
53+
override fun loadFromJson(serialized: JsonElement) {
54+
val strSet = gson.fromJson<Set<String>>(serialized, strSetType)
55+
.mapNotNull { str -> immutableSet.find { it.toString() == str } }
56+
.toMutableSet()
57+
58+
value = strSet
59+
}
60+
61+
4162
override val layout: ImGuiBuilder.() -> Unit
4263
get() =
4364
{

0 commit comments

Comments
 (0)