@@ -2,11 +2,12 @@ package com.mintanable.notethepad.feature_ai.data.repository
22
33import android.content.Context
44import android.util.Log
5- import com.google.gson.Gson
65import com.mintanable.notethepad.core.common.DispatcherProvider
76import com.mintanable.notethepad.core.model.ai.AiModel
87import com.mintanable.notethepad.core.model.ai.AiModelCatalog
98import com.mintanable.notethepad.feature_ai.BuildConfig
9+ import kotlinx.serialization.Serializable
10+ import kotlinx.serialization.json.Json
1011import com.mintanable.notethepad.feature_ai.domain.repository.AiModelRepository
1112import dagger.hilt.android.qualifiers.ApplicationContext
1213import kotlinx.coroutines.flow.Flow
@@ -36,7 +37,10 @@ class AiModelRepositoryImpl @Inject constructor(
3637 .readTimeout(30 , TimeUnit .SECONDS )
3738 .writeTimeout(30 , TimeUnit .SECONDS )
3839 .build()
39- private val gson = Gson ()
40+ private val json = Json {
41+ ignoreUnknownKeys = true
42+ coerceInputValues = true
43+ }
4044
4145 override fun getModels (): Flow <List <AiModel >> = flow {
4246 if (_cachedModels .value.isNotEmpty()) {
@@ -121,8 +125,8 @@ class AiModelRepositoryImpl @Inject constructor(
121125 val gistResponse = response.body.string()
122126
123127 if (! gistResponse.isNullOrEmpty()) {
124- val wrapper = gson.fromJson (gistResponse, GistWrapper :: class .java )
125- val content = wrapper? .files?.get(MODEL_CATALOG_FILENAME )?.content
128+ val wrapper = json.decodeFromString< GistWrapper > (gistResponse)
129+ val content = wrapper.files?.get(MODEL_CATALOG_FILENAME )?.content
126130
127131 if (content != null ) {
128132 val internalFile = File (context.filesDir, MODEL_CATALOG_FILENAME )
@@ -141,7 +145,7 @@ class AiModelRepositoryImpl @Inject constructor(
141145 return try {
142146 val file = File (context.filesDir, MODEL_CATALOG_FILENAME )
143147 if (file.exists()) {
144- file.readText().let { gson.fromJson (it, AiModelCatalog :: class .java ) }
148+ file.readText().let { json.decodeFromString< AiModelCatalog > (it) }
145149 } else null
146150 } catch (e: Exception ) {
147151 Log .e(" kptest" , " Failed to parse internal catalog" , e)
@@ -154,18 +158,20 @@ class AiModelRepositoryImpl @Inject constructor(
154158 context.assets.open(MODEL_CATALOG_FILENAME )
155159 .bufferedReader()
156160 .use { it.readText() }
157- .let { gson.fromJson (it, AiModelCatalog :: class .java ) }
161+ .let { json.decodeFromString< AiModelCatalog > (it) }
158162 } catch (e: Exception ) {
159163 Log .e(" kptest" , " Failed to parse assets catalog" , e)
160164 null
161165 }
162166 }
163167}
164168
169+ @Serializable
165170data class GistWrapper (
166171 val files : Map <String , GistFile >? = null
167172)
168173
174+ @Serializable
169175data class GistFile (
170176 val content : String? = null
171177)
0 commit comments