StringListConverter.kt 1.34 KB
Newer Older
1
2
3
4
package com.frostnerd.smokescreen.database.converters

import androidx.room.TypeConverter
import com.google.gson.Gson
Daniel Wolf's avatar
Daniel Wolf committed
5
import com.google.gson.reflect.TypeToken
6
7


Daniel Wolf's avatar
Daniel Wolf committed
8
9
/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
10
 *
Daniel Wolf's avatar
Daniel Wolf committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact the developer at daniel.wolf@frostnerd.com.
25
26
27
 */
class StringListConverter {
    private val gson = Gson()
Daniel Wolf's avatar
Daniel Wolf committed
28
    private val type = object:TypeToken<List<String>>() {}.type
29
30

    @TypeConverter
Daniel Wolf's avatar
Daniel Wolf committed
31
32
33
    fun stringToList(value: String): List<String> {
        return if(value == "[]") emptyList()
        else gson.fromJson(value, type)
34
35
36
    }

    @TypeConverter
Daniel Wolf's avatar
Daniel Wolf committed
37
38
39
    fun someObjectListToString(value: List<String>): String {
        return if(value.isEmpty()) "[]"
        else gson.toJson(value)
40
41
    }
}