File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
src/main/kotlin/com/lambda/config/settings/collections Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1717
1818package 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
2023import com.lambda.config.AbstractSetting
2124import com.lambda.gui.dsl.ImGuiBuilder
2225import imgui.flag.ImGuiSelectableFlags.DontClosePopups
2326import 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 {
Original file line number Diff line number Diff line change 1717
1818package 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
2023import com.lambda.config.AbstractSetting
2124import com.lambda.gui.dsl.ImGuiBuilder
2225import 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 {
You can’t perform that action at this time.
0 commit comments