如何使用kotlin序列化我的类对象?

时间:2018-10-15 06:00:13

标签: android serialization kotlin deserialization

我有片段,我需要将我的值构造函数传递给OncretaeView()方法,以便我在构造函数和tr中接收我的值,以将我的对象转换为序列化的对象,但不进行转换。这是我的代码。请指导我如何序列化和反序列化我的对象。 预先感谢

import java.io.Serializable

class SendingFragment: Fragment(),Serializable {
companion object {
        /**
         * new instance pattern for fragment
         */
        @JvmStatic
        fun newInstance(myObject: List<TransactionEntity>?, cc: Context, appDatabase: AppDatabase, networkDefinitionProvider: NetworkDefinitionProvider, incoming: TransactionAdapterDirection): SendingFragment {

            val gson = Gson()
            val gson1 = GsonBuilder().create()
            val model = myObject as List<TransactionEntity>
            val IT = gson.toJson(model)
            System.out.println("json representation :" + IT)

            val bo = ByteArrayOutputStream()
            val so = ObjectOutputStream(bo)
            so.writeObject(appDatabase)
            so.flush()
            val serializedObject = String(Base64.encode(bo.toByteArray()))

            val bundle = Bundle()
            bundle.putString("bundleValue", IT)
            bundle.putSerializable("serializedObject",serializedObject)
            val sendFragament: SendingFragment = SendingFragment()
            sendFragament.setArguments(bundle)
            return sendFragament
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val mFragserializedObject = arguments!!.getSerializable("serializedObject")
        System.out.println( "json serializedObject" + mFragserializedObject)
    }
}

Appdatabase.kt

@Database(entities = {AddressBookEntry.class, Token.class, Balance.class, TransactionEntity.class}, version = 1)
@TypeConverters({RoomTypeConverters.class})
public abstract class AppDatabase extends RoomDatabase {

    public abstract AddressBookDAO getAddressBook();

    public abstract TokenDAO getTokens();

    public abstract TransactionDAO getTransactions();

    public abstract BalanceDAO getBalances();
}

更新

class BeanDemo : Serializable {

    var MyAppDatabase: AppDatabase ? = null

    constructor() {

    }

    //secoutry constructor

    constructor(appDatabase: AppDatabase){
        this. MyAppDatabase =  appDatabase
        Log.d("appDatabase : Bean", "appDatabase$appDatabase")
    }

    //getter/setter methods

    fun getName(): AppDatabase? {
        Log.d("appDatabase : getName", "appDatabase$MyAppDatabase")
        return MyAppDatabase
    }

    fun setName(NEWAPPDB: AppDatabase) {
        Log.d("appDatabase : NEWAPPDB", "appDatabase$NEWAPPDB")
        MyAppDatabase = NEWAPPDB
    }
}

**myfragent.kt**



 fun newInstance(myObject: List<TransactionEntity>?, cc: Context, appDatabase: AppDatabase, networkDefinitionProvider: NetworkDefinitionProvider, incoming: TransactionAdapterDirection): SendingFragment {

                val gson = Gson()
                val gson1 = GsonBuilder().create()
                val model = myObject as List<TransactionEntity>
                val IT = gson.toJson(model)
    //             Here
                val sampleVar = BeanDemo(appDatabase)
                sampleVar.setName(appDatabase)
 val bundle = Bundle()
            bundle.putString("bundleValue", IT)
            bundle.putSerializable("serializedObject",sampleVar)
            val sendFragament: SendingFragment = SendingFragment()
            sendFragament.setArguments(bundle)
            return sendFragament

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {



        val bundle = arguments
        val obj = bundle!!.getSerializable("serializedObject") as BeanDemo
        val name = obj.getName()

        }

但是 val obj = 为null,使用setter方法保存该值,但不返回该值。

1 个答案:

答案 0 :(得分:0)

创建Model/POJO类时,然后将extendclass一起Serializable

示例-

class JsonData : Serializable {

    @SerializedName("hasPreviousData")
    var hasPreviousData: Boolean = false

    @SerializedName("dataList")
    var dataList: ArrayList<DataList>? = null

    inner class DataList : Serializable {
        @SerializedName("id")
        var id: String? = null
        @SerializedName("createdAt")
        var createdAt: String? = null
        @SerializedName("name")
        var name: String? = null
    }
}