|
17 | 17 |
|
18 | 18 | package com.lambda.config.settings.collections |
19 | 19 |
|
20 | | -import com.google.gson.JsonArray |
21 | 20 | import com.google.gson.JsonElement |
22 | 21 | import com.google.gson.reflect.TypeToken |
23 | 22 | import com.lambda.Lambda.gson |
@@ -47,7 +46,8 @@ import java.lang.reflect.Type |
47 | 46 | open class CollectionSetting<R : Any>( |
48 | 47 | defaultValue: MutableCollection<R>, |
49 | 48 | private var immutableCollection: Collection<R>, |
50 | | - type: Type |
| 49 | + type: Type, |
| 50 | + private val serialize: Boolean, |
51 | 51 | ) : SettingCore<MutableCollection<R>>( |
52 | 52 | defaultValue, |
53 | 53 | type |
@@ -107,16 +107,18 @@ open class CollectionSetting<R : Any>( |
107 | 107 |
|
108 | 108 | context(setting: Setting<*, MutableCollection<R>>) |
109 | 109 | override fun toJson(): JsonElement = |
110 | | - gson.toJsonTree(value) |
| 110 | + gson.toJsonTree(value, type) |
111 | 111 |
|
112 | 112 | context(setting: Setting<*, MutableCollection<R>>) |
113 | 113 | override fun loadFromJson(serialized: JsonElement) { |
114 | | - val strList = gson.fromJson<Collection<String>>(serialized, strListType) |
115 | | - .mapNotNull { str -> immutableCollection.find { it.toString() == str } } |
116 | | - .toMutableList() |
| 114 | + val strList = |
| 115 | + if (serialize) gson.fromJson(serialized, type) |
| 116 | + else gson.fromJson<Collection<String>>(serialized, strListType) |
| 117 | + .mapNotNull { str -> immutableCollection.find { it.toString() == str } } |
| 118 | + .toMutableList() |
117 | 119 |
|
118 | | - value = strList |
119 | | - } |
| 120 | + value = strList |
| 121 | + } |
120 | 122 |
|
121 | 123 | companion object { |
122 | 124 | fun <T : CollectionSetting<R>, R : Any> Setting<T, MutableCollection<R>>.onSelect(block: SafeContext.(R) -> Unit) = apply { |
|
0 commit comments