package com.navinfo.outdoor.util;

import androidx.room.TypeConverter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.navinfo.outdoor.bean.Info;

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 PhotoInfoConverter {
    Gson gson = new Gson();

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

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

        return gson.fromJson(data, listType);
    }

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

}
