package com.navinfo.outdoor.util;

import androidx.room.TypeConverter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

/**
 * describe:使用Room框架,room不支持对象中直接存储集合
 * https://stackoverflow.com/questions/53812636/android-error-cannot-figure-out-how-to-save-this-field-into-database-you-can
 */
public class StringTypeConverter {
    Gson gson = new Gson();

    @TypeConverter
    public List<String> stringToSomeObjectList(String data) {
        if (data == null) {
            return Collections.emptyList();
        }

        Type listType = new TypeToken<List<String>>() {}.getType();

        return gson.fromJson(data, listType);
    }

    @TypeConverter
    public String someObjectListToString(List<String> someObjects) {
        return gson.toJson(someObjects);
    }
}
